Outline for the day:
In this lecture we will prove that any comparison-based sorting method (a method that does not use any properties of the objects being sorted except that they are from a totally ordered set) must take at least W(n log n) time. Then we will show how to break this lower bound with some non-comparison-based methods.
Always write O(T(n)) for upper bounds and W(T(n)) for lower bounds. They have very different formal definitions:
Lower bounds on complexity are generally harder to prove than upper bounds. To prove that a problem can be solved in O(T(n)) time, you only need to exhibit an algorithm for it and prove that your algorithm runs in O(T(n)) time. To prove that a problem requires at least W(T(n)) time, you need to show that any possible algorithm must take at least W(T(n)) time. This is a much more general statement. It is typically much more difficult to prove, since you do not have a concrete algorithm to work with.
A sorting method is comparison-based if the elements to be sorted are drawn from a totally ordered set and the only operations you can perform on them are comparisons x ≤ y.
A totally ordered set is a set P with an order relation ≤ defined on it such that
(see CS280). Examples: natural numbers, integers, rational numbers, real numbers.
You have already seen several examples of O(n log n)-time comparison-based sorting methods: mergesort (CS211), heapsort (last lecture). Quicksort is O(n2) in the worse case but O(n log n) on average for uniformly distributed inputs.
We can prove that any comparison-based sorting method must take at least W(n log n) time in the worse case.
Note: this does not preclude an algorithm from taking, say, O(n) on some inputs of length n; it just cannot take O(n) time on all inputs of length n. Thus the bound W(n log n) describes the worst case.
Consider the action of an arbitrary comparison-based sorting algorithm on an input array of length n. The sorting algorithm, whatever it may be, starts from some starting state and computes according to some rules. We don't need to know the nature of the rules, we just need to know that at any point in time the computation is in some state, and it changes state by performing operations.
Whenever it makes a comparison x ≤ y, the computation can take one of two possible branches, depending on whether the answer to the comparison is "yes" or "no". Thus the set of possible computations forms a computation tree whose root is the start state and which has a branch point at every test x ≤ y (it may have other branches). A path in the tree from the root to a leaf constitutes the set of states the machine goes through on some input permutation. At a leaf of the tree, which corresponds to a halting state, it must "know" what the original permutation of the input array was in the sense that there may not be two different input permutations leading to the same leaf. If two or more distinct input permutations caused the computation to take the same path in the tree, then the algorithm could not be correct, since it did not distinguish between different input permutations.
This says that the number of leaves of the tree must be at least the number of permutations of n elements, namely n!. Since the branching is at most binary, the number of nodes in the tree can at most double at each level as we go down the tree. To achieve n! leaves, the tree must be of depth at least log n! = W(n log n). Thus there must be a computation path of this length in the tree.
To show log n! is W(n log n), note
n! = n·(n-1)·(n-2)···3·2·1
≥ n·(n-1)·(n-2)···(n/2+1)
≥ (n/2)n/2
so
log n! ≥ (n/2) log (n/2)
= (n/2) log n - (n/2) log 2
≥ (n/2) log n - (n/4) log n (for sufficiently large n)
= (n/4) log n
= W(n log n).
Now that we have shown the W(n log n) lower bound for comparison-based sorting, let's break it with a non-comparison-based method. Suppose we are sorting data elements according to keys that are integers in the range [0, k-1] where k is small compared to n. Here is a method that runs in time O(n + k). The method uses three arrays A, B, and C, where A and B are the input and output arrays, respectively, and C is an auxiliary "counting array". A and B are of length n and C is of length k.
[Question: Why is this not a comparison-based method?]
The unsorted input comes in array A. C is the "counting array". We initialize all elements of C to 0. This takes time O(n + k). Below is an example with n = 25 and k = 6.
A: 3 5 5 4 0 2 3 0 4 2 5 1 5 4 5 2 0 3 1 0 0 3 4 2 5 B: - - - - - - - - - - - - - - - - - - - - - - - - - C: 0 0 0 0 0 0
[Here we show the keys of each data element only in array A.] We then make a pass through A, counting the number of elements with key i, and for each one increment the i-th element of C. This takes time O(n).
A: 3 5 5 4 0 2 3 0 4 2 5 1 5 4 5 2 0 3 1 0 0 3 4 2 5 B: - - - - - - - - - - - - - - - - - - - - - - - - - C: 5 2 4 4 4 6
Now make a pass through C, computing cumulative sums. After this step, location i of C will contain the number of elements with key i or less. This takes time O(k).
A: 3 5 5 4 0 2 3 0 4 2 5 1 5 4 5 2 0 3 1 0 0 3 4 2 5 B: - - - - - - - - - - - - - - - - - - - - - - - - - C: 5 7 11 15 19 25
Now go through C and subtract 1 from each element (actually we don't need a separate step for this, we could just roll it into the previous step).
A: 3 5 5 4 0 2 3 0 4 2 5 1 5 4 5 2 0 3 1 0 0 3 4 2 5 B: - - - - - - - - - - - - - - - - - - - - - - - - - C: 4 6 10 14 18 24
At this point, the i-th entry of C gives the position in the output array of the last data element in the input array with key i.
We now go through the array A backwards, starting from location n-1 down to 0. For each element of A, we look at its key and get the number in C at that index. This gives the position in the output array B of the data element. We store the data element there, then decrement the entry in C by 1. After the first step, we have
A: 3 5 5 4 0 2 3 0 4 2 5 1 5 4 5 2 0 3 1 0 0 3 4 25B: - - - - - - - - - - - - - - - - - - - - - - - - 5 C: 4 6 10 14 18 23
After the second step, we have
A: 3 5 5 4 0 2 3 0 4 2 5 1 5 4 5 2 0 3 1 0 0 3 42 5B: - - - - - - - - - - 2 - - - - - - - - - - - - - 5 C: 4 6 9 14 18 23
After the third step, we have
A: 3 5 5 4 0 2 3 0 4 2 5 1 5 4 5 2 0 3 1 0 0 34 2 5B: - - - - - - - - - - 2 - - - - - - - 4 - - - - - 5 C: 4 6 9 14 17 23
When we are done, we have
A:3 5 5 4 0 2 3 0 4 2 5 1 5 4 5 2 0 3 1 0 0 3 4 2 5B: 0 0 0 0 0 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 5 5 C: -1 4 6 10 14 18
This takes time O(n). The total time is O(n + k).
A sorting method is stable if elements with the same key always occur in the same order in the output array as they did in the input array.
Mergesort, quicksort, and heapsort are not stable (but can be made so). Counting sort is stable.
Stability is important in radix sort and lexicographic sort, discussed below.
Suppose we have a set of strings of equal length m over a finite alphabet of size k. Each string is of the form a0a1···am-1 where ai is an element of an ordered alphabet. They could also be m-digit numbers in k-ary notation. The leftmost letter (digit) is the most significant, the rightmost the least significant.
We sort the elements in m stages. In the first stage, we sort the elements on their least significant (rightmost) digit; then in the second stage, we sort on the next least significant digit; and so on. In the m-th stage, we sort the elements on the most significant (leftmost) digit.
In each stage, we must use a stable sorting method so that the later stages do not mess up the work we did in earlier stages.
For example, suppose we wanted to sort the strings arc, cat, tin, bat, tan, ant. There are 3 stages. The following columns show the order after each stage.
arc arc tan arc cat tin cat ant tin tan bat bat bat cat tin cat tan bat arc tan ant ant ant tin
If we use counting sort for each stage, the total time is O(m(n+k)).
This would be a good method to use for, say, sorting entries in the Manhattan telephone directory by telephone number.
We can modify radix sort to sort a list of strings over a finite alphabet of varying lengths lexicographically (dictionary order). Sort first on length, and let m be the longest length of any string. In the first stage, sort all strings of length m on their m-th digit using a stable sort. In the second stage, append all strings of length m-1 to the front of the list of strings of length m, then sort this new list of strings of length m or m-1 on their (m-1)-st digit, and so on. In the last stage, sort all strings on their first digit using a stable sort.
If we use counting sort for each stage, the total time is O(m(n+k)).
Say the key space is integers [0, k-1]. This method has fast expected time independent of k. However, its worst-case time is O(n log n).
If we assume that the keys are uniformly distributed over the possible key values, then we expect each bucket to have n/m elements. Having
significantly more is very unlikely. We roughly expect:
O(n) + mO((n/m) log (n/m)) = O(n log (n/m))
which is O(n) when m is a constant fraction of n. For example, if m = n/1000, we
have m buckets, and we would have expected time O(n log 1000) = O(n). Note that this does not depend on k.