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.
- Stable QuickSort
- Why quicksort is better than mergesort ?
- C++ Program for QuickSort
- Python Program for QuickSort
- QuickSort using Random Pivoting
- Java Program for QuickSort
- 3-Way QuickSort (Dutch National Flag)
- QuickSort on Singly Linked List
- QuickSort on Doubly Linked List
- Generic Implementation of QuickSort Algorithm in C
- When does the worst case of Quicksort occur?
- Hoare's vs Lomuto partition scheme in QuickSort
- Can QuickSort be implemented in O(nLogn) worst case time complexity?
- Comparisons involved in Modified Quicksort Using Merge Sort Tree