Quicksort was invented in 1962 by C.A.R. Hoare. It is the fastest comparison-based sorting algorithm in practice.
Legend:
pink: greater value in current comparison
red: smaller value in current comparison
yellow: pivot element
orange: the part that is already sorted
blue: animation running
The idea is the following:
When implementing this idea it is of central importance that the partitioning step is carried out efficiently. Another interesting point is the choice of the pivot element p. For simplicity, we take the rightmost item in this implementation. Quicksort performs best if the recursion tree is as well-balanced as possible. Then the running time (in O-notation) is O(n log n). In the worst case, however, the recursion tree is entirely unbalanced, and the running time becomes quadratic. Since we always choose the rightmost element as the pivot element, the worst-case input is an array that is entirely sorted.
If we use instead an implementation where the pivot element is chosen in each round at random, the expected running time will be in O(n log n). This expected running time holds on each input (in particular on worst-case inputs)1. Observe that in this case, we talk about a randomized algorithm, where the running time depends on the random choices. There the running time is a random variable, and thus we are interested in the expected value of the running time on a given input (e.g. a best-case, or worst-case input).