Quick Sort

FastSlow
Initializing...
Time Complexity
Best
O(n log n)
Average
O(n log n)
Worst
O(n²)
Space Complexity
O(log n)
StabilityFALSE
In-placeTRUE

Explanation

Quick Sort is a highly efficient sorting algorithm and is based on partitioning of array of data into smaller arrays. A large array is partitioned into two arrays one of which holds values smaller than the specified value, say pivot, based on which the partition is made and another array holds values greater than the pivot value. It has an average time complexity of O(n log n), though it can degrade to O(n²) in the worst case.

Pseudo Code

1function quickSort(array, low, high)
2 if low < high
3 pi = partition(array, low, high)
4 quickSort(array, low, pi - 1)
5 quickSort(array, pi + 1, high)
6
7function partition(array, low, high)
8 pivot = array[high]
9 i = low - 1
10 for j = low to high - 1
11 if array[j] < pivot
12 i++
13 swap array[i] and array[j]
14 swap array[i + 1] and array[high]
15 return i + 1
Sponsor Ad