-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdutch_flag_algo_quicksort3.cpp
More file actions
executable file
·78 lines (67 loc) · 1.51 KB
/
dutch_flag_algo_quicksort3.cpp
File metadata and controls
executable file
·78 lines (67 loc) · 1.51 KB
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
76
77
78
#include <bits/stdc++.h>
using namespace std;
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
/* This function partitions a[] in three parts
a) a[l..i] contains all elements smaller than pivot
b) a[i+1..j-1] contains all occurrences of pivot
c) a[j..r] contains all elements greater than pivot */
//It uses Dutch National Flag Algorithm
void partition(vector<int> &a, int low, int high, int &i, int &j)
{
// To handle 2 elements
if (high - low <= 1)
{
if (a[high] < a[low])
swap(&a[high], &a[low]);
i = low;
j = high;
return;
}
int mid = low;
int pivot = a[high];
while (mid <= high)
{
if (a[mid] < pivot)
swap(&a[low++], &a[mid++]);
else if (a[mid] == pivot)
mid++;
else if (a[mid] > pivot)
swap(&a[mid], &a[high--]);
}
//update i and j
i = low - 1;
j = mid; //or high-1
}
// 3-way partition based quick sort
void quicksort(vector<int> &a, int low, int high)
{
if (low >= high) //1 or 0 elements
return;
int i, j;
// Note that i and j are passed as reference
partition(a, low, high, i, j);
// Recur two halves
quicksort(a, low, i);
quicksort(a, j, high);
}
// Driver program
int main()
{
int n;
std::cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
{
std::cin >> a[i];
}
quicksort(a, 0, n - 1);
for (int i = 0; i < n; i++)
{
std::cout << a[i] << ' ';
}
}