Back to Dashboard
Quick Sort
An efficient, comparison-based sorting algorithm that uses a divide-and-conquer strategy to pick a 'pivot' and partition the array around it.
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 < high3 pi = partition(array, low, high)4 quickSort(array, low, pi - 1)5 quickSort(array, pi + 1, high)67function partition(array, low, high)8 pivot = array[high]9 i = low - 110 for j = low to high - 111 if array[j] < pivot12 i++13 swap array[i] and array[j]14 swap array[i + 1] and array[high]15 return i + 1
Sponsor Ad