Tip:
Highlight text to annotate it
X
Quicksort in 60 seconds.
Quicksort's a sorting algorithm that works by dividing and conquering the data, a strategy
also favoured by Roman emperor Julius Caesar, who was not a
very good programmer. The Romans, having no "0", could only represent the numbers 1, 3,
7, 15, 31, and 63 in Binary, which put them at a serious disadvantage when it came to
general computing.
First we look at the array that's been passed in.
If there aren't any elements in the array, it's sorted. We're done. Return it.
If there's only one element in the array, it's sorted. We're done. Return it.
Select an element from the array. Any element you want. At random, it doesn't matter which
one you pick.
We're going to call this element the PIVOT, named after the Pivot step in the Electric
Slide, a line dance that is considered a crucial part of modern Computing Science.
We iterate through the array, taking every element of the array that's less than the
pivot and putting it to the left of the pivot, then taking every element that's greater than
the pivot and putting it to the right of the pivot.
Then, we call a sorting algorithm on all of the elements to the left of the pivot, and
we call a sorting algorithm on all of the elements to the right of the pivot. Maybe
you think it's unfair to use a sorting algorithm inside our sorting algorithm - but here's
the rub: we're using quicksort, so it's cool.
Then we're done. That's quicksort.