forked from samarthgaur2/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuicksort 3-way.cpp
75 lines (59 loc) · 1.39 KB
/
Quicksort 3-way.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
using namespace std;
// Quick Sort using Bentley-McIlroy 3-way Partitioning
void Quick_Sort_3way(int array[], int left, int right)
{
int pivot = array[right];
int i = left -1;
int j = right;
int p = left - 1;
int q = right;
if(right <= left) return;
while(true)
{
while(array[++i] < pivot) {}
while(pivot < array[--j])
{
if (j == left) break;
}
if(i >= j) break;
swap(array[i], array[j]);
if(array[i] == pivot)
{
p++;
swap(array[p], array[i]);
}
if(array[j] == pivot)
{
q--;
swap(array[j], array[q]);
}
}
swap(array[i], array[right]);
j = i - 1;
i = i + 1;
for(int k = left; k < p; k++, j--)
swap(array[k], array[j]);
for(int k = right - 1; k > q; k--, i++)
swap(array[i], array[k]);
Quick_Sort_3way(array, left, j);
Quick_Sort_3way(array, i, right);
}
// Function to print elements of array
void Print_Array(int array[], int size)
{
for(int i = 0; i < size; i++)
cout << array[i] << " ";
cout << endl;
}
// Driver Function
int main()
{
int array[] = {2, 4, 3, 1, 6, 8, 4};
int size = sizeof(array) / sizeof(int);
Quick_Sort_3way(array, 0, size - 1);
Print_Array(array, size);
return 0;
}
// Output
// 1 2 3 4 4 6 8