Let P be a quicksort program that sorts numbers in ascending order using the…
2024
Let P be a quicksort program that sorts numbers in ascending order using the first element as the pivot. Let t1 and t2 be the number of comparisons made by P for the inputs [1, 2, 3, 4, 5] and [4, 1, 5, 3, 2] respectively. Which one of the following holds?
- A.
t1 = 5
- B.
t1 < t2
- C.
t1 > t2
- D.
t1 = t2
Attempted by 7 students.
Show answer & explanation
Correct answer: C
Concept:
Quicksort partitions an array by comparing a chosen pivot against every other element in that sub-array once, then recurses separately on the two resulting parts. When the pivot is always the first element, the total comparison count depends entirely on how balanced each split turns out to be: a pivot that is always the extreme (smallest or largest) value of its sub-array forces the most skewed possible split at every step -- the O(n^2) worst case -- while a pivot nearer the middle of the values gives more balanced splits and fewer total comparisons.
Application -- tracing t1 for [1, 2, 3, 4, 5]:
Pivot = 1 (first element). Compare it with 2, 3, 4, 5 -> 4 comparisons. Since 1 is the smallest value, every other element lands on one side, leaving a sub-array of size 4 ([2, 3, 4, 5]).
Pivot = 2. Compare it with 3, 4, 5 -> 3 comparisons. Again the pivot is the smallest of its sub-array, leaving a size-3 sub-array ([3, 4, 5]).
Pivot = 3. Compare it with 4, 5 -> 2 comparisons, leaving a size-2 sub-array ([4, 5]).
Pivot = 4. Compare it with 5 -> 1 comparison. The remaining sub-array has size 1, so recursion stops.
Adding the comparisons from every level gives t1 = 4 + 3 + 2 + 1.
Application -- tracing t2 for [4, 1, 5, 3, 2]:
Pivot = 4 (first element). Compare it with 1, 5, 3, 2 -> 4 comparisons. The values less than 4 (1, 3, 2) form a sub-array of size 3, and the single value greater than 4 (5) forms a sub-array of size 1 that is already sorted, so it needs no further comparisons.
Pivot = 1 (first element of the size-3 sub-array). Compare it with the remaining two values -> 2 comparisons. Since 1 is again the smallest, the remaining sub-array has size 2.
Pivot = the first element of that size-2 sub-array. Compare it with the one remaining value -> 1 comparison. Whichever side that value lands on, it forms a sub-array of size 1, which needs no further comparisons since a single element is already sorted, so recursion stops.
Adding the comparisons from every level gives t2 = 4 + 2 + 1.
Cross-check:
For a fully sorted ascending input with a first-element pivot, every single split is maximally skewed, so the total comparisons follow the closed form (n-1) + (n-2) + ... + 1 = n(n-1)/2; for n = 5 this independently gives the same total obtained by the level-by-level trace of t1 above. The second input is not sorted, so at least one split (the very first one, which separates off an already-sorted size-1 sub-array) is less skewed than the worst case, meaning its total must come out strictly below that same n(n-1)/2 ceiling -- consistent with the level-by-level trace of t2 above.
Since the sorted input forces the worst-case (maximally skewed) split at every step while the unsorted input does not, t1 is strictly greater than t2, i.e., t1 > t2.
Important point: the partitioning step in the trace above assumes the common convention where elements smaller than the pivot keep their original relative order (as in a standard left-to-right / Lomuto-style scan); a different valid partitioning convention could make a different element the next pivot inside the size-3 sub-array for t2, changing that exact count by 1 (e.g., 6 instead of 7). Either way, the overall relation t1 > t2 still holds, since the cross-check above shows t1 sits exactly at the maximum possible total (n(n-1)/2) while t2 must fall strictly below that same ceiling for any valid partitioning convention. More generally: a first-element (or last-element) pivot always hits its worst case on an already sorted (or reverse-sorted) input, regardless of implementation.