The worst case time complexity of a typical implementation of QuickSort is O(n2). The worst case occurs when the picked pivot is always an extreme (smallest or largest) element. This happens when input array is sorted or reverse sorted and either first or last element is picked as pivot.
Although randomized QuickSort works well even when the array is sorted, there is still possibility that the randomly picked element is always an extreme. Can the worst case be reduced to O(nLogn)?
The answer is yes, we can achieve O(nLogn) worst case. The idea is based on the fact that the median element of an unsorted array can be found in linear time. So we find the median first, then partition the array around the median element.
Following is C++ implementation based on above idea. Most of the functions in below progran are copied from K’th Smallest/Largest Element in Unsorted Array | Set 3 (Worst Case Linear Time)
Sorted array is 1 5 6 7 8 9 10 20 30 900 1000
How is QuickSort implemented in practice – is above approach used?
Although worst case time complexity of the above approach is O(nLogn), it is never used in practical implementations. The hidden constants in this approach are high compared to normal Quicksort. Following are some techniques used in practical implementations of QuickSort.
1) Randomly picking up to make worst case less likely to occur (Randomized QuickSort)
2) Calling insertion sort for small sized arrays to reduce recursive calls.
3) QuickSort is tail recursive, so tail call optimizations is done.
So the approach discussed above is more of a theoretical approach with O(nLogn) worst case time complexity.
This article is compiled by Shivam. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- When does the worst case of Quicksort occur?
- QuickSort Tail Call Optimization (Reducing worst case space to Log n )
- Find a permutation that causes worst case of Merge Sort
- Time complexity of insertion sort when there are O(n) inversions?
- An Insertion Sort time complexity question
- Python Code for time Complexity plot of Heap Sort
- C program for Time Complexity plot of Bubble, Insertion and Selection Sort using Gnuplot
- QuickSort on Doubly Linked List
- QuickSort on Singly Linked List
- 3-Way QuickSort (Dutch National Flag)
- Hoare's vs Lomuto partition scheme in QuickSort
- Why quicksort is better than mergesort ?
- Dual pivot Quicksort
- C++ Program for QuickSort
- Java Program for QuickSort
- Python Program for QuickSort
- QuickSort using Random Pivoting
- Comparisons involved in Modified Quicksort Using Merge Sort Tree
- Generic Implementation of QuickSort Algorithm in C