プログラミング演習Ⅲ- sorting-2 bubble sort and insertion...
TRANSCRIPT
![Page 1: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/1.jpg)
プログラミング演習Ⅲ- Sorting-2Bubble Sort and Insertion Sort
P. Ravindra S. De Silva
e-Mail: [email protected], Room F-413
URL: www.icd.cs.tut.ac.jp/~ravi/prog3/index_j.html
![Page 2: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/2.jpg)
Page 2
バブルソート
![Page 3: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/3.jpg)
Page 3
ソート
順序がバラバラの数列を順番通りに並べる
512354277 101
1 2 3 4 5 6
5 12 35 42 77 101
1 2 3 4 5 6
![Page 4: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/4.jpg)
Page 4
一番大きい値を“バブルアップ”する
横並びの一つの数列について
– 先頭から終端までうごく
– 一対比較評価と交換則を使用して一番大きい値を終端に“バブル”する
512354277 101
1 2 3 4 5 6
![Page 5: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/5.jpg)
Page 5
512354277 101
1 2 3 4 5 6
Swap42 77
一番大きい値を“バブルアップ”する
横並びの一つの数列について
– 先頭から終端までうごく
– 一対比較評価と交換則を使用して一番大きい値を終端に“バブル”する
![Page 6: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/6.jpg)
Page 6
512357742 101
1 2 3 4 5 6
Swap35 77
一番大きい値を“バブルアップ”する
横並びの一つの数列について
– 先頭から終端までうごく
– 一対比較評価と交換則を使用して一番大きい値を終端に“バブル”する
![Page 7: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/7.jpg)
Page 7
512773542 101
1 2 3 4 5 6
Swap12 77
一番大きい値を“バブルアップ”する
横並びの一つの数列について
– 先頭から終端までうごく
– 一対比較評価と交換則を使用して一番大きい値を終端に“バブル”する
![Page 8: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/8.jpg)
Page 8
577123542 101
1 2 3 4 5 6
交換する必要はない
一番大きい値を“バブルアップ”する
横並びの一つの数列について
– 先頭から終端までうごく
– 一対比較評価と交換則を使用して一番大きい値を終端に“バブル”する
![Page 9: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/9.jpg)
Page 9
577123542 101
1 2 3 4 5 6
Swap5 101
一番大きい値を“バブルアップ”する
横並びの一つの数列について
– 先頭から終端までうごく
– 一対比較評価と交換則を使用して一番大きい値を終端に“バブル”する
![Page 10: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/10.jpg)
Page 10
77123542 5
1 2 3 4 5 6
101
一番大きな値が適切な位置に配置された
一番大きい値を“バブルアップ”する
横並びの一つの数列について
– 先頭から終端までうごく
– 一対比較評価と交換則を使用して一番大きい値を終端に“バブル”する
![Page 11: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/11.jpg)
Page 11
取り扱う要素
一番大きな値は正しい位置に配置されたものの、
残りの値はまだ不揃いのまま
そのため、この処理を何度も繰り返す必要がある
77123542 5
1 2 3 4 5 6
101
一番大きな値だけは適切な位置に配置されている
![Page 12: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/12.jpg)
Page 12
すべての要素をバブルアップする
77123542 51 2 3 4 5 6
101
5421235 771 2 3 4 5 6
101
4253512 771 2 3 4 5 6
101
4235512 771 2 3 4 5 6
101
4235125 771 2 3 4 5 6
101
N -
1
![Page 13: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/13.jpg)
Page 13
to_do, index isoftype Num
to_do <- N – 1
loop
exitif(to_do = 0)
index <- 1
loop
exitif(index > to_do)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop
to_do <- to_do - 1
endloop
endprocedure // Bubblesort
内部
ループ
外部
ループ
アルゴリズム
![Page 14: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/14.jpg)
Page 14
動作例(アニメーション)
674523 14 6 3398 42
to_do
index
7
N 8
1 2 3 4 5 6 7 8
![Page 15: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/15.jpg)
Page 15
674523 14 6 3398 42
to_do
index
7
1
N 8
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 16: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/16.jpg)
Page 16
674523 14 6 3398 42
to_do
index
7
1
N 8
交換
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 17: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/17.jpg)
Page 17
674598 14 6 3323 42
to_do
index
7
1
N 8
交換
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 18: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/18.jpg)
Page 18
674598 14 6 3323 42
to_do
index
7
2
N 8
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 19: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/19.jpg)
Page 19
674598 14 6 3323 42
to_do
index
7
2
N 8
交換
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 20: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/20.jpg)
Page 20
679845 14 6 3323 42
to_do
index
7
2
N 8
交換
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 21: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/21.jpg)
Page 21
679845 14 6 3323 42
to_do
index
7
3
N 8
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 22: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/22.jpg)
Page 22
679845 14 6 3323 42
to_do
index
7
3
N 8
交換
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 23: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/23.jpg)
Page 23
671445 98 6 3323 42
to_do
index
7
3
N 8
交換
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 24: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/24.jpg)
Page 24
671445 98 6 3323 42
to_do
index
7
4
N 8
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 25: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/25.jpg)
Page 25
671445 98 6 3323 42
to_do
index
7
4
N 8
交換
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 26: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/26.jpg)
Page 26
671445 6 98 3323 42
to_do
index
7
4
N 8
交換
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 27: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/27.jpg)
Page 27
671445 6 98 3323 42
to_do
index
7
5
N 8
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 28: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/28.jpg)
Page 28
671445 6 98 3323 42
to_do
index
7
5
N 8
交換
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 29: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/29.jpg)
Page 29
981445 6 67 3323 42
to_do
index
7
5
N 8
交換
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 30: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/30.jpg)
Page 30
981445 6 67 3323 42
to_do
index
7
6
N 8
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 31: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/31.jpg)
Page 31
981445 6 67 3323 42
to_do
index
7
6
N 8
交換
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 32: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/32.jpg)
Page 32
331445 6 67 9823 42
to_do
index
7
6
N 8
交換
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 33: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/33.jpg)
Page 33
331445 6 67 9823 42
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8
動作例(アニメーション)
![Page 34: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/34.jpg)
Page 34
331445 6 67 9823 42
to_do
index
7
7
N 8
交換
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 35: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/35.jpg)
Page 35
331445 6 67 4223 98
to_do
index
7
7
N 8
交換
1 2 3 4 5 6 7 8
動作例(アニメーション)
![Page 36: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/36.jpg)
Page 36
外部ループ1周目のあと
331445 6 67 4223 98
to_do
index
7
8
N 8
最初の“バブルアップ”が終了した
1 2 3 4 5 6 7 8
![Page 37: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/37.jpg)
Page 37
二回目の“バブルアップ”
331445 6 67 4223 98
to_do
index
6
1
N 8
1 2 3 4 5 6 7 8
![Page 38: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/38.jpg)
Page 38
331445 6 67 4223 98
to_do
index
6
1
N 8
交換なし
1 2 3 4 5 6 7 8
二回目の“バブルアップ”
![Page 39: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/39.jpg)
Page 39
331445 6 67 4223 98
to_do
index
6
2
N 8
1 2 3 4 5 6 7 8
二回目の“バブルアップ”
![Page 40: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/40.jpg)
Page 40
331445 6 67 4223 98
to_do
index
6
2
N 8
交換
1 2 3 4 5 6 7 8
二回目の“バブルアップ”
![Page 41: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/41.jpg)
Page 41
334514 6 67 4223 98
to_do
index
6
2
N 8
交換
1 2 3 4 5 6 7 8
二回目の“バブルアップ”
![Page 42: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/42.jpg)
Page 42
334514 6 67 4223 98
to_do
index
6
3
N 8
1 2 3 4 5 6 7 8
二回目の“バブルアップ”
![Page 43: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/43.jpg)
Page 43
334514 6 67 4223 98
to_do
index
6
3
N 8
交換
1 2 3 4 5 6 7 8
二回目の“バブルアップ”
![Page 44: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/44.jpg)
Page 44
33614 45 67 4223 98
to_do
index
6
3
N 8
交換
1 2 3 4 5 6 7 8
二回目の“バブルアップ”
![Page 45: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/45.jpg)
Page 45
33614 45 67 4223 98
to_do
index
6
4
N 8
1 2 3 4 5 6 7 8
二回目の“バブルアップ”
![Page 46: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/46.jpg)
Page 46
33614 45 67 4223 98
to_do
index
6
4
N 8
交換なし
1 2 3 4 5 6 7 8
二回目の“バブルアップ”
![Page 47: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/47.jpg)
Page 47
33614 45 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
5
N 8
二回目の“バブルアップ”
![Page 48: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/48.jpg)
Page 48
33614 45 67 4223 98
to_do
index
6
5
N 8
交換
1 2 3 4 5 6 7 8
二回目の“バブルアップ”
![Page 49: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/49.jpg)
Page 49
67614 45 33 4223 98
to_do
index
6
5
N 8
交換
1 2 3 4 5 6 7 8
二回目の“バブルアップ”
![Page 50: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/50.jpg)
Page 50
67614 45 33 4223 98
to_do
index
6
6
N 8
1 2 3 4 5 6 7 8
二回目の“バブルアップ”
![Page 51: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/51.jpg)
Page 51
67614 45 33 4223 98
to_do
index
6
6
N 8
交換
1 2 3 4 5 6 7 8
二回目の“バブルアップ”
![Page 52: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/52.jpg)
Page 52
42614 45 33 6723 98
to_do
index
6
6
N 8
交換
1 2 3 4 5 6 7 8
二回目の“バブルアップ”
![Page 53: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/53.jpg)
Page 53
外部ループの2周目のあと
42614 45 33 6723 98
to_do
index
6
7
N 8
二番目の“バブルアップ”が終了
1 2 3 4 5 6 7 8
![Page 54: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/54.jpg)
Page 54
三度目の“バブルアップ”
42614 45 33 6723 98
to_do
index
5
1
N 8
1 2 3 4 5 6 7 8
![Page 55: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/55.jpg)
Page 55
挿入ソート
![Page 56: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/56.jpg)
Page 56 56
挿入ソート
ソート処理中の数列はソート済のデータと未ソートの二つに分かれる
それぞれのパスにおいて、未ソートの数列の一番目の要素がソート済の数列中に挿入される
![Page 57: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/57.jpg)
Page 57 57
挿入ソート
![Page 58: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/58.jpg)
Page 58
例: 挿入ソート
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
![Page 59: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/59.jpg)
Page 59
例: 挿入ソート
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
30 10 40 20
1 2 3 4
i = ∅ j = ∅ key = ∅A[j] = ∅ A[j+1] = ∅
![Page 60: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/60.jpg)
Page 60
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
30 10 40 20
1 2 3 4
i = 2 j = 1 key = 10A[j] = 30 A[j+1] = 10
例: 挿入ソート
![Page 61: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/61.jpg)
Page 61
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
30 30 40 20
1 2 3 4
i = 2 j = 1 key = 10A[j] = 30 A[j+1] = 30
例: 挿入ソート
![Page 62: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/62.jpg)
Page 62
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
30 30 40 20
1 2 3 4
i = 2 j = 1 key = 10A[j] = 30 A[j+1] = 30
例: 挿入ソート
![Page 63: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/63.jpg)
Page 63
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
30 30 40 20
1 2 3 4
i = 2 j = 0 key = 10A[j] = ∅ A[j+1] = 30
例: 挿入ソート
![Page 64: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/64.jpg)
Page 64
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
30 30 40 20
1 2 3 4
i = 2 j = 0 key = 10A[j] = ∅ A[j+1] = 30
例: 挿入ソート
![Page 65: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/65.jpg)
Page 65
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 2 j = 0 key = 10A[j] = ∅ A[j+1] = 10
例: 挿入ソート
![Page 66: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/66.jpg)
Page 66
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 0 key = 10A[j] = ∅ A[j+1] = 10
例: 挿入ソート
![Page 67: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/67.jpg)
Page 67
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 0 key = 40A[j] = ∅ A[j+1] = 10
例: 挿入ソート
![Page 68: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/68.jpg)
Page 68
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 0 key = 40A[j] = ∅ A[j+1] = 10
例: 挿入ソート
![Page 69: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/69.jpg)
Page 69
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 2 key = 40A[j] = 30 A[j+1] = 40
例: 挿入ソート
![Page 70: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/70.jpg)
Page 70
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 2 key = 40A[j] = 30 A[j+1] = 40
例: 挿入ソート
![Page 71: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/71.jpg)
Page 71
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 2 key = 40A[j] = 30 A[j+1] = 40
例: 挿入ソート
![Page 72: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/72.jpg)
Page 72
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 4 j = 2 key = 40A[j] = 30 A[j+1] = 40
例: 挿入ソート
![Page 73: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/73.jpg)
Page 73
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 4 j = 2 key = 20A[j] = 30 A[j+1] = 40
例: 挿入ソート
![Page 74: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/74.jpg)
Page 74
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 4 j = 2 key = 20A[j] = 30 A[j+1] = 40
例: 挿入ソート
![Page 75: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/75.jpg)
Page 75
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 4 j = 3 key = 20A[j] = 40 A[j+1] = 20
例: 挿入ソート
![Page 76: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/76.jpg)
Page 76
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 4 j = 3 key = 20A[j] = 40 A[j+1] = 20
例: 挿入ソート
![Page 77: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/77.jpg)
Page 77
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 40 40
1 2 3 4
i = 4 j = 3 key = 20A[j] = 40 A[j+1] = 40
例: 挿入ソート
![Page 78: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/78.jpg)
Page 78
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 40 40
1 2 3 4
i = 4 j = 3 key = 20A[j] = 40 A[j+1] = 40
例: 挿入ソート
![Page 79: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/79.jpg)
Page 79
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 40 40
1 2 3 4
i = 4 j = 3 key = 20A[j] = 40 A[j+1] = 40
例: 挿入ソート
![Page 80: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/80.jpg)
Page 80
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 40 40
1 2 3 4
i = 4 j = 2 key = 20A[j] = 30 A[j+1] = 40
例: 挿入ソート
![Page 81: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/81.jpg)
Page 81
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 40 40
1 2 3 4
i = 4 j = 2 key = 20A[j] = 30 A[j+1] = 40
例: 挿入ソート
![Page 82: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/82.jpg)
Page 82
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 30 40
1 2 3 4
i = 4 j = 2 key = 20A[j] = 30 A[j+1] = 30
例: 挿入ソート
![Page 83: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/83.jpg)
Page 83
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 30 40
1 2 3 4
i = 4 j = 2 key = 20A[j] = 30 A[j+1] = 30
例: 挿入ソート
![Page 84: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/84.jpg)
Page 84
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 30 40
1 2 3 4
i = 4 j = 1 key = 20A[j] = 10 A[j+1] = 30
例: 挿入ソート
![Page 85: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/85.jpg)
Page 85
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 30 30 40
1 2 3 4
i = 4 j = 1 key = 20A[j] = 10 A[j+1] = 30
例: 挿入ソート
![Page 86: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/86.jpg)
Page 86
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 20 30 40
1 2 3 4
i = 4 j = 1 key = 20A[j] = 10 A[j+1] = 20
例: 挿入ソート
![Page 87: プログラミング演習Ⅲ- Sorting-2 Bubble Sort and Insertion Sortravi/prog3/Files/Day7-Jpn.pdfPage 8 42 35 12 77 101 5 1 2 3 4 5 6 交換する必要はない 一番大きい値を“バブルアップ”する](https://reader033.vdocuments.pub/reader033/viewer/2022052815/60a637fc5a22342c125f77e5/html5/thumbnails/87.jpg)
Page 87
InsertionSort(A, n) {for i = 2 to n {
key = A[i]j = i - 1;while (j > 0) and (A[j] > key) {
A[j+1] = A[j]j = j - 1
}A[j+1] = key
}
}
10 20 30 40
1 2 3 4
i = 4 j = 1 key = 20A[j] = 10 A[j+1] = 20
終了!
例: 挿入ソート