Assg 05: Quicksort
COSC 2336 Data Structures
Objectives
• Practice writing functions
• Practice writing recursive functions.
• Learn about Analysis of algorithms and O(n log n) sorts
Description
In this assignment we will be implementing one of the most popular sorting
algorithms used in libraries (like the C++ STL library, and the UNIX qsort
function) to provide basic sorting abilities, the Quicksort algorithm. I would
recommend that you at least read section 7.5 from our supplemental Shaffer
textbook on Quicksort, if not sections 7.1-7.5 talking about three well known
O(n log n) sorting algorithms, and the 3 O(n2) algorithms we discussed last
week.
Quicksort, when properly implemented, is very attractive because it pro-
vides a way to do a fast sort completely in-place (without having to allocate
additional memory to do the sort, beyond a single value needed when swap-
ping two values in the list being sorted). In the worst case, Quicksort is
actually O(n2), no better than bubble sort. But this worse case only occurs
when every pivot selected is the wort possible, and does not divide the list
at all. This is very unlikely to happen, unless you know how the pivot is
selected, and specifically design the input list to always choose the worst
possible pivot. On average the cost of Quicksort is O(n log n), and it is
usually very likely that average case performance will result when lists to be
sorted are relatively random.
The most direct implementation of Quicksort is as a recursive algorithm.
Quicksort is an example of a divide and conquer approach to solving the
problem of sorting the list. We are given a list of items, A and indexes left
1
and right that indicate a sub-portion of the list to be sorted. left and right
indicate the actual indexes, so if the list is a regular C array of integers, and
the array is of size 10
int left;
int right;
const inst SIZE = 10;
int A[size];
Then to sort the whole list we set left = 0 and right = 9 to initially
call the Quicksort function:
left = 0;
right = size-1;
quicksort(A, left, right);
Conceptually the steps of the Quicksort algorithm are as follows:
1. if list size is 0 or 1 (left <= right) return (lists of this size are sorted
by definition).
2. Choose a pivot value and swap the pivot value to the end of the list
swap(pivotIndex, right)
3. Partition the list. Partitioning means all values less than the pivot
value should end up on the left of the list, and all values greater will
be on the right. The first index k where a value >= to the pivot value
is at indicates the new left and right side sub-lists.
4. Swap the pivot value to its correct position k swap(k, right)
5. Recursively call Quicksort on the new left and right sub-lists
• quicksort(A, left, k-1)
• quicksort(A, k+1, right)
Most of the real work happens in the function/code to partition the list.
The partitioning of the list, for Quicksort to be an in-place sort, must work
by swapping values in-place in the list o.