| English | 意味 |
|---|---|
| bubble sort/ˈbʌbl sɔːt/ | バブルソート |
| insertion sort/ɪnˈsɜːʃn sɔːt/ | 挿入ソート |
| in place/ɪn pleɪs/ | インプレイス |
| stable/ˈsteɪbl/ | 安定 |
The sort that is slow on purpose
- Every serious library sorts with an algorithm no exam asks you to write. Bubble sort and insertion sort are both $O(n^2)$, and both are beaten by any decent sort on a large list.
- They are on the syllabus anyway, and for a good reason: they are short enough to trace by hand, and tracing one is how you learn what a sort actually does to an array.
- There is also a real case for insertion sort. On a list that is small, or already nearly sorted, it is genuinely the fastest thing there is, and real libraries switch to it for exactly those cases.
- This lesson is bubble sort 冒泡排序 and insertion sort 插入排序: the algorithms, their behaviour, and where each one wins.
Bubble sort
FOR pass ← 1 TO n - 1
swapped ← FALSE
FOR i ← 1 TO n - pass
IF A[i] > A[i + 1] THEN
swap A[i], A[i + 1]
swapped ← TRUE
ENDIF
NEXT i
IF NOT swapped THEN // already sorted
EXIT FOR
ENDIF
NEXT pass
- Each pass compares adjacent pairs and swaps any that are out of order, so the largest remaining value "bubbles" to the end.
- After pass $k$ the last $k$ elements are final, which is why the inner loop stops at $n - \text{pass}$.
- The
swappedflag lets it stop early: if a whole pass makes no swap, the list is sorted.
Insertion sort
FOR i ← 2 TO n
key ← A[i]
j ← i - 1
WHILE j >= 1 AND A[j] > key DO
A[j + 1] ← A[j] // shift right
j ← j - 1
ENDWHILE
A[j + 1] ← key // drop it in
NEXT i
- It builds a sorted section at the front, growing by one each time. Each new element is held aside as
key, larger elements are shifted right to open a gap, and the key is dropped in. - This is how most people sort a hand of playing cards, which is the analogy the exam expects.

The left is sorted, the right is untouched, and the boundary moves right
ソートアルゴリズム
隣接する要素を比較し、必要なら交換する
バブルソートのステップを踏む:各パスで最大値が末尾に浮き上がります。
バブルソートは以下の方法で動作します:
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
各パスで隣接ペアを交換し、最大要素を末尾へ"泡上げ"ます。
バブルソートの平均・最悪の時間計算量は:
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
n要素に対する2重ループにより O(n²);既に整列されている場合は早期終了により O(n) が最良ケースです。
Worked example: trace one pass
- Trace the first pass of a bubble sort on 5, 3, 8, 1.
- Compare 5 and 3: out of order, swap, giving 3, 5, 8, 1. Compare 5 and 8: in order, no swap. Compare 8 and 1: swap, giving 3, 5, 1, 8.
- After one pass the largest value, 8, is in its final position, and one more comparison per pass can be skipped from now on.
- Now trace insertion sort's third step on 3, 5, 8, 1. The key is 1. Shift 8, 5 and 3 each one place right, then place 1 at the front: 1, 3, 5, 8. Show the array after every step; that is where the marks are.
挿入ソートは以下のようにして整列結果を構築します:
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
左側に整列済みの前方部分を拡大し、大きい要素を右へシフトして各キーを正しい位置に置きます。
挿入ソートは新しい要素をどのように配置しますか?
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
キーを保管してシフトすることは、バブルソートの繰り返し隣接交換とは異なる点です。
Performance
| bubble | insertion | |
|---|---|---|
| best case | $O(n)$, one pass with no swaps | $O(n)$, already sorted, no shifts |
| average and worst | $O(n^2)$ | $O(n^2)$ |
| extra memory | $O(1)$, in place 原地 | $O(1)$, in place |
| stable | yes | yes, stable 稳定 |
- In place means it needs only a constant amount of extra memory, sorting within the array itself. Stable means two equal values keep their original relative order, which matters when a list has already been sorted by another field.
- Both reach $O(n)$ on already-sorted data, but only if bubble sort has the
swappedflag. Without it, it always performs every pass.
5, 3, 8, 1 に対するバブルソートの最初のパス後、配列はどうなりますか?カンマで区切って4つの数字を記入してください。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
5と3が交換され、5と8は交換されず、8と1が交換されます。最大値が末尾に到達したため、次のパスは1回分だけ短い比較で済みます。
Worked example: which sort, and why
- A list of 200,000 records must be sorted from scratch. Neither: both are $O(n^2)$, so a merge or quick sort at $O(n \log n)$ is needed. Say so rather than choosing the least bad.
- A sorted list of 10,000 gains 5 new records at the end and must be sorted again. Insertion sort: the data is nearly sorted, so each new key shifts only a short distance and it approaches $O(n)$.
- A teaching example must be traced by hand on paper. Bubble sort: it is the simplest to follow, which is its real remaining use.
- Justify from the state of the data and the size, not from a general preference.
各ソートの考え方をその意味と対応させてください。
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
バブルソートは隣接要素を交換し、挿入ソートは整列済みの前方部分を拡大します。どちらも最悪ケースで O(n²) です。安定性は等しいキーの順序に関するものです。
挿入ソートは、小さいまたはほぼ整列された配列で O(n) に近い性能を発揮します。なぜなら、シフトする必要がある要素が少ないからです。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
ほぼ整列されたデータでは、新しいアイテムはすでにほぼ正しい位置にあります。そのため、挿入ソートは小さな入力において高度なソート法より優れています。
バブルソートと挿入ソートの両方に当てはまるものはどれですか?すべて選択してください。
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
大きな未整列リストにおいて、O(n log n) のソートが圧倒的に勝ちます。そう述べるのが正解であり、2つの選択肢の中で最も悪い方を選ぶことではありません。
Why one pass is not the whole story
- Both sorts do repeated passes, and the exam distinguishes them by what one pass achieves and by when they stop.
- A bubble sort pass compares adjacent pairs and swaps them, so one pass carries the largest remaining item to its final place. The whole sort is $n - 1$ passes.
- An insertion sort pass takes the next item and moves it back into the already-sorted part, so after $k$ passes the first $k$ items are sorted among themselves but not yet in final position.
- Bubble sort can be improved with a flag: if a pass makes no swaps, the list is already sorted and the algorithm stops. On nearly-sorted data that turns it into one pass.
- Without the flag, both are $n^2$ in the worst case, which is why either is a poor choice for a large file and why the exam asks about small ones.
10,000レコードの整列済みリストの末尾に5件の新レコードが加わりました。再ソートに適したソート法はどれですか?
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
ほぼ整列されたデータは挿入ソートの最良ケースに相当し、O(n) に近づく。実際のライブラリはこの理由で挿入ソートに切り替える。
各ソートアルゴリズムを、1パスで達成される結果と対応させよ。
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
その差こそがトレース問題が実際にテストしていることである。バブルソートのフラグは、ほぼ整列されたデータに対して早期終了を可能にするが、これは本来挿入ソートが得意とする点でもある。
Marks that slip away
- Bubble sort compares adjacent pairs. An answer that compares an element with all the others is describing a different algorithm.
- The inner loop shortens each pass, because the end of the array is already final. Say why.
- Insertion sort shifts elements right to open a gap; it does not swap repeatedly. The distinction is the point of the algorithm.
- Both are $O(n^2)$ on average and at worst, and $O(n)$ at best. Give the case with the order.
You've got it
- bubble sort: repeated passes comparing adjacent pairs and swapping, largest bubbling to the end, inner loop shortening each pass, with a
swappedflag for early exit - insertion sort: grow a sorted section at the front, holding each key aside, shifting larger elements right and dropping the key into the gap
- both are $O(n^2)$ average and worst, $O(n)$ best, in place and stable
- insertion sort genuinely wins on small or nearly sorted lists; for a large unsorted list neither is the right answer