Suppose that we want to sort . The quick-sort algorithm is defined as follows,
- For , put the two values in order
- For , choose a random value . Put all values smaller than on the left of and all values greater on the right of . Run quick-sort on these two smaller subsets and .
Define as the number of comparisons necessary to quick-sort distinct numbers. is then a measure of the effectiveness of quick-sort.
We express as a sum of smaller random variables to make this easier. First, let’s use as an alias for the numbers (smallest number is , second to smallest is , largest is ). For , if and are directly compared. Therefore, .
All values will initially be in the same bucket. and will continue to be in the same bucket until either , , or a number between them is chosen as a pivot. They will only be compared if one of them is chosen, otherwise they get separated forever :(
Anyways, this means , so
In big- notation, this means that quick-sort is , as expected.