- Lower bound for comparison based sorting algorithms
- Which sorting algorithm makes minimum number of memory writes?
- Merge Sort
- Bubble Sort
- Insertion Sort
- 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
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.
- 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
- 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
Writing code in comment? Please use ide.geeksforgeeks.org , generate link and share the link here.
- Count all distinct pairs with difference equal to k
- Union and Intersection of two Linked Lists
- Sort an array in wave form
- Intersection of two Sorted Linked Lists
- QuickSort on Singly Linked List
- Segregate 0s and 1s in an array
- Bucket Sort
- Print All Distinct Elements of a given integer array
- Merge two sorted arrays
- Sort a stack using a temporary stack
- Sort an array according to the order defined by another array
- Sort elements by frequency | Set 2
- Sort string of characters
- Recursive Bubble Sort
- Segregate even and odd numbers | Set 3
- Find minimum difference between any two elements
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Check if two arrays are equal or not
- Sort elements by frequency | Set 4 (Efficient approach using hash)
- Sort a linked list of 0s, 1s and 2s
- Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
- Merge two sorted arrays with O(1) extra space
- Find the point where maximum intervals overlap
- Rearrange positive and negative numbers with constant extra space
Khan Academy does not support Internet Explorer.
Just select one of the options below to start upgrading.
If you’re seeing this message, it means we’re having trouble loading external resources on our website.
If you’re behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.
Analysis of quicksort
partitionfunction is always either the smallest or the largest element in the -element subarray. Then one of the partitions will contain no elements and the other partition will contain elements—all but the pivot. So the recursive calls will be on subarrays of sizes 0 and .
Worst-case running time
Best-case running time
Average-case running time
nnodes, and so the partitioning time for every level is at most . Altogether, there are levels, and so the total partitioning time is . Now, there's a mathematical fact that
instead of like this:
partitionfunction assumes that the pivot is in the rightmost position of the subarray. No problem—just swap the element that you chose as the pivot with the rightmost element, and then partition as before. Unless your enemy knows how you choose random locations in the subarray, you win!