- Lower bound for comparison based sorting algorithms
- Which sorting algorithm makes minimum number of memory writes?
- Merge Sort
- QuickSort
- Bubble Sort
- Insertion Sort
- HeapSort
- Selection Sort
- std::sort() in C++ STL
- Sort an array of 0s, 1s and 2s
- k largest(or smallest) elements in an array | added Min Heap method
- Count Inversions in an array | Set 1 (Using Merge Sort)
- Merge Sort for Linked Lists
- Counting Sort
- Radix Sort
- Minimum number of swaps required to sort an array
- Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)
- Time Complexities of all Sorting Algorithms
- Sort elements by frequency | Set 1
- Find a triplet that sum to a given value
- Sort a nearly sorted (or K sorted) array
- Given a sorted dictionary of an alien language, find order of characters
- Given a number, find the next smallest palindrome
- Chocolate Distribution Problem
- Merge k sorted arrays | Set 1
- Find four elements that sum to a given value | Set 2 ( O(n^2Logn) Solution)
- Longest Consecutive Subsequence
- Stability in sorting algorithms
- Find whether an array is subset of another array | Added Method 3
- Find all triplets with zero sum

# When does the worst case of Quicksort occur?

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.

References:

http://en.wikipedia.org/wiki/Quicksort

*arrow_drop_up*

## Recommended Posts:

- 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
- C++ Program for QuickSort
- Stable QuickSort
- Why quicksort is better than mergesort ?
- Dual pivot Quicksort
- Java Program for QuickSort
- QuickSort using Random Pivoting
- Python Program for QuickSort
- Generic Implementation of QuickSort Algorithm in C
- 3-Way QuickSort (Dutch National Flag)
- QuickSort on Singly Linked List
- QuickSort on Doubly Linked List