-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortingAlgos.cpp
More file actions
273 lines (242 loc) · 7.54 KB
/
SortingAlgos.cpp
File metadata and controls
273 lines (242 loc) · 7.54 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
// Source file for SortingAlgos.h
// Created by Michael Gosling on 2/18/19.
#include "SortingAlgos.h"
/**
* Sort an array using the bubble sort algorithm
* @param a Array to sort
* @param length array length
*/
void SortingAlgos::bubbleSort(int a[], int length) {
int i, j; // indexes
for (i = 0; i < length-1; i++) {
for (j = 0; j < length - i - 1; j++) {
// if the value of this element is lower than the previous,
// swap them
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
/**
* Sort an array using the selection sort algorithm
* @param a Array to sort
* @param length length of array
*/
void SortingAlgos::selectionSort(int a[], int length) {
int outer, inner, min;
for (outer = 0; outer < length - 1; outer++) {
min = outer;
for (inner = outer + 1; inner < length; inner++)
if (a[inner] < a[min]) min = inner;
int temp = a[outer];
a[outer] = a[min];
a[min] = temp;
}
}
/**
* Sort an array using the insertion sort algorithm
* @param a Array to sort
* @param length Length of the array
*/
void SortingAlgos::insertionSort(int a[], int length) {
int i, j, tmp;
for (i = 1; i < length; i++) {
j = i;
// move backwards from j (starting at i) until j hits 0, swapping values if they're
// out of order
while (j > 0 && a[j - 1] > a[j]) {
tmp = a[j];
a[j] = a[j - 1];
a[j - 1] = tmp;
j--;
}
}
}
/**
* Sort an array using the Shell sort algorithm
* @param a Array to sort
* @param length Length of the array
*/
void SortingAlgos::shellSort(int a[], int length) {
/*
* This sort essentially splits up the array and uses insertion sort,
* decreasing the gap each time it hits the end of the array.
*/
int i, j, gap;
// starting at half the length, as long as gap is above 0, dividing by half each time
for (gap = length / 2; gap > 0; gap /= 2) {
// i starts at gap and increases as long as it's lower than length
for (i = gap; i < length; i++) {
auto temp = a[i];
// j starts at i and decrements by the gap value as long as it's larger
// or equal to the gap value
for (j = i; j >= gap; j -= gap) {
// if temp is higher than j minus gap index, assign to j index
if (temp < a[j - gap])
a[j] = a[j - gap];
else
break;
}
a[j] = temp; // assign the index of j the value of temp
}
}
}
/**
* Find the pivot point of the array by partitioning it
* @param a Array to partition
* @param beg Beginning of the array
* @param end End of the array
* @return The pivot point of the array
*/
int SortingAlgos::partition(int a[], int beg, int end) {
int p = beg, pivot = a[beg], location;
for (location = beg + 1; location <= end; location++) {
if (pivot>a[location]) {
a[p] = a[location];
a[location] = a[p + 1];
a[p + 1] = pivot;
p++;
}
}
return p;
}
/**
* Sort an array using the Quick Sort algorithm
* @param a Array to sort
* @param beg Beginning of array
* @param end end of array
* @param length size of the array
*/
void SortingAlgos::quickSort(int a[], int beg, int end, int length) {
if (beg<end) {
int pivot = partition(a, beg, end); // find pivot
quickSort(a, beg, pivot - 1, length); // subsort left
quickSort(a, pivot + 1, end, length); // subsort right
}
}
/**
* Overload so we don't always have to pass in the beginning and end
* @param a
* @param length
*/
void SortingAlgos::quickSort(int a[], int length) {
quickSort(a, 0, length - 1, length);
}
/**
* Merge arrays
* @param a array
* @param left left index
* @param middle middle point
* @param right right index
*/
void SortingAlgos::merge(int a[], int left, int middle, int right) {
int i, j, k;
int n1 = middle - left + 1;
int n2 = right - middle;
// temp arrays
int L[n1], R[n2];
// Copy data to temp arrays L[] and R[]
for (i = 0; i < n1; i++) L[i] = a[left + i];
for (j = 0; j < n2; j++) R[j] = a[middle + 1+ j];
// merge temp arrays
i = 0; // initial index of first subarray
j = 0; // initial index of second subarray
k = left; // initial index of merged subarray
while (i < n1 && j < n2) { // while the index of the subarrays are less than n1 and n2 respectively
if (L[i] <= R[j]) { // if the left array value is smaller, assign it's position in the main array
a[k] = L[i];
i++;
} else { // otherwise assign the right array value
a[k] = R[j];
j++;
}
k++; // increment merged array index either way
}
// Copy the remaining elements of L[], if there are any
while (i < n1) {
a[k] = L[i];
i++;
k++;
}
// Copy the remaining elements of R[], if there are any
while (j < n2) {
a[k] = R[j];
j++;
k++;
}
}
/**
* Perform Merge Sort on given array
* @param a array to sort
* @param left left index
* @param right right index
*/
void SortingAlgos::mergeSort(int a[], int left, int right) {
if (left < right) {
// get middle to split in two
int mid = left+(right-left)/2;
// sort first and second halves
mergeSort(a, left, mid);
mergeSort(a, mid+1, right);
// merge the halves
merge(a, left, mid, right);
}
}
/**
* Gets the highest value integer in the array
* @param a Array
* @param length Length of array
* @return Integer of max value
*/
int SortingAlgos::getMax(int a[], int length) {
// start with first element in array as max value
int max = a[0];
// loop through array and set max if the value is higher than the current max
for (int i = 1; i < length; i++)
max = (a[i] > max) ? a[i] : max;
return max;
}
/**
* Sorts the array according to the current digit
* @param a Array to sort
* @param length Length of array
* @param e exponent value
*/
void SortingAlgos::countSort(int a[], int length, int e) {
// create an output array
int out[length];
// create a count array to hold digits 0-9
int count[10] = {0};
// Divide the value by the exponent, then mod 10. The remaining value is the
// digit we're looking for. We increment that by 1
for (auto i = 1; i < length; i++)
count[(a[i]/e)%10]++;
// We add count[i-1] to count[i] in order to get eh actual position of the digit
// in the output array
for (auto i = 1; i < 10; i++)
count[i] += count[i-1];
// build the output array, iterating backwards
for (auto i = length - 1; i >= 0; i--){
out[count[(a[i]/e)%10]-1] = a[i];
count[(a[i]/e)%10]--;
}
// copy the output array to the sorting array.
// now the sorting array contains sorted numbers, according to the current digit
for (auto i = 0; i < length; i++)
a[i] = out[i];
}
/**
* Sort array using Radix Sort
* @param a array to sort
* @param length size of the array
*/
void SortingAlgos::radixSort(int a[], int length) {
// Find the number with the max value in the array, then we know the number of digits
int max = getMax(a, length);
// Count sort for every digit. We pass exponent, which is 10^i (where i is the current digit)
for (auto e = 1; max/e > 0; e *= 10)
countSort(a, length, e);
}