markdown
Sorting Basicsmd 03b4311
lecture/information/algorithm/foundation/sorting-basics.lecture.n.md
Download PDF

Sorting Basics

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

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
タブを全て閉じる