Quick Sort
Let's now study the famous Quick Sort algorithm!
Quick sort approach
Quick sort is the fastest-known, comparison-based sorting algorithm for lists in the average case.
Caveat: Merge sort works better on linked lists and there are other non-comparison-based algorithms that outperform Quick Sort.
Steps for quick sort algorithm
- Start with a list of n elements
- Choose a pivot element from the list to be sorted
- Partition the list into 2 unsorted sublists, such that all elements in one sublist are less than the pivot and all the elements in the other sublist are greater than the pivot
- Elements that are equal to the pivot can go in either sublist
- Sort each sublist recursively to yield two sorted sublists
- Concatenate the two sorted sublists and the pivot to yield one sorted list
How to pick the pivot?
There are two methods by which you can choose a pivot:
Choose randomly
The pivot can be picked randomly from the given elements. The chances of the first element being picked at every recursive call with this strategy is, ...