QUICKSORT STUDY GUIDE


Quicksort. Partition on some pivot. Quicksort to the left of the pivot. Quicksort to the right.

Partitioning. Understand exactly how to carry out 2-way partitioning as discussed in class. Be able to recognize Dijkstra's 3-way partitioning as discussed in the book.

Quicksort order of growth. Understand how to show that in the best case, quicksort is N lg N, and in the worse case is N^2. Shuffling is needed to probabilistically guarantee 2 N ln N behavior.

Quicksort compare counting. Know why the best case is ~N lg N compares and worst case is ~1/2 N^2. Despite the greater number of compares, quicksort is usually faster than mergesort. Be familiar with the fact that shuffling yields 2N ln N compares on average (but you don't need to fully digest this proof -- especially solution of the difficult recurrence relation, as that involves discrete math that is beyond the scope of the course).

Pivot choice. Understand how the pivot affects the size of the subproblems created after partitioning.

Quicksort properties. Quicksort is not stable but it is in-place (uses no more than log N memory).

Practical improvements. Cutoff to insertion sort. Using 3-way partitioning to attain faster performance for arrays with a constant number of keys.

Recommended Problems

C level

  1. Give a worst-case input for non-random quicksort that chooses the leftmost element as a pivot. Why is a best case input hard to think of?
  2. Textbook 2.3.8
  3. Textbook 2.3.11
  4. Spring 2013 midterm, #4a

B level

  1. Textbook 2.3.3
  2. Textbook 2.3.13 (don't do 2.3.20)
  3. Spring 2013 midterm, #4b

A level

  1. One strategy to speeding up quicksort is to insertion sort subarrays of size approximately 10 or smaller. An alternate approach is to simply not sort arrays of size less than 10, then use insertion sort on the entire array. Prove that this alternate approach results in a linear time sort, despite insertion sort's worst case N^2 performance.