Overview of Quicksort
Like merge sort, quicksort uses divide-and-conquer, and so it's a recursive algorithm. The way that quicksort uses divide-and-conquer is a little different from how merge sort does. In merge sort, the divide step does hardly anything, and all the real work happens in the combine step. Quicksort is the opposite: all the real work happens in the divide step. In fact, the combine step in quicksort does absolutely nothing.
Quicksort has a couple of other differences from merge sort. Quicksort works in place. And its worst-case running time is as bad as selection sort's and insertion sort's:
Here is how quicksort uses divide-and-conquer. As with merge sort, think of sorting a subarray
Divide by choosing any element in the subarray
. Call this element the pivot. Rearrange the elements in so that all other elements in that are less than or equal to the pivot are to its left and all elements in are to the pivot's right. We call this procedure partitioning. At this point, it doesn't matter what order the elements to the left of the pivot are in relative to each other, and the same holds for the elements to the right of the pivot. We just care that each element is somewhere on the correct side of the pivot.
As a matter of practice, we'll always choose the rightmost element in the subarray,, as the pivot. So, for example, if the subarray consists of , then we choose as the pivot. After partitioning, the subarray might look like . Let be the index of where the pivot ends up. Conquer by recursively sorting the subarrays
(all elements to the left of the pivot, which must be less than or equal to the pivot) and (all elements to the right of the pivot, which must be greater than the pivot). Combine by doing nothing. Once the conquer step recursively sorts, we are done. Why? All elements to the left of the pivot, in
, are less than or equal to the pivot and are sorted, and all elements to the right of the pivot, in , are greater than the pivot and are sorted. The elements in can't help but be sorted!
Think about our example. After recursively sorting the subarrays to the left and right of the pivot, the subarray to the left of the pivot is, and the subarray to the right of the pivot is . So the subarray has , followed by , followed by . The subarray is sorted.
The base cases are subarrays of fewer than two elements, just as in merge sort. In merge sort, you never see a subarray with no elements, but you can in quicksort, if the other elements in the subarray are all less than the pivot or all greater than the pivot.
Let's go back to the conquer step and walk through the recursive sorting of the subarrays. After the first partition, we have have subarrays of
To sort the subarray
To sort the subarray
Here is how the entire quicksort algorithm unfolds. Array locations in blue have been pivots in previous recursive calls, and so the values in these locations will not be examined or moved again:
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy