-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.cpp
302 lines (219 loc) · 6.62 KB
/
sort.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
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
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
// Swap values with a temporary variable
template <typename DataType>
void swap(DataType* one, DataType* two) {
if (!one || !two) {
return;
}
DataType temp(*one);
*one = *two;
*two = temp;
}
void printHeader() {
cout << endl << "#####################################" << endl;
}
void populateArray(int* array, int size) {
for (int index = 0; index < size; index++) {
array[index] = rand() % 100;
}
}
void printSubarray(const int* const array, int leftIndex, int rightIndex) {
for (int index = leftIndex; index <= rightIndex; index++) {
if (index != leftIndex) {
cout << ", ";
}
cout << array[index];
}
}
void printArray(const int* const array, int size) {
printSubarray(array, 0, size - 1);
}
void siftDown(int*array, int startIndex, int endIndex) {
int rootIndex = startIndex;
while (rootIndex * 2 + 1 <= endIndex) {
int childIndex = 2 * rootIndex + 1;
int swapIndex = rootIndex;
if (array[swapIndex] < array[childIndex]) {
swapIndex = childIndex;
}
if (childIndex + 1 <= endIndex &&
array[swapIndex] < array[childIndex + 1]) {
swapIndex = childIndex + 1;
}
if (swapIndex != rootIndex) {
int temp = array[rootIndex];
array[rootIndex] = array[swapIndex];
array[swapIndex] = temp;
rootIndex = swapIndex;
} else {
return;
}
}
}
void heapify(int* array, int size) {
int startIndex = (size - 1) / 2;
while (startIndex >= 0) {
siftDown(array, startIndex, size - 1);
startIndex--;
}
}
void heapSort(int* array, int size) {
heapify(array, size);
int endIndex = size - 1;
while (endIndex > 0) {
int temp = array[0];
array[0] = array[endIndex];
array[endIndex] = temp;
endIndex--;
siftDown(array, 0, endIndex);
}
}
void bubbleSort(int* array, int size) {
bool didSwap = true;
while (didSwap == true) {
didSwap = false;
for (int index = 0; index < size - 1; index++) {
if (array[index] > array[index + 1]) {
int temp = array[index];
array[index] = array[index + 1];
array[index + 1] = temp;
didSwap = true;
}
}
}
}
int quickSortPartition(int* array, int leftIndex, int rightIndex,
int pivotIndex) {
int pivotValue = array[pivotIndex];
swap(array[pivotIndex], array[rightIndex]);
int storeIndex = leftIndex;
for (int index = leftIndex; index < rightIndex; index++) {
if (array[index] <= pivotValue) {
swap(array[index], array[storeIndex]);
storeIndex++;
}
}
swap(array[storeIndex], array[rightIndex]);
return storeIndex;
}
void quickSortSubarray(int* array, int leftIndex, int rightIndex) {
if (leftIndex < rightIndex) {
int pivotIndex = (leftIndex + rightIndex) / 2;
int pivotNewIndex = quickSortPartition(array, leftIndex, rightIndex,
pivotIndex);
quickSortSubarray(array, leftIndex, pivotNewIndex - 1);
quickSortSubarray(array, pivotNewIndex + 1, rightIndex);
}
}
void quickSort(int* array, int size) {
quickSortSubarray(array, 0, size - 1);
}
void mergeSubarrays(int* array, int* outputArray, int oneLeftIndex,
int oneRightIndex, int twoLeftIndex, int twoRightIndex) {
int oneIndex = oneLeftIndex;
int twoIndex = twoLeftIndex;
int storeIndex = 0;
while (oneIndex <= oneRightIndex || twoIndex <= twoRightIndex) {
if (oneIndex <= oneRightIndex &&
(twoIndex > twoRightIndex || array[oneIndex] < array[twoIndex])) {
outputArray[storeIndex] = array[oneIndex];
oneIndex++;
} else {
outputArray[storeIndex] = array[twoIndex];
twoIndex++;
}
storeIndex++;
}
}
void copyArray(int* srcArray, int* destArray, int size) {
for (int index = 0; index < size; index++) {
destArray[index] = srcArray[index];
}
}
void mergeSort(int* array, int size) {
int* contents = array;
int* buffer = new int[size];
for (int width = 1; width < size; width = 2 * width) {
for (int index = 0; index < size; index = index + 2 * width) {
int twoLeftIndex = min(index + width, size - 1);
int twoRightIndex = min((index + 2 * width) - 1, size - 1);
mergeSubarrays(contents, buffer + index, index, twoLeftIndex - 1,
twoLeftIndex, twoRightIndex);
}
int* temp = contents;
contents = buffer;
buffer = temp;
}
if (array == buffer) {
copyArray(contents, buffer, size);
delete [] contents;
} else {
delete [] buffer;
}
}
int main(int /*argc*/, char** /*argv*/) {
cout << endl << "Beginning Sort Coding." << endl;
srand(time(NULL));
const int arraySize = 20;
int array[arraySize];
//
// Bubble Sort
//
printHeader();
cout << endl << "Bubble Sort" << endl;
populateArray(array, arraySize);
cout << endl << "Initial Array Contents:" << endl;
printArray(array, arraySize);
cout << endl;
bubbleSort(array, arraySize);
cout << endl << "Sorted Contents:" << endl;
printArray(array, arraySize);
cout << endl;
//
// Quick Sort
//
printHeader();
cout << endl << "Quick Sort" << endl;
populateArray(array, arraySize);
cout << endl << "Initial Array Contents:" << endl;
printArray(array, arraySize);
cout << endl;
quickSort(array, arraySize);
cout << endl << "Sorted Contents:" << endl;
printArray(array, arraySize);
cout << endl;
//
// Merge Sort
//
printHeader();
cout << endl << "Merge Sort" << endl;
populateArray(array, arraySize);
cout << endl << "Initial Array Contents:" << endl;
printArray(array, arraySize);
cout << endl;
mergeSort(array, arraySize);
cout << endl << "Sorted Contents:" << endl;
printArray(array, arraySize);
cout << endl;
//
// Heap Sort
//
printHeader();
cout << endl << "Heap Sort" << endl;
populateArray(array, arraySize);
cout << endl << "Initial Array Contents:" << endl;
printArray(array, arraySize);
cout << endl;
heapSort(array, arraySize);
cout << endl << "Sorted Contents:" << endl;
printArray(array, arraySize);
cout << endl;
//
// We're done.
//
printHeader();
cout << endl << "Done With Sort Coding." << endl;
}