The Quick sort is another sorting algorithm based on the divide and conquer approach. The algorithm first selects a pivot element, and then divide, or partition, the input array into two lists around the pivot, the first with small items and the second with large items.
The algorithm then sorts both lists recursively until the resultant list is completely sorted.
The pivot help dividing the two list with elements that differs, making more efficient each recursive iteration. The pivot selection affect the performance of the algorithm.
With Quick sort, the input list is partitioned in linear time,
By using the median value as the pivot, you end up with a final runtime of O(n) + O(n log2n). You can simplify this down to O(n log2n) because the logarithmic portion grows much faster than the linear portion.
Python3 implementation: quick_sort.py
Although its worst-case scenario is theoretically
One of the main disadvantages is the lack of a guarantee that it will achieve the average runtime complexity.
Quick sort also trades off memory space for speed. This may become a limitation for sorting larger lists.
def quick_sort(array):
# Base cases
if len(array) < 2:
return array
low, same, high = [], [], []
# Select the pivot randomly
pivot = array[randint(0, len(array) - 1)]
for item in array:
# Elements smaller than the pivot go to the low list
if item < pivot:
low.append(item)
# Elements equal to pivot go to the same list.
elif item == pivot:
same.append(item)
# Elements larger than pivot go to the high list.
elif item > pivot:
high.append(item)
# The final result combines the sorted low, same, sorted
return quick_sort(low) + same + quick_sort(high)