The answer depends on strategy for choosing pivot. In early versions of Quick Sort where leftmost (or rightmost) element is chosen as pivot, the worst occurs in following cases.
1) Array is already sorted in same order.
2) Array is already sorted in reverse order.
3) All elements are same (special case of case 1 and 2)
Since these cases are very common use cases, the problem was easily solved by choosing either a random index for the pivot, choosing the middle index of the partition or (especially for longer partitions) choosing the median of the first, middle and last element of the partition for the pivot. With these modifications, the worst case of Quick sort has less chances to occur, but worst case can still occur if the input array is such that the maximum (or minimum) element is always chosen as pivot.
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.
- Can QuickSort be implemented in O(nLogn) worst case time complexity?
- QuickSort Tail Call Optimization (Reducing worst case space to Log n )
- Find a permutation that causes worst case of Merge Sort
- 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
- Comparisons involved in Modified Quicksort Using Merge Sort Tree
- Generic Implementation of QuickSort Algorithm in C
- Stable QuickSort
- QuickSort using Random Pivoting
- Merge two sorted arrays in O(1) extra space using QuickSort partition
- How to make Mergesort to perform O(n) comparisons in best case?
- Print a case where the given sorting algorithm fails