As we know, the single pivot quick sort takes a pivot from one of the ends of the array and partitioning the array, so that all elements are left to the pivot are less than or equal to the pivot, and all elements that are right to the pivot are greater than the pivot.
The idea of dual pivot quick sort is to take two pivots, one in the left end of the array and the second, in the right end of the array. The left pivot must be less than or equal to the right pivot, so we swap them if necessary.
Then, we begin partitioning the array into three parts: in the first part, all elements will be less than the left pivot, in the second part all elements will be greater or equal to the left pivot and also will be less than or equal to the right pivot, and in the third part all elements will be greater than the right pivot. Then, we shift the two pivots to their appropriate positions as we see in the below bar, and after that we begin quicksorting these three parts recursively, using this method.
Dual pivot quick sort is a little bit faster than the original single pivot quicksort. But still, the worst case will remain O(n^2) when the array is already sorted in an increasing or decreasing order.
Sorted array: 8 24 29 38 42 57 75 77
This article is contributed by Shlomi Elhaiani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
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 on Doubly Linked List
- QuickSort on Singly Linked List
- Can QuickSort be implemented in O(nLogn) worst case time complexity?
- 3-Way QuickSort (Dutch National Flag)
- QuickSort Tail Call Optimization (Reducing worst case space to Log n )
- Hoare's vs Lomuto partition scheme in QuickSort
- Why quicksort is better than mergesort ?
- 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