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 firstname.lastname@example.org. 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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Stable QuickSort
- C++ Program for QuickSort
- Why quicksort is better than mergesort ?
- Python Program for QuickSort
- QuickSort using Random Pivoting
- Java Program for QuickSort
- QuickSort on Doubly Linked List
- QuickSort on Singly Linked List
- When does the worst case of Quicksort occur?
- 3-Way QuickSort (Dutch National Flag)
- Generic Implementation of QuickSort Algorithm in C
- Hoare's vs Lomuto partition scheme in QuickSort
- Comparisons involved in Modified Quicksort Using Merge Sort Tree
- Can QuickSort be implemented in O(nLogn) worst case time complexity?