Skip to content

Quicksort

Partition a list around a pivot in place, trace quicksort, and give its comparisons, extra space and stability, best, average and worst.

It opens on

One pass of quicksort puts the last item, 5, into its place in the sorted list, without sorting the rest. Which place, 0 to 7, is that?

Why it matters

Quicksort is the sort most standard libraries have used for arrays because it sorts in place and is fast on typical lists, and its worst case, a plain sorted list, is the standard example of why a pivot rule matters.

Good to know first

  • Selection and insertion sort: a[i], a[j] = a[j], a[i] swaps two items in place, and the comparisons of a sort are counted for the best and the worst input
  • Merge sort: T(n) = 2T(n/2) + n solves to Θ(n log n), and merge sort needs Θ(n) extra space
  • Recursion and the call stack: each call is a frame, and the depth is how many frames wait at once
  • Recurrences and divide and conquer: T(n) = T(n − 1) + n − 1 unrolls to a sum 1 + 2 + ... + (n − 1)

Step by step

  1. Partition around a pivot

    Trace the Lomuto partition on a list, say where it puts the pivot and why, and show that it makes n − 1 comparisons and no extra list.

    At j equal to 0 the item is 6, and 6 < 5 is False, so nothing moves and i stays 0. At j equal to 1 the item is 3, which is smaller, so places 0 and 1 swap, the list is 3, 6, 8, 3, 1, 7, 2, 5, and i becomes 1.

    A common mix-up

    Partitioning sorts the list around the pivot.

    In fact: It only puts the pivot in its final place and splits the others into smaller and not smaller, each side in no particular order.

  2. Quicksort on both parts

    Trace quicksort on the list of 8 by its partitions and its tree of calls, and count the comparisons it makes.

    Our first partition returned 4, so the two calls are on places 0 to 3, which hold 3, 3, 1, 2, and on places 5 to 7, which hold 7, 6, 8. The 5 is done.

    the root and its two parts

    A common mix-up

    Quicksort has to merge the two sorted parts afterwards.

    In fact: The pivot sits between them and every item on the left is smaller than every item on the right, so nothing is merged.

  3. Best, average and worst

    State quicksort's comparisons in the best, average and worst case, with its extra space and stability, and give the input that makes the last-item pivot worst.

    Quicksort is not stable. Sort the records 3a, 3b, 1, 2 by their number. The partition around 2 swaps the 1 with 3a, which puts 3b ahead of 3a, and then the final swap of the pivot with 3b gives 1, 2, 3a, 3b. The next partition is on 3a, 3b with the pivot 3b, and its final swap puts 3b first, so the result is 1, 2, 3b, 3a.

    the records

    A common mix-up

    Quicksort is always Θ(n log n).

    In fact: That is its best and its average case; with the last item as pivot a sorted, a reversed or an all-equal list takes n(n − 1)/2 comparisons, Θ(n²).

Try it

Partition 4, 9, 2, 7, 1, 5, 3 around the last item. What is the list afterwards and what does partition return?Answer

2, 1, 3, 7, 9, 5, 4 and it returns 2

Hint: The smaller items are 2 and 1, so the pivot 3 goes to place 2

Quicksort on 2, 4, 1, 3 with the last item as the pivot. How many comparisons in all?Answer

4

Hint: Partition around 3 makes 3 comparisons and leaves 2, 1 and 4; the part 2, 1 makes one more

A list of 10 equal items is sorted with this code. How many comparisons, and why?Answer

45, since nothing is smaller than the pivot, so every split is 0 and n − 1 and the comparisons are 9 + 8 + ... + 1

Hint: a[j] < pivot is False for equal items

Now i is 1 and j is 3, where the item is 3. Is it smaller than the pivot, which two places swap, and what is the list afterwards?Answer

Yes, places 1 and 3 swap, the list is 3, 3, 8, 6, 1, 7, 2, 5, and i becomes 2.

The call on 3, 3, 1, 2 takes its last item, 2, as the pivot. Which place does the 2 end in, and which items are on each side of it?Answer

Place 1, with the 1 on the left, and 3, 3 on the right.

Quicksort

30 minutes every month. No card.