-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch7.cpp
207 lines (189 loc) · 4.34 KB
/
ch7.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream> // std::cout
class Solution{
public:
void Swap(int &a, int &b){
int tmp = a;
a = b;
b = tmp;
}
// quicksort clrs p171
int Partition(int A[], int p, int q){
int x = A[p];
int i = p;
for(int j = p + 1; j <= q; j++){
if(A[j] <= x){
i++;
Swap(A[i], A[j]);
}
}
Swap(A[p], A[i]);
return i;
}
void Quicksort(int A[], int p, int r){
if(p < r){
int q = Partition(A, p, r);
Quicksort(A, p, q - 1);
Quicksort(A, q + 1, r);
}
}
// equal element quicksort, clrs p174 7.1-2
// all array elements are the same, returns i = (p + q) / 2
// it still has nlgn instead of n^2 time complexity
int EqualElementPartition(int A[], int p, int q){
int x = A[p];
int i = p;
for(int j = p + 1; j <= q; j++){
if(A[j] < x){
i++;
Swap(A[i], A[j]);
}
else if(A[j] == x){
// partition in the middle for all equal elements
if(i <= (p + q) / 2){
i++;
Swap(A[i], A[j]);
}
}
}
Swap(A[p], A[i]);
return i;
}
void EqualElementQuicksort(int A[], int p, int r){
if(p < r){
int q = EqualElementPartition(A, p, r);
EqualElementQuicksort(A, p, q - 1);
EqualElementQuicksort(A, q + 1, r);
}
}
// clrs p185 7-1 hoare partition correctness
int HoarePatition(int A[], int p, int q){
int x = A[p];
int i = p - 1;
int j = q + 1;
while(true){
do{
i++;
} while(A[i] < x);
do{
j++;
} while(A[j] > x);
if(i < j)
Swap(A[i], A[j]);
else
return j;
}
}
void HoareQuicksort(int A[], int p, int r){
if(p >= r)
return;
int q = HoarePatition(A, p, r);
HoareQuicksort(A, p, q - 1);
HoareQuicksort(A, q + 1, r);
}
// clrs p186 7-2 quicksort with equal element values
// refer to http://www.douban.com/note/219642993/
void ThreeWayQuicksort(int A[], int l, int r){
if(l >= r)
return;
int x = A[l]; // pivot is selected as the left most element
int left = l + 1, lflag = l + 1; // left pointers
int right = r, rflag = r; // right pointers
while(true){
while(A[left] <= x && left <= r){
// if an element is equal to pivot, then swap it with A[lflag]
if(A[left] == x){
Swap(A[left], A[lflag]);
lflag++;
}
left++;
}
while(A[right] >= x && right > l){
// if an element is equal to pivot, then swap it with A[rflag]
if(A[right] == x){
Swap(A[right], A[rflag]);
rflag--;
}
right--;
}
if(left >= right) break;
// swap a bigger element (than pivot) on the left with a smaller one on the right
Swap(A[left], A[right]);
left++;
right--;
}
// swap the equal elements on the left to the middle
left--;
lflag--;
while(lflag >= l){
Swap(A[left], A[lflag]);
left--;
lflag--;
}
// swap the equal elements on the right to the middle
right++;
rflag++;
while(rflag <= r){
Swap(A[right], A[rflag]);
right++;
rflag++;
}
// recursively call the function
ThreeWayQuicksort(A, l, left);
ThreeWayQuicksort(A, right, r);
}
};
int main()
{
// unit test
int A[] = {2, 8, 7, 1, 3, 5, 6, 4};
std::cout << "Unsorted array A entries: ";
for(int i = 0; i < 8; i++){
std::cout << A[i] << " ";
}
std::cout << std::endl;
Solution sln;
sln.Quicksort(A, 0, 7);
std::cout << "Sorted array A entries: ";
for(int i = 0; i < 8; i++){
std::cout << A[i] << " ";
}
std::cout << std::endl;
//int B[] = {1, 1, 1, 1, 1, 1, 1, 1}; // all elements are equal
int B[] = {1, 5, 1, 1, 2, 1, 1, 1};
std::cout << "Unsorted array B entries: ";
for(int i = 0; i < 8; i++){
std::cout << B[i] << " ";
}
std::cout << std::endl;
sln.EqualElementQuicksort(B, 0, 7);
std::cout << "Sorted array B entries: ";
for(int i = 0; i < 8; i++){
std::cout << B[i] << " ";
}
std::cout << std::endl;
int C[] = {13, 19, 9, 5, 12, 8, 7, 4, 11, 2, 6, 21};
std::cout << "Unsorted array C entries: ";
for(int i = 0; i < 12; i++){
std::cout << C[i] << " ";
}
std::cout << std::endl;
sln.EqualElementQuicksort(C, 0, 11);
std::cout << "Sorted array C entries: ";
for(int i = 0; i < 12; i++){
std::cout << C[i] << " ";
}
std::cout << std::endl;
int D[] = {2, 1, 2, 4, 3};
std::cout << "Unsorted array D entries: ";
for(int i = 0; i < 5; i++){
std::cout << D[i] << " ";
}
std::cout << std::endl;
sln.ThreeWayQuicksort(D, 0, 4);
std::cout << "Sorted array D entries: ";
for(int i = 0; i < 5; i++){
std::cout << D[i] << " ";
}
std::cout << std::endl;
return 0;
}