Quick Sort
Quicksort, or partitionexchange sort, is a sorting algorithm developed by Tony Hoare that, on average, makes O(n log n) comparisons to sort n items. In the worst case, it makes O(n^{2}) comparisons, though this behavior is rare. Quicksort is often faster in practice than other O(n log n) algorithms. Additionally, quicksort’s sequential and localized memory references work well with a cache. Quicksort is a comparison sort and, in efficient implementations, is not a stable sort. Quicksort can be implemented with an inplace partitioning algorithm, so the entire sort can be done with only O(log n) additional space used by the stack during the recursion.
Visualization of the quicksort algorithm. The horizontal lines are pivot values.


Class  Sorting algorithm 

Worst case performance  O(n^{2}) 
Best case performance  O(n log n) (simple partition) or O(n) (threeway partition and equal keys) 
Average case performance  O(n log n) 
Worst case space complexity  O(n) auxiliary (naive) O(log n) auxiliary 
Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller subarrays: the low elements and the high elements. Quicksort can then recursively sort the subarrays.
The steps are:
 Pick an element, called a pivot, from the array.
 Reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
 Recursively apply the above steps to the subarray of elements with smaller values and separately to the subarray of elements with greater values.
The base case of the recursion is arrays of size zero or one, which never need to be sorted. In pseudocode, a quicksort that sorts elements i through k (inclusive) of an array A can be expressed compactly as
quicksort(A, i, k): if i < k: p := partition(A, i, k) quicksort(A, i, p  1) quicksort(A, p + 1, k)
Sorting the entire array is accomplished by calling quicksort(A, 1, length(A))
. The partition
operation is step 2 from the description in English, above. It can be defined as:
// left is the index of the leftmost element of the subarray // right is the index of the rightmost element of the subarray (inclusive) // number of elements in subarray = rightleft+1 partition(array, left, right) pivotIndex := choosePivot(array, left, right) pivotValue := array[pivotIndex] swap array[pivotIndex] and array[right] storeIndex := left for i from left to right  1 if array[i] < pivotValue swap array[i] and array[storeIndex] storeIndex := storeIndex + 1 swap array[storeIndex] and array[right] // Move pivot to its final place return storeIndex
This is the inplace partition algorithm. It partitions the portion of the array between indexes left and right, inclusively, by moving all elements less than or equal array[pivotIndex]
before the pivot, and the greater elements after it. In the process it also finds the final position for the pivot element, which it returns. It temporarily moves the pivot element to the end of the subarray, so that it doesn’t get in the way. Because it only uses exchanges, the final list has the same elements as the original list. Notice that an element may be exchanged multiple times before reaching its final place. Also, in case of pivot duplicates in the input array, they can be spread across the right subarray, in any order. This doesn’t represent a partitioning failure, as further sorting will reposition and finally “glue” them together.
This form of the partition algorithm is not the original form; multiple variations can be found in various textbooks, such as versions not having the storeIndex. However, this form is probably the easiest to understand.
Each recursive call to the combined quicksort function reduces the size of the array being sorted by at least one element, since in each invocation the element at pivotNewIndex is placed in its final position. Therefore, this algorithm is guaranteed to terminate after at most n recursive calls. However, since partition reorders elements within a partition, this version of quicksort is not a stable sort.