forked from duanjigui/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuick-Sort(Extra-Optimised).cpp
More file actions
74 lines (60 loc) · 886 Bytes
/
Quick-Sort(Extra-Optimised).cpp
File metadata and controls
74 lines (60 loc) · 886 Bytes
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
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define ELEMENT_COUNT 1000000
using namespace std;
int d[ELEMENT_COUNT];
int flag = 0;
void qsort(int l, int r)
{
if (l < r)
{
int _rand = rand() % (r - l + 1) + l;
int temp = d[_rand];
d[_rand] = d[r];
d[r] = temp;
int x = d[r];
int j = l - 1;
for (int i = l; i < r; i++)
{
if (d[i] < x)
{
j++;
temp = d[i];
d[i] = d[j];
d[j] = temp;
}
else if (d[i] == x)
{
if ((flag++ & 1) == 0)
{
j++;
temp = d[i];
d[i] = d[j];
d[j] = temp;
}
}
}
j++;
temp = d[r];
d[r] = d[j];
d[j] = temp;
qsort(l, j - 1);
qsort(j + 1, r);
}
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &d[i]);
}
qsort(0, n - 1);
for (int i = 0; i < n; i++)
{
printf("%d ", d[i]);
}
return 0;
}