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. Show the results after the first partitioning during 3-way quicksort on the following.
    S W I M P E A T S F R I E S
    Answers
  5. Consider using 2-way quicksort to sort the numbers: 3, 4, 5, 6, 7, 8 and 9. After a random shuffle, we have the following sequence: 5, 6, 8, 3, 9, 4, 7. Show the result of the first call to partition() by giving contents of the array after each exchange.
    Answers
  6. Fall 2012 midterm, #3

B level

  1. Textbook 2.3.3
  2. Textbook 2.3.13 (don't do 2.3.20)
  3. Give a very short proof in plain English that 3-way quicksort always completes in linear time for an array with N items and 7 distinct keys.
    Answers
  4. For each of the algorithms listed in the analysis guide (Question 4 of B level), pick which of those applies to an algorithm listed below: Answers

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.