-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.hpp
223 lines (182 loc) · 5.09 KB
/
QuickSort.hpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* copyright (c) 2016 K Sreram
* This program and any associated files are under MIT license (https://opensource.org/licenses/MIT)
*/
#ifndef _QUICK_SORT_
#define _QUICK_SORT_
#include <vector>
#include <stdlib.h>
#include <time.h>
enum Flags{ LOMUTO_PARTITION_SCHEME = 1, HOARE_PARTITION_SCHEME = 2 };
template <class T>
void swap(T& a, T& b)
{
T temp;
temp = a;
a = b;
b = temp;
}
template < class DataType >
class QuickSort
{
std::vector < DataType >* dataList;
Flags algorithmChoice;
float relativePositionOfPivot;
public:
bool Randomize;
size_t count;
void obtainDataList(std::vector < DataType >* data)
{
dataList = data;
}
QuickSort()
{
// the defalut algorithm used
algorithmChoice = HOARE_PARTITION_SCHEME;
count = 0;
Randomize = false;
srand(time(NULL));
}
QuickSort(float p, std::vector < DataType >* data, const Flags& flag)
{
setPositonOfPivot(p);
setSortAlgorithm(flag);
obtainDataList(data);
Randomize = false;
srand(time(NULL));
count = 0;
}
void reInitialize(float p, std::vector < DataType >* data, const Flags& flag)
{
setPositonOfPivot(p);
setSortAlgorithm(flag);
obtainDataList(data);
Randomize = false;
srand(time(NULL));
count = 0;
}
void setSortAlgorithm(const Flags& flag)
{
algorithmChoice = flag;
}
void setPositonOfPivot(float p)
{
relativePositionOfPivot = p;
}
void Sort()
{
sortPorcedure(dataList, 0, dataList->size() - 1, algorithmChoice, relativePositionOfPivot);
}
std::vector < DataType >* returnArray()
{
return dataList;
}
///**************************************************************************************************
/// The following contains the main modules for the sort procedure
///**************************************************************************************************
/*
* This partition scheme is the most used among the two possiblities. This has two pointers,
* starting from the highest possible position and the lowest possible position respectively.
* Then as the iteration proceeds, both the pointers start approching each other.
*
* A partition scheme is used to determine how the array is partitioned after positioning the pivot.
*/
int hoarePartitionScheme(std::vector < DataType >* array, int lowPos, int highPos, int pivotPos)
{
int i = lowPos;
int j = highPos;
///sets the pivot based on the location
DataType pivot = (*array)[pivotPos];
/// this is an infnite loop which gets terminated when the pointers (i and j) cross each other.
while (true)
{
for (;
(i <= highPos) && ((*array)[i] <= pivot);
++i
#ifdef TEST_COMPLEXITY
,count++
#endif // TEST_COMPLEXITY
);
for (;
(j >= lowPos) && ((*array)[j] >= pivot);
--j
#ifdef TEST_COMPLEXITY
, count++
#endif // TEST_COMPLEXITY
);
if (i > j || i > highPos || j < lowPos) break;
swap((*array)[i], (*array)[j]);
}
// initial pivotPos is before the partition: need to be brought to 'j'
if (pivotPos < j)
{
swap((*array)[j], (*array)[pivotPos]);
return j;
}
// initial pivotPos is after the partition: need to be brought in place, i.e., 'i'
else if (pivotPos > i)
{
swap((*array)[i], (*array)[pivotPos]);
return i;
}
else
{
// swapping is not needed if the element is already preasent in the location it have to be in.
return pivotPos;
}
}
int lomutoPartitionScheme(std::vector < DataType >* array, int lowPos, int highPos, int pivotPos)
{
int i;
int j;
DataType pivot = (*array)[pivotPos];
for (i = j = lowPos; j <= highPos; ++j)
{
#ifdef TEST_COMPLEXITY
count++;
#endif // TEST_COMPLEXITY
if ((*array)[j] < pivot )
{
swap((*array)[i], (*array)[j]);
++i;
}
if (i == pivotPos)
++i;
}
if (pivotPos > i)
{
swap((*array)[i], (*array)[pivotPos]);
return i;
}
else if (pivotPos < i)
{
swap((*array)[i - 1], (*array)[pivotPos]);
return i - 1;
}
return i;
}
void sortPorcedure(std::vector < DataType > * array, int lowPos, int highPos, const Flags& flag, float relativePositionOfPivot)
{
int pos;
if (Randomize)
relativePositionOfPivot = (float)(rand() % (*array).size()) / ((float)(*array).size());
int pivotPos = lowPos + (highPos - lowPos)*relativePositionOfPivot;
if (lowPos >= highPos) return;
switch (flag)
{
case HOARE_PARTITION_SCHEME:
pos = hoarePartitionScheme(array, lowPos, highPos, pivotPos );
break;
case LOMUTO_PARTITION_SCHEME:
pos = lomutoPartitionScheme(array, lowPos, highPos, pivotPos );
break;
default:
break;
}
/// This should be designed in such a way that the placed pivot isn't included in the sorting process
/// again after it gets sorted
sortPorcedure(array, lowPos, pos-1, flag, relativePositionOfPivot);
sortPorcedure(array, pos+1, highPos, flag, relativePositionOfPivot);
}
};
#endif