Sorting algorithms
導入済み
15:34
探索 & ソート
百万人の名前が入った電話帳があります。一つずつ確認していくと、百万回の比較が必要になるかもしれません。しかし、すでにそのコツはご存知でしょう:…で開いてみてください。
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| sort/sɔːt/ | To arrange items in a list into a chosen order. |
| library/ˈlaɪbrəri/ | A collection of reusable routines that programs can call. |
| algorithm/ˈælɡərɪθəm/ | A finite sequence of precise steps for solving a problem or carrying out a task. |
| bubble sort/ˈbʌbl sɔːt/ | バブルソート |
| insertion sort/ɪnˈsɜːʃn sɔːt/ | 挿入ソート |
| list/lɪst/ | A collection of items arranged in a particular order. |
| trace/treɪs/ | To follow an algorithm step by step, recording how its data changes. |
| array/əˈreɪ/ | An indexed collection of elements arranged in one or more dimensions. |
| pass/pæs/ | One complete run through a part of the list during sorting. |
| swap/swɒp/ | To exchange two values so that each takes the other's place. |
| elements/ˈelɪmənts/ | Individual items in a collection or data structure. |
| inner loop/ˈɪnə luːp/ | A loop inside another loop that repeats steps during each pass. |
| flag/flæɡ/ | A stored indicator of a condition or state, often represented by a Boolean value or a single bit. |
| sorted section | The part of a list whose items are already in order. |
| key/kiː/ | The item held aside to be placed into the sorted section. |
| best case | The input that allows an algorithm to complete with the least work. |
| extra memory | Memory used by an algorithm in addition to its input data. |
| in place/ɪn pleɪs/ | インプレイス |
| stable/ˈsteɪbl/ | 安定 |
| field/fiːld/ | A named component of a record, with its own value and type. |
| records | Groups of related data stored together as a single item. |
| linear time | Running time that grows in proportion to the number of items. |
意図的に遅いソート
- 本格的なライブラリは、試験で書かせることのないアルゴリズムを用いてソートを行います。バブルソートと挿入ソートはどちらも$O(n^2)$であり、大規模なリストではどの善きソートにも負けます。
- これらはカリキュラムに含まれていますが、明確な理由があります:短 enough で手動でのトレースが可能であり、トレースすることでソートが実際に配列に対して何をするかを学ぶことができます。
- 挿入ソートにも現実的な利点があります。リストが小規模であるか、すでにほぼ並べ替えられている場合、それは本当に最速であり、実際のライブラリはこれらの場合にこれを切り替えます。
- この授業はバブルソートと挿入ソートです:アルゴリズム、その振る舞い、そしてそれぞれが勝つ場面について解説します。
バブルソート
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
- 各パスで隣り合うペアを比較し、順序が逆であるものを交換するため、残っている最大の値が「泡」のように最後へ浮上します。
- パス$k$終了後、最後の$k$要素は確定しているため、内側ループは$n - \text{pass}$で停止します。
swappedフラグにより早期停止が可能です:一連のパスで交換が行われなければ、リストは既に並べ替え済みです。
挿入ソート
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
- 先頭に整列されたセクションを構築 し、毎回1つずつ拡大する。新しい要素は
keyとして一時的に保管され、大きい要素は右へシフトして隙間を作り、キーを挿入する。 - これは多くの人がカードの手牌をソートする方法であり、試験で想定される例え話です。

左側は並べ替え済み、右側は未変化であり、境界が右へ移動します。


ソートアルゴリズム
compare adjacent, swap if needed
Step through a bubble sort: each pass floats the largest value to the end.
Bubble sort works by:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Each pass swaps adjacent pairs, "bubbling" the largest element to the end.
The average/worst-case time complexity of bubble sort is:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Two nested loops over n elements give O(n²); the best case (already sorted) is O(n) with the early exit.
worked example: 一度のパスをトレース
- 5, 3, 8, 1 に対するバブルソートの最初のパスをトレースします。
- 5 と 3 を比較:順序が逆なので交換し、3, 5, 8, 1 にします。5 と 8 を比較:順序が正しいので交換しない。8 と 1 を比較:交換し、3, 5, 1, 8 にします。
- 一度のパス後、最大の値 8 は最終位置にあり、これ以降パスあたりさらに1回の比較を省略できます。
- 次に 3, 5, 8, 1 に対する挿入ソートの第3ステップをトレースします。 キーは 1 です。8, 5, 3 をそれぞれ1つ右へシフトし、前に 1 を配置:1, 3, 5, 8。各ステップ後の配列を示してください。そこに得点があります。

Insertion sort builds the sorted result by:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
It grows a sorted prefix on the left, shifting larger elements right to drop each key into place.
How does insertion sort place each new element?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Holding the key aside and shifting is what distinguishes it from bubble sort's repeated adjacent swaps.
パフォーマンス
| バブルソート | 挿入ソート | |
|---|---|---|
| 最良ケース | $O(n)$、交换なしの1パス | $O(n)$、既に並べ替え済みでシフトなし |
| 平均・最悪ケース | $O(n^2)$ | $O(n^2)$ |
| 追加メモリ | $O(1)$、インプレイース | $O(1)$、インプレイース |
| 安定性 | はい | はい、安定 |
- インプレイースとは、定数程度の追加メモリしか必要せず、配列内でソートすることです。安定とは、等しい値が元の相対順序を保つことを指し、他のフィールドで既にソートされたリストにおいて重要です。
- どちらも並べ替え済みデータで$O(n)$に達しますが、バブルソートに
swappedフラグがある場合に限ります。ない場合、常にすべてのパスを実行します。

After the first pass of a bubble sort on 5, 3, 8, 1, what is the array? Write the four numbers separated by commas.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
5 and 3 swap, 5 and 8 do not, 8 and 1 swap. The largest value has reached the end, so the next pass can be one comparison shorter.
解き方例:どのソートか、そしてその理由
- 200,000レコードのリストを初めからソートする必要があります。 どちらでもなく:どちらも$O(n^2)$なので、$O(n \log n)$のmerge sortまたはquick sortが必要です。そう述べてください。最も悪い方を選ばないでください。
- 10,000の並べ替え済みリストに末尾に5つの新レコードが加わり、再度ソートする必要があります。 挿入ソート:データがほぼ並べ替え済みなので、各新しいキーは短い距離だけシフトされ、$O(n)$に近づきます。
- 紙の上で手動トレースが必要な教学例です。 バブルソート:追跡するのが最も単純であり、それが現在の主な用途です。
- 一般的な好みではなく、データの状態とサイズに基づいて正当化してください。

Match each sorting idea to what it means.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Bubble swaps neighbours, insertion grows a sorted prefix; both are O(n²) worst-case; stability is about equal-key order.
Insertion sort runs close to O(n) on small or nearly-sorted arrays, because few elements need to be shifted.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
On nearly-sorted data each new item is already almost in place — which is why insertion sort beats fancier sorts on small inputs.
Which are true of both bubble sort and insertion sort? Select すべて that apply.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
On a large unsorted list an O(n log n) sort wins decisively. Saying so is the right answer, not choosing the least bad of the two.
このカードは修正されたため、翻訳が更新されるまで英語で表示されます。
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. This can reduce the number of passes on nearly-sorted data.
- 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.
A sorted list of 10,000 records gains 5 new records at the end. Which sort suits re-sorting it?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Nearly sorted data is exactly insertion sort's best case, approaching O(n). Real libraries switch to it for this reason.
Match each sort to what one pass achieves.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
That difference is what a trace question is really testing. A bubble-sort flag also lets it stop early on nearly-sorted data, which insertion sort handles well anyway.
見落としやすいポイント
- バブルソートは隣り合うペアを比較します。ある要素を他全てと比較する答えは別のアルゴリズムを説明しています。
- 内側ループは各パスで短縮されます。配列の末端が既に確定しているためです。その理由を述べよ。
- 挿入ソートでは要素を右へシフトして隙間を作ります。 repeatedly交換するものではありません。この違いこそがアルゴリズムの核心です。
- 両方とも平均および最悪の場合に $O(n^2)$、最良の場合に $O(n)$ です。順序を含むケースを答えよ。
理解できましたか
- バブルソート: 反復するパスで隣接するペアを比較・交換し、最大値を末尾へ浮き上がらせます。内側ループは各パスで短縮され、早期終了用の
swappedフラグがあります。 - 挿入ソート: 先頭に並べ替え済みセクションを成長させます。キーを取り出し、大きい要素を右へシフトして隙間を作り、そこにキーを落とします。
- 両方とも平均および最悪で $O(n^2)$、最良で $O(n)$、インプレイスかつ安定です。
- 挿入ソートは小さいまたはほぼ並んでいるリストで実際に勝ちます。大きな未整列リストに対してはどちらも正解ではありません。
何が問題でしたか?この内容はサイトの運営チームだけが確認します。