Quick_Sort
Quick_Sort
Invented by Hoare
• C.A.R. Tony Hoare, at age 26, invented his algorithm in 1960 while
trying to sort words for a machine translation project from Russian to
English. Says Hoare, “My first thought on how to do this was
bubblesort and, by an amazing stroke of luck, my second thought was
Quicksort.” It is hard to disagree with his overall assessment: “I have
been very lucky. What a wonderful way to start a career in
Computing, by discovering a new sorting algorithm!” [Hoa96]. Twenty
years later, he received the TuringAward for “fundamental
contributions to the definition and design of programming
languages”; in 1980, he was also knighted for services to education
and computer science.
QuickSort
The Sorting Problem
Input : array of n numbers, unsorted
A[i]p A[i]p
• Exchange the pivot with the last element in the first (i.e.,
) subarray — the pivot is now in its final position
• Sort the two subarrays recursively
Algorithm
Best case
Quicksort Example
5 3 1 9 8 2 4 7
Analysis of Quicksort
• Best case: split in the middle — Θ(n log n)
• Worst case: sorted array! — Θ(n2)
• Average case: random arrays — Θ(n log n)
• Improvements:
• better pivot selection: median of three partitioning
• switch to insertion sort on small subfiles
• elimination of recursion
These combine to 20-25% improvement