-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.h
More file actions
402 lines (340 loc) · 10.8 KB
/
Sort.h
File metadata and controls
402 lines (340 loc) · 10.8 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#if !defined (SORT_H)
#define SORT_H
template < class T >
class Sort
{
private:
static void _selectionSort(T** items, int numItems, int (*compare) (T* one, T* two));
static void _mergeSort(T** items, int first, int last, int (*compare) (T* one, T* two));
static void merge(T** items, int first, int mid, int last, int (*compare) (T* one, T* two));
static void choosePivot(T** items, int first, int last);
static int partition(T** items, int first, int last, int (*compare) (T* one, T* two));
static void _quickSort(T** items, int first, int last, int (*compare) (T* one, T* two));
static void _insertionSort(T** items, int numItems, int (*compare) (T* one, T* two));
static int insertLocation(T** items, T* item, int last, int (*compare) (T* one, T* two));
static void _insertionBinarySort(T** items, int numItems, int (*compare) (T* one, T* two));
static int linearSearch(T** items, int numItems, T* item, int index, int (*compare) (T* one, T* two));
static int binarySearch(T** items, int numItems, T* item, int first, int last, int (*compare) (T* one, T* two));
public:
static T** selectionSort(T** items, int numItems, int (*compare) (T* one, T* two));
static T** insertionSort(T** items, int numItems, int (*compare) (T* one, T* two));
static T** mergeSort(T** items, int numItems, int (*compare) (T* one, T* two));
static T** quickSort(T** items, int numItems, int (*compare) (T* one, T* two));
static T** insertionBinarySort(T** items, int numItems, int (*compare) (T* one, T* two));
static int linearSearch(T** items, int numItems, T* item, int (*compare) (T* one, T* two));
static int binarySearch(T** items, int numItems, T* item, int (*compare) (T* one, T* two));
};
template < class T >
T** Sort<T>::quickSort(T** items, int num_items, int (*compare) (T* one, T* two))
{
//DO THIS
//create a new array that will be sorted and returned
//this is in case the original, unsorted array is also needed
T** array = new T*[num_items];
for (int x = 0; x < num_items; x++)
{
array[x]= items[x];
}
_quickSort(array, 0, num_items-1, compare);
return array;
}
template < class T >
void Sort<T>::_quickSort(T** array, int first, int last, int (*compare) (T* one, T* two))
{
int pivotIndex;
//DO THIS
//make the necessary partition and recursive calls for quick sort
if (first < last)
{
pivotIndex = partition(array, first, last, compare);
_quickSort(array, first, (pivotIndex - 1), compare);
_quickSort(array,(pivotIndex + 1), last, compare);
}
}
template < class T >
int Sort<T>::partition(T** array, int first, int last, int (*compare) (T* one, T* two))
{
//DO THIS
//complete the partition method (Lomuto partition)
//temp is used to swap elements in the array
T* temp;
//initially, choosePivot does nothing
choosePivot(array, first, last);
int s1= first-1;
for (int x = first; x <= last; x++)
{
if ((*compare)(array[first], array[x]) >= 0)
{
s1++;
temp = array[x];
array[x] = array[s1];
array[s1] = temp;
}
}
temp = array[s1];
array[s1] = array[first];
array[first] = array[s1];
return s1;
}
template < class T >
void Sort<T>::choosePivot(T** items, int first, int last)
{
//DO THIS
//find a better item to be the partition than simply using the item in the first index
//you will need to swap
T* temp;
temp = items[first + ((last - first)/2)];
items[first + ((last - first)/2)] = items [first];
items [first]= temp;
}
//no work below this point
template < class T >
int Sort<T>::linearSearch(T** items, int numItems, T* item, int (*compare) (T* one, T* two))
{
return linearSearch(items, numItems, item, 0, compare);
}
template < class T >
int Sort<T>::linearSearch(T** items, int numItems, T* item, int loc, int (*compare) (T* one, T* two))
{
//we are using a special return type as the cases below are not exceptional
if (loc == numItems) //base case end of array
{
return -1; //item not in array (base case #1)
}
int index;
int comp = (*compare) (item, items[loc]);
if (comp != 0)
{
index = linearSearch(items, numItems, item, loc + 1, compare);
}
else
{
index = loc;
}
return index;
}
template < class T >
int Sort<T>::binarySearch(T** items, int numItems, T* item, int (*compare) (T* one, T* two))
{
return binarySearch(items, numItems, item, 0, numItems - 1, compare);
}
template < class T >
int Sort<T>::binarySearch(T** items, int numItems, T* item, int first, int last, int (*compare) (T* one, T* two))
{
// reaches the base case much faster
if (first > last)
{
return -1; // value not in original array (base case #1)
}
int index;
int mid = first + (last - first)/2;
int comp = (*compare) (item, items[mid]);
if (comp == 0)
{
index = mid; // value found at search[mid] (base case #2)
}
else if (comp < 0)
{
//making these method calls is extra overhead that recursion requires
//as it is easy and readable to write a binary search using a loop, that is the preferred implementation
index = binarySearch(items, numItems, item, first, mid - 1, compare);
}
else
{
index = binarySearch(items, numItems, item, mid + 1, last, compare);
}
return index; //returns the index up through the chain
}
template < class T >
T** Sort<T>::selectionSort(T** items, int numItems, int (*compare) (T* one, T* two))
{
T** sorted = new T*[numItems];
for (int i = 0; i < numItems; i++)
{
sorted[i] = items[i];
}
//passes the address of the function via compare
_selectionSort(sorted, numItems, compare);
return sorted;
}
template < class T >
void Sort<T>::_selectionSort(T** items, int numItems, int (*compare) (T* one, T* two))
{
int min;
T* item;
for (int spot = 0; spot < numItems - 1; spot++) //n
{
min = spot; //n - 1
for (int scan = spot + 1; scan < numItems; scan++) //[n(n + 1)/2] - 1
{
//dereferences compare (calls the function)
if ((*compare) (items[scan], items[min]) < 0) //(n - 1)(n)/2
{
min = scan; //worst case (n - 1)(n)/2
}
}
// Swap the values
item = items[min]; //3(n - 1)
items[min] = items[spot];
items[spot] = item;
}
}
template < class T >
T** Sort<T>::mergeSort(T** items, int numItems, int (*compare) (T* one, T* two))
{
T** sorted = new T*[numItems];
for (int i = 0; i < numItems; i++)
{
sorted[i] = items[i];
}
_mergeSort(sorted, 0, numItems - 1, compare);
return sorted;
}
template < class T >
void Sort<T>::_mergeSort(T** items, int first, int last, int (*compare) (T* one, T* two))
{
if (first < last)
{
// sort each half
int mid = first + (last - first)/2; //(first + last)/2; // index of midpoint
// sort left half sort[first..mid]
_mergeSort(items, first, mid, compare);
// sort right half sort[mid + 1..last]
_mergeSort(items, mid + 1, last, compare);
// merge the two halves
merge(items, first, mid, last, compare);
}
}
template < class T >
void Sort<T>::merge(T** items, int first, int mid, int last, int (*compare) (T* one, T* two))
{
//tempArray
T** temp = new T*[last - first + 1];
// initialize the local indexes to indicate the subarrays
int first1 = first; // beginning of first subarray
int last1 = mid; // end of first subarray
int first2 = mid + 1; // beginning of second subarray
int last2 = last; // end of second subarray
// while both subarrays are not empty, copy the
// smaller item into the temporary array
int index = 0;
while ((first1 <= last1) && (first2 <= last2))
{
if ((*compare) (items[first1], items[first2]) <= 0) //careful here for stable sorting
{
temp[index] = items[first1];
first1++;
}
else
{
temp[index] = items[first2];
first2++;
}
index++;
}
// finish off the nonempty subarray
// finish off the first subarray, if necessary
while (first1 <= last1)
{
temp[index] = items[first1];
first1++;
index++;
}
// finish off the second subarray, if necessary
while (first2 <= last2)
{
temp[index] = items[first2];
first2++;
index++;
}
// copy the result back into the original array
for (index = 0; index <= last - first; index++)
{
items[index + first] = temp[index];
}
delete[] temp;
}
template < class T >
T** Sort<T>::insertionSort(T** items, int numItems, int (*compare) (T* one, T* two))
{
T** sorted = new T*[numItems];
for (int i = 0; i < numItems; i++)
{
sorted[i] = items[i];
}
_insertionSort(sorted, numItems, compare);
return sorted;
}
template < class T >
void Sort<T>::_insertionSort(T** items, int numItems, int (*compare) (T* one, T* two))
{
T* temp;
int position;
for (int spot = 1; spot < numItems; spot++)
{
temp = items[spot];
position = spot;
// shift larger values to the right
while (position > 0 && (*compare) (items[position - 1], temp) > 0)
{
items[position] = items[position - 1];
position--;
}
items[position] = temp;
}
}
template < class T >
T** Sort<T>::insertionBinarySort(T** items, int numItems, int (*compare) (T* one, T* two))
{
T** sorted = new T*[numItems];
for (int i = 0; i < numItems; i++)
{
sorted[i] = items[i];
}
_insertionBinarySort(sorted, numItems, compare);
return sorted;
}
template < class T >
void Sort<T>::_insertionBinarySort(T** items, int numItems, int (*compare) (T* one, T* two))
{
T* temp;
int position;
for (int spot = 1; spot < numItems; spot++)
{
temp = items[spot];
// shift larger values to the right
position = insertLocation(items, temp, spot - 1, compare); //binary search to minimize comparisons
for(int i = spot; i > position; i--)
{
items[i] = items[i - 1];
}
items[position] = temp;
}
}
template < class T >
int Sort<T>::insertLocation(T** items, T* item, int last, int (*compare) (T* one, T* two))
{
//based on array indices
int first = 0;
int mid = first + (last - first)/2;
while (first <= last)
{
int comp = ((*compare) (item, items[mid]));
if (comp == 0)
{
break;
}
else if (comp < 0)
{
last = mid - 1;
mid = first + (last - first)/2;
}
else
{
first = mid + 1;
mid = first + (last - first)/2;
}
}
return mid;
}
#endif