Lecture 08 Ticket Solution

Complete before the beginning of class on Monday, 09/19.

Download this ticket in pdf format or .tex source.

\[\def\compare{ {\mathrm{compare}} } \def\swap{ {\mathrm{swap}} } \def\sort{ {\mathrm{sort}} } \def\true{ {\mathrm{true}} } \def\false{ {\mathrm{false}} } \def\split{ {\mathrm{split}} } \def\val{ {\mathrm{val}} }\]

Give an argument that every (correct) sorting algorithm requires \(\Omega(n)\) elementary operations (e.g., \(\compare\) and \(\swap\)) to sort arrays of size \(n\). Your argument doesn’t need to be mathematically formal–an intuitive explanation is sufficient.

Hint. What can you say about an algorithm that uses \(\ll n\) operations on an array of size \(n\)?

Solution

Intuitively, any algorithm that sorts arrays must at the very least access (and potentially modify) the entire array. Since each \(\compare\) and \(\swap\) operation accesses at most two elements in the array, any sorting algorithm should require at least \(n / 2 = \Omega(n)\) such operations on some input.

Formal Argument by Contrapositive

We can make this argument more formal by constructing an explicit sequence of inputs that require \(\Omega(n)\) operations–specifically \(\swap\)s–to sort. Specifically, for each size \(n\), consider the array \(a\) of size \(n\) consisting of the values \(1\) through \(n\) in decreasing order: \(a = [n, n-1, \ldots, 2, 1]\).

Claim. Sorting \(a\) requires \(n / 2\) \(\swap\)s.

This claim may seem “obvious,” but we will formally prove it using a technique called “proof by contrapositive.” The idea is as follows. Our claim can be rephrased as,

  • If an algorithm \(A\) sorts \(a\), then \(A\) uses at least \(n / 2\) \(\swap\)s.

This statement is logically equivalent to the statement

  • If \(A\) uses fewer than \(n / 2\) \(\swap\)s, then \(A\) does not sort \(a\).

The second claim is known as the contrapositive of the first claim. Since the two original claim and its contrapositive are logically equivalent, we can prove the second claim instead of the first directly. Here is an argument.

Proof of claim. We argue by appealing to the contrapositive. Suppose \(A\) is an algorithm that performs at most \(n / 2 - 1\) \(\swap\) operations on input \(a\). Since each \(\swap\) modifies at most \(2\) values in \(a\), the total number of modified values is at most \(2 (n / 2 - 1) = n - 2\). Therefore, there are two indices \(i\) and \(j\) with \(i < j\) whose values are never modified by \(A\). By the construction of \(a\), we have \(a[i] = n - i + 1\) and \(a[j] = n - j + 1\), so that \(a[i] > a[j]\). However, this implies that \(a\) is not sorted, as \(i < j\). \(\Box\)

Intuitively, the contrapositive of the claim is easier to reason about because the hypothesis “\(A\) uses fewer than \(n / 2\) \(\swap\)s” seems like a more concrete restriction on \(A\) than the hypothesis of the original claim (“\(A\) sorts \(a\)”). For the latter hypothesis, we need to reason about all possible algorithms that sort \(a\), which seems like an impossible task. On the other hand, the hypothsis of the contrapositive gives a very clear restriction on what the algorithm \(A\) can do: it cannot even modify all of the entries of the array. Thus, all we needed to show is that in order to sort some arrays, it is necessary to modify every entry. This is precisely what our argument shows.