Comparison model
The algorithm learns key order only through comparisons. Lower bounds in this model do not automatically apply to algorithms that inspect digits or exploit a bounded key range.
REFERENCE SHEET
Use ordering to narrow possibilities, and distinguish algorithm cost from unavoidable cost.
Before this Asymptotics & recurrences, Recursion, invariants & termination
The algorithm learns key order only through comparisons. Lower bounds in this model do not automatically apply to algorithms that inspect digits or exploit a bounded key range.
Equal-key records retain their original relative order.
Maintain an interval of possible positions in a sorted random-access sequence; each comparison discards about half the remaining candidates.
A deterministic comparison sort of n distinct keys needs at least n! leaves, one per possible ordering, hence worst-case depth at least ⌈log₂(n!)⌉.
Merging two sorted runs takes linear time; the balanced recurrence gives Θ(n log n) worst-case time. Standard array mergesort uses O(n) auxiliary storage.
Sorted-array membership uses O(log n) comparisons and O(1) extra space in an iterative implementation. The maintained interval must shrink on every unsuccessful iteration.
Sorted keys support membership, boundaries, and range queries.
Stable sorting lets a later primary-key sort preserve a previous secondary-key order within equal primary keys.
01 / Trace
When a[mid]≥6, move hi to mid; otherwise move lo to mid+1.
Start [0,5): mid=2 so hi=2. Then mid=1 and a[1]=3, so lo=2. The interval is empty at 2, the desired boundary.
02 / Reason
Identify the information its operations use.
Counting sort indexes an array using key values rather than learning order only from comparisons. Its O(n+k) time uses a different model and depends on the key range.
Further reading and full-length treatments: