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
- 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?
- Textbook 2.3.8
- Textbook 2.3.11
-
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
I M P E A E F R I S S S T W
-
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
5 6 8 3 9 4 7
5 4 8 3 9 6 7
5 4 3 8 9 6 7
3 4 5 8 9 6 7
- Fall 2012 midterm, #3
B level
- Textbook 2.3.3
- Textbook 2.3.13 (don't do 2.3.20)
-
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
Must partition only 7 times total. Each partition takes linear time.
-
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:
- Mergesort:
- Binary search in a sorted array:
- Quicksort (if partitioning always divides the array in half):
Answers
Mergesort: Algorithm 3
Binary search in a sorted array: Algorithm 2
Quicksort (if partitioning always divides the array in half): Algorithm 3
A level
- 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.