-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sorts.h
323 lines (229 loc) · 6.46 KB
/
Sorts.h
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//Sorts header file used in HW4
//Written by: Ozgur Yilmaz Beker
#ifndef SORTS_H
#define SORTS_H
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//Vector print for debugging
template <class T>
void printVector(vector<T>& subject)
{
for (int i = 0; i < subject.size(); i++)
cout << subject[i] << endl;
}
//Swap
template <class T>
inline void Swap( T & obj1,
T & obj2 )
{
T tmp = obj1;
obj1 = obj2;
obj2 = tmp;
}
//*************************************************************************
//=========================================================================
// SORTING ALGORITHMS
//=========================================================================
//*************************************************************************
//**********************************************
//1- Insertion Sort - Ascending
//**********************************************
//Pre: T is comparable
template <class T>
void insertSort(vector <T>& subject)
{
for (int i = 1; i < subject.size(); i++)
{
int idx = i;
while (idx > 0 && subject[idx] <= subject[idx - 1]) //Swap condition
{
Swap(subject[idx], subject[idx - 1]);
idx--;
}
}
}
//**********************************************
//2- Heap Sort - Ascending
//Pre: T is comparable
//**********************************************
//First, percolation operation
template <class T>
void percDown(vector<T>& subject, int i, int simulatedSize)
{
T temp = subject[i];
int percPosition = 0;
for(temp = subject[i] ; 2*i + 1 < simulatedSize ; i = percPosition) //Max iterations = log2(size)
{
percPosition = 2*i + 1;
if (percPosition != simulatedSize-1 && subject[percPosition] < subject[percPosition+1]) //Correct for left / right child
percPosition++;
if (subject[percPosition] > temp) //Swap condition
subject[i] = subject[percPosition];
else
break;
}
subject[i] = temp; //Final replacement for Swap op
}
//Below is the rest of the code used in the sort
template <class T>
void heapSort(vector<T>& subject)
{
//Convert vector to heap
//Only need to iterate over first size/2 elements, as leaf nodes are not important in heap
for (int i = subject.size() / 2; i >= 0 ; i--)
percDown(subject, i, subject.size());
// sort
for (int j = subject.size() - 1; j > 0; j--)
{
Swap(subject[0], subject[j]);
// re-form the heap
percDown(subject, 0, j);
}
}
//**********************************************
//3- Merge Sort (inplace algo - no temp array)
//Pre: T is comparable
//**********************************************
//Idea, shift array indices to save space, however this will make us lose time inevitably
//Merge operation
template <class T>
void merge(vector<T>& subject, int l, int r, int rend)
{
int lend = r-1;
int tmp = l;
int n = rend - l + 1;
while ( l <= lend && r <= rend )
if ( subject[ l ] <= subject[ r ] ) //Left is smaller, just move the leftPos
l++;
else //Right is smaller, Swap with all elements until leftpos and move indexers accordingly
{
int curRight = r;
while(curRight > l)
{
Swap(subject[curRight], subject[curRight-1]);
curRight--;
}
l++;
lend++;
r++;
}
}
//Mergesort recursive calls
template <class T>
void mergeSort(vector<T>& subject, int loweridx, int upperidx)
{
if ( loweridx < upperidx ) //if equal, no need to waste time (1 element base condition)
{
int middleidx = ( loweridx + upperidx ) / 2;
mergeSort( subject, loweridx, middleidx);
mergeSort( subject, middleidx + 1, upperidx);
merge( subject, loweridx, middleidx + 1, upperidx);
}
}
//Mergesort initial driver
template <class T>
void mergeSort(vector<T>& subject)
{
mergeSort(subject, 0, subject.size() - 1);
}
//**********************************************
//4- Quick Sort (median pivot, small = insertSort)
//Pre: T is comparable
//**********************************************
//Median for quicksort
template <class T>
const T& median(vector<T>& subject, int l, int r)
{
int m = ( l + r ) / 2;
// Place pivot at position right
Swap( subject[ m ], subject[ r ] );
return subject[ r ];
}
//Overloaded insertion sort
template <class T>
void insertSort(vector <T>& subject, int l, int r)
{
for (int i = l+1; i < r+1; i++)
{
int idx = i;
while (idx > l && subject[idx] <= subject[idx - 1]) //Swap condition
{
Swap(subject[idx], subject[idx - 1]);
idx--;
}
}
}
//Quicksort recursive calls
template <class T>
void quickSort(vector<T>& subject, int l, int r)
{
if (l+10 <= r)
{
T pivot = median(subject, l, r);
int i = l, j = r-1;
while(true)
{
while (subject[i] < pivot){i++;} //Find element larger than pivot from left
while (subject[j] > pivot) {j--;} //Find element smaller than pivot from right
if (i < j) //Still partitioning
Swap(subject[i], subject[j]);
else //Partition finished
break;
}
Swap(subject[i], subject[r]); //Pivot to its original pos
quickSort(subject, l, i-1);
quickSort(subject, i+1, r);
}
else
insertSort(subject, l, r);
}
//Quicksort initial driver
template <class T>
void quickSort(vector<T>& subject)
{
quickSort(subject, 0, subject.size()-1);
}
//*************************************************************************
//=========================================================================
// SEARCHING ALGORITHMS
//=========================================================================
//*************************************************************************
//**********************************************
//1- Binary Search
//Pre: T is comparable
//**********************************************
template <class T>
int binarySearch(vector<T>& subject, T& key)
{
int l = 0 , r = subject.size() - 1, m; //initialize left right and middle
while (l <= r){
m = (l+r)/2; // get middle
if (subject[m] == key) // key found at idx m
return m;
else if (subject[m] < key) // key is in the right half
l = m+1;
else // key is in the left half
r = m-1;
}
return -1; //will be used to control if NOT_FOUND
}
//**********************************************
//2- Sequential (Linear) Search
//**********************************************
template <class T>
int linSearch(vector<T>& subject, T& key)
{
int idx = -1;
for (int i = 0; i < subject.size(); i++)
{
if(subject[i] == key)
{
idx = i;
break;
}
}
return idx;
}
#endif