markdown
整列の基本md 03b4311
lecture/information/algorithm/foundation/sorting-basics.lecture.n.md
Download PDF

整列せいれつ基本きほん

date2026-07-14document_iddoc_b23650bdaa56cb7caec56fbb7d34c2a6description整列の仕様、安定性、原位置性、比較モデルを定義し、挿入整列と併合整列の正当性および計算量を説明する。prerequisites計算量の基本 / 再帰の基本 / 配列の基本操作type講義statusactiverelateddata/lecture/information/algorithm/foundation/recursion-basics.lecture.n.md / data/lecture/information/algorithm/search/binary-search.lecture.n.md
algorithmfoundationundergraduatelecture
data/lecture/information/algorithm/foundation/recursion-basics.lecture.n.md

1導入どうにゅう

この講義こうぎでは、整列せいれつ仕様しよう評価基準ひょうかきじゅん定義ていぎし、挿入整列そうにゅうせいれつ併合整列へいごうせいれつ正当性せいとうせいおよび計算量けいさんりょう説明せつめいする。整列済せいれつずみの配列はいれつは、後続こうぞくする二分探索にぶんたんさく前提ぜんていとなる。

2整列せいれつ仕様しよう

要素列ようそれつ A=(a0,,an-1) と、要素ようそかぎ全順序集合ぜんじゅんじょしゅうごう対応たいおうさせる関数かんすう kかんがえる。昇順整列しょうじゅんせいれつは、出力列しゅつりょくれつ B=(b0,,bn-1)つぎ二条件にじょうけんたす算法さんぽうである。

  1. BA置換ちかんである。すなわち、各要素かくようそ個数こすう保存ほぞんされる。
  2. k(b0)k(b1)k(bn-1)成立せいりつする。

第一条件だいいちじょうけん要素ようそ欠落けつらく重複生成ちょうふくせいせい禁止きんしし、第二条件だいにじょうけん出力順序しゅつりょくじゅんじょ規定きていする。

3評価基準ひょうかきじゅん

3.1安定性あんていせい

安定あんてい整列せいれつは、i<j かつ k(ai)=k(aj) である二要素にようそ相対順序そうたいじゅんじょ出力しゅつりょくでも保存ほぞんする。複数ふくすうかぎ段階的だんかいてき整列せいれつする場合ばあい、この性質せいしつによってさき整列結果せいれつけっか維持いじできる。

3.2原位置性げんいちせい

入力長にゅうりょくちょう n比例ひれいする別配列べつはいれつ使用しようせず、追加領域ついかりょういき O(1)入力配列内にゅうりょくはいれつない更新こうしんする整列せいれつ原位置整列げんいちせいれつという。再帰呼出さいきよびだしの記憶領域きおくりょういきふくめるかは解析かいせき明示めいじする。

3.3比較ひかくモデル

比較整列ひかくせいれつでは、かぎかんする情報じょうほうk(x)k(y) などの比較結果ひかくけっかからだけ取得しゅとくする。以下いかでは一回いっかい比較ひかく代入だいにゅうO(1) とし、最悪時さいあくじ時間計算量じかんけいさんりょう評価ひょうかする。

4挿入整列そうにゅうせいれつ

挿入整列そうにゅうせいれつは、i=1,2,,n-1じゅんaiし、整列済せいれつずみの接頭辞せっとうじ A[0,i)適切てきせつ位置いち挿入そうにゅうする。かぎ挿入要素そうにゅうようそよりおおきい要素ようそだけをみぎ移動いどうする。

4.1正当性せいとうせい

反復開始時はんぷくかいしじに「A[0,i) は、もとA[0,i)要素ようそ昇順しょうじゅん整列せいれつしたれつである」を不変条件ふへんじょうけんとする。

  • i=1 では、一要素いちようそ接頭辞せっとうじ整列済せいれつずみである。
  • ai よりおおきい要素ようそみぎ移動いどうし、その直前ちょくぜんai挿入そうにゅうすると、要素ようそ保存ほぞんしたまま A[0,i+1)昇順しょうじゅんになる。
  • i=n では接頭辞せっとうじ配列全体はいれつぜんたい一致いっちする。

したがって、挿入整列そうにゅうせいれつ整列せいれつ仕様しようたす。同一鍵どういつかぎ要素ようそさないため安定あんていであり、一時変数いちじへんすうだけで実装じっそうできるため原位置げんいちである。

4.2計算量けいさんりょう

すで整列済せいれつずみなら各反復かくはんぷく比較回数ひかくかいすう定数ていすうであり、時間計算量じかんけいさんりょうΘ(n) である。逆順ぎゃくじゅんでは i 番目ばんめ挿入そうにゅうi かい移動いどうようするため、合計ごうけいi=1n-1i=Θ(n2) となる。

5併合整列へいごうせいれつ

併合整列へいごうせいれつは、れつながさが高々たかだか 1 になるまで二分にぶんし、各部分列かくぶぶんれつ再帰的さいきてき整列せいれつしたあとふたつの整列済せいれつずれつ併合へいごうする。併合へいごうでは両列りょうれつ先頭せんとう比較ひかくし、ちいさいほう出力しゅつりょくうつす。

5.1正当性せいとうせい

併合へいごうでは、「出力済しゅつりょくずみのれつ昇順しょうじゅんであり、両入力列りょうにゅうりょくれつからのぞいた要素ようそ過不足かふそくなくふくむ」を不変条件ふへんじょうけんとする。両先頭りょうせんとうちいさいほう未出力要素全体みしゅつりょくようそぜんたい最小要素さいしょうようそであるため、この要素ようそ追加ついかしても不変条件ふへんじょうけん保存ほぞんされる。終了時しゅうりょうじには全要素ぜんようそ出力しゅつりょくされる。

列長れつちょうかんする帰納法きのうほう適用てきようすると、なが0 または 1れつ自明じめい整列済せいれつずみであり、みじか二列にれつ再帰結果さいきけっかただしい併合へいごうからもとれつただしく整列せいれつされる。したがって算法全体さんぽうぜんたい正当せいとうである。

同一鍵どういつかぎでは左側ひだりがわ要素ようそさき出力しゅつりょくすれば安定性あんていせい保持ほじできる。標準的ひょうじゅんてき配列実装はいれつじっそう併合用へいごうようO(n) 領域りょういき使用しようするため、原位置げんいちではない。

5.2計算量けいさんりょう

ながn併合へいごうΘ(n) 時間じかんようする。時間計算量じかんけいさんりょう T(n)

T(0)=T(1)=Θ(1),T(n)=T(n/2)+T(n/2)+Θ(n)(n2)

たす。再帰木さいきぎたかさは log2n であり、同一深度どういつしんどにある部分列長ぶぶんれつちょう合計ごうけいn である。したがって各深度かくしんど併合費用へいごうひよう高々たかだか O(n) であり、上界じょうかいO(nlogn) となる。また、0d<log2n各深度かくしんどではなが2 以上いじょう部分列ぶぶんれつ合計長ごうけいちょうΘ(n) であるため、下界げかいΩ(nlogn) となる。よって、T(n)=Θ(nlogn) である。

標準的ひょうじゅんてき配列実装はいれつじっそうでは、再利用さいりようする併合用配列へいごうようはいれつΘ(n)呼出よびだしスタックが Θ(log(n+1))領域りょういき使用しようする。したがって、呼出よびだしスタックをふく総補助空間計算量そうほじょくうかんけいさんりょうΘ(n) である。空列くうれつただちに終了しゅうりょうする。

6比較整列ひかくせいれつ下界かかい

たがいにことなるかぎをもつ n 要素ようそには n! とおりの入力順序にゅうりょくじゅんじょがある。決定的比較整列けっていてきひかくせいれつ二分決定木にぶんけっていぎは、これらを識別しきべつするためにすくなくとも n! まい必要ひつようとし、そのたかさはすくなくとも log2(n!) である。n2 では n!(n/2)n/2 であるため、log2(n!)=Ω(nlogn)成立せいりつする。したがって、任意にんい決定的比較整列けっていてきひかくせいれつ最悪時さいあくじΩ(nlogn) かい比較ひかくようする。併合整列へいごうせいれつΘ(nlogn) はこの比較ひかくモデルで漸近的ぜんきんてき最適さいてきである。

7二分探索にぶんたんさくへの接続せつぞく

二分探索にぶんたんさくは、A0A1An-1成立せいりつする配列はいれつ前提ぜんていとする。未整列みせいれつ入力にゅうりょく一度いちどだけ探索たんさくするなら、整列せいれつ費用ひよう利益りえき上回うわまわ場合ばあいがある。おな配列はいれつ反復探索はんぷくたんさくするなら、最初さいしょO(nlogn)整列せいれつし、そのあと各探索かくたんさくO(logn)実行じっこうする構成こうせい有効ゆうこうとなる。

data/lecture/information/algorithm/search/binary-search.lecture.n.md

Sorting Basics

data/lecture/information/algorithm/foundation/recursion-basics.lecture.n.md

1Introduction

This lecture defines the specification and evaluation criteria of sorting and explains the correctness and complexity of insertion sort and merge sort. A sorted array is a prerequisite for the subsequent binary-search lecture.

2Sorting specification

Let A=(a0,,an-1) be a sequence, and let a key function k map each element to a totally ordered set. An ascending sorting algorithm produces B=(b0,,bn-1) satisfying both conditions below.

  1. B is a permutation of A; the multiplicity of every element is preserved.
  2. k(b0)k(b1)k(bn-1).

The first condition prohibits lost or duplicated elements, and the second specifies the output order.

3Evaluation criteria

3.1Stability

A stable sort preserves the relative output order of any two elements satisfying i<j and k(ai)=k(aj). This property preserves the result of an earlier sort when sorting by several keys in stages.

3.2In-place operation

An in-place sort updates the input array using O(1) auxiliary space rather than an additional array proportional to n. An analysis must state whether it includes recursive call-stack space.

3.3Comparison model

A comparison sort obtains information about keys only through results such as k(x)k(y). Below, each comparison and assignment takes O(1) time, and complexity denotes worst-case time unless stated otherwise.

4Insertion sort

Insertion sort takes ai for i=1,2,,n-1 and inserts it into its proper position in the sorted prefix A[0,i). It shifts right only elements whose keys are greater than the inserted key.

4.1Correctness

Use the following loop invariant: at the start of iteration i, A[0,i) contains exactly the original elements of that prefix in nondecreasing order.

  • For i=1, a one-element prefix is sorted.
  • Shifting elements greater than ai and inserting ai immediately before them preserves all elements and makes A[0,i+1) sorted.
  • At i=n, the prefix is the entire array.

Thus insertion sort satisfies the sorting specification. It is stable because equal-key elements do not pass each other, and it is in-place because it requires only a temporary variable.

4.2Complexity

For an already sorted input, every iteration performs a constant number of comparisons, so the time complexity is Θ(n). In reverse order, insertion i requires i shifts, giving i=1n-1i=Θ(n2) total work.

5Merge sort

Merge sort divides a sequence until every part has length at most 1, recursively sorts the parts, and merges two sorted sequences. A merge compares their first remaining elements and transfers the smaller one to the output.

5.1Correctness

For merging, maintain the invariant that the produced sequence is sorted and contains exactly the elements removed from the two inputs. The smaller of the two first remaining elements is a minimum among all remaining elements, so appending it preserves the invariant. At termination, every element has been produced.

Induction on sequence length now proves the whole algorithm correct. Sequences of length 0 or 1 are trivially sorted. The inductive hypothesis gives two correctly sorted shorter sequences, and the correct merge produces a sorted permutation of the original sequence.

If equal keys are resolved by taking the left element first, merge sort is stable. A standard array implementation uses O(n) auxiliary merge storage and is therefore not in-place.

5.2Complexity

Merging a total of n elements takes Θ(n) time. The time complexity T(n) satisfies

T(0)=T(1)=Θ(1),T(n)=T(n/2)+T(n/2)+Θ(n)(n2).

The recursion tree has height log2n, and the subproblem lengths at any one depth sum to n. Hence the merge cost at each depth is at most O(n), giving an O(nlogn) upper bound. At every depth 0d<log2n, subproblems of length at least 2 have total length Θ(n), giving an Ω(nlogn) lower bound. Therefore, T(n)=Θ(nlogn).

In a standard array implementation, a reusable merge array uses Θ(n) space and the call stack uses Θ(log(n+1)) space. Thus, the total auxiliary-space complexity, including the call stack, is Θ(n). An empty sequence terminates immediately.

6Lower bound for comparison sorting

There are n! input orders of n elements with mutually distinct keys. The binary decision tree of a deterministic comparison sort needs at least n! leaves to distinguish these orders, so its height is at least log2(n!). For n2, the inequality n!(n/2)n/2 implies log2(n!)=Ω(nlogn). Hence every deterministic comparison sort requires Ω(nlogn) comparisons in the worst case. Merge sort's Θ(nlogn) bound is asymptotically optimal in this model.

7Connection to binary search

Binary search assumes A0A1An-1. For a single query on unsorted input, the cost of sorting may exceed its benefit. For repeated queries on the same array, sorting once in O(nlogn) time and then performing each query in O(logn) time can be effective.

data/lecture/information/algorithm/search/binary-search.lecture.n.md
raw .n.md をコピー
loc をコピー (filepath:line ~ line)
copy share link
copy encoded share link
path をコピー
copy share link
copy encoded share link
copy share link
copy encoded share link
タブを全て閉じる