# Dual pivot Quicksort

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.

An example: `// C program to implement dual pivot QuickSort ` `#include ` ` `  `int` `partition(``int``* arr, ``int` `low, ``int` `high, ``int``* lp); ` ` `  `void` `swap(``int``* a, ``int``* b) ` `{ ` `    ``int` `temp = *a; ` `    ``*a = *b; ` `    ``*b = temp; ` `} ` ` `  `void` `DualPivotQuickSort(``int``* arr, ``int` `low, ``int` `high) ` `{ ` `    ``if` `(low < high) { ` `        ``// lp means left pivot, and rp means right pivot. ` `        ``int` `lp, rp;  ` `        ``rp = partition(arr, low, high, &lp); ` `        ``DualPivotQuickSort(arr, low, lp - 1); ` `        ``DualPivotQuickSort(arr, lp + 1, rp - 1); ` `        ``DualPivotQuickSort(arr, rp + 1, high); ` `    ``} ` `} ` ` `  `int` `partition(``int``* arr, ``int` `low, ``int` `high, ``int``* lp) ` `{ ` `    ``if` `(arr[low] > arr[high]) ` `        ``swap(&arr[low], &arr[high]); ` `    ``// p is the left pivot, and q is the right pivot. ` `    ``int` `j = low + 1; ` `    ``int` `g = high - 1, k = low + 1, p = arr[low], q = arr[high]; ` `    ``while` `(k <= g) { ` ` `  `        ``// if elements are less than the left pivot ` `        ``if` `(arr[k] < p) { ` `            ``swap(&arr[k], &arr[j]); ` `            ``j++; ` `        ``} ` ` `  `        ``// if elements are greater than or equal  ` `        ``// to the right pivot ` `        ``else` `if` `(arr[k] >= q) { ` `            ``while` `(arr[g] > q && k < g) ` `                ``g--; ` `            ``swap(&arr[k], &arr[g]); ` `            ``g--; ` `            ``if` `(arr[k] < p) { ` `                ``swap(&arr[k], &arr[j]); ` `                ``j++; ` `            ``} ` `        ``} ` `        ``k++; ` `    ``} ` `    ``j--; ` `    ``g++; ` ` `  `    ``// bring pivots to their appropriate positions. ` `    ``swap(&arr[low], &arr[j]); ` `    ``swap(&arr[high], &arr[g]); ` ` `  `    ``// returning the indices of the pivots. ` `    ``*lp = j; ``// because we cannot return two elements  ` `             ``// from a function. ` ` `  `    ``return` `g; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 24, 8, 42, 75, 29, 77, 38, 57 }; ` `    ``DualPivotQuickSort(arr, 0, 7); ` `    ``printf``(``"Sorted array: "``); ` `    ``for` `(``int` `i = 0; i < 8; i++) ` `        ``printf``(``"%d "``, arr[i]); ` `    ``printf``(``"\n"``); ` `    ``return` `0; ` `} `

Output:

```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 contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Improved By : ManasChhabra2

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.