Sorting Basics
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 be a sequence, and let a key function map each element to a totally ordered set. An ascending sorting algorithm produces satisfying both conditions below.
- is a permutation of ; the multiplicity of every element is preserved.
- .
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 and . 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 auxiliary space rather than an additional array proportional to . 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 . Below, each comparison and assignment takes time, and complexity denotes worst-case time unless stated otherwise.
4Insertion sort
Insertion sort takes for and inserts it into its proper position in the sorted prefix . 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 , contains exactly the original elements of that prefix in nondecreasing order.
- For , a one-element prefix is sorted.
- Shifting elements greater than and inserting immediately before them preserves all elements and makes sorted.
- At , 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 . In reverse order, insertion requires shifts, giving total work.
5Merge sort
Merge sort divides a sequence until every part has length at most , 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 or 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 auxiliary merge storage and is therefore not in-place.
5.2Complexity
Merging a total of elements takes time. The time complexity satisfies
The recursion tree has height , and the subproblem lengths at any one depth sum to . Hence the merge cost at each depth is at most , giving an upper bound. At every depth , subproblems of length at least have total length , giving an lower bound. Therefore, .
In a standard array implementation, a reusable merge array uses space and the call stack uses space. Thus, the total auxiliary-space complexity, including the call stack, is . An empty sequence terminates immediately.
6Lower bound for comparison sorting
There are input orders of elements with mutually distinct keys. The binary decision tree of a deterministic comparison sort needs at least leaves to distinguish these orders, so its height is at least . For , the inequality implies . Hence every deterministic comparison sort requires comparisons in the worst case. Merge sort's bound is asymptotically optimal in this model.
7Connection to binary search
Binary search assumes . 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 time and then performing each query in time can be effective.
data/lecture/information/algorithm/search/binary-search.lecture.n.md