-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_functions.h
228 lines (203 loc) · 6.82 KB
/
sort_functions.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
#include "SDL_functions.h"
using namespace std;
void swap(int &a, int &b) {
int tmp = a;
a = b;
b = tmp;
}
bool bubble_sort(animation_data &ad, int* a) {
for (int j = 1; j < ad.size; j++) {
for (int i = 0; i < ad.size - 1; i++) {
if (a[i] > a[i + 1]) {
swap(a[i], a[i + 1]);
if (!draw_array(ad, a, i + 1)) return false;
}
}
}
draw_array(ad, a, -1);
return true;
}
bool selection_sort(animation_data &ad, int* a) {
for (int j = 0; j < ad.size - 1; j++) {
int min = a[j];
int min_index;
for (int i = j + 1; i < ad.size; i++) {
if (a[i] < min) {
min = a[i];
min_index = i;
if (!draw_array(ad, a, min_index)) return false;
}
}
swap(a[j], a[min_index]);
}
draw_array(ad, a, -1);
return true;
}
bool insertion_sort(animation_data &ad, int* a) {
for (int j = 1; j < ad.size; j++) {
int inserting = j;
for (int i = inserting - 1; i >= 0; i--) { // check the elements before inserting
if (a[i] > a[inserting]) { // if the element before inserting is greater, swap them
swap(a[i], a[inserting]);
inserting = i;
if (!draw_array(ad, a, inserting)) return false;
}
else { // inserting is in the correct spot
break;
}
}
}
draw_array(ad, a, -1);
return true;
}
bool merge(animation_data &ad, int* a, int left_index, int middle_index, int right_index) {
// create two arrays for the left and right
int left_size = middle_index - left_index + 1;
int right_size = right_index - middle_index;
int left_array[left_size];
int right_array[right_size];
for (int i = 0; i < left_size; i++) {
left_array[i] = a[left_index + i];
}
for (int i = 0; i < right_size; i++) {
right_array[i] = a[middle_index + i + 1];
}
// merge temporary arrays back into a
int left_curr = 0;
int right_curr = 0;
int merged_curr = left_index;
// take the smaller element and put it into merged
while ((left_curr < left_size) && (right_curr < right_size)) {
if (left_array[left_curr] <= right_array[right_curr]) {
a[merged_curr] = left_array[left_curr];
left_curr++;
}
else {
a[merged_curr] = right_array[right_curr];
right_curr++;
}
if (!draw_array(ad, a, merged_curr)) return false;
merged_curr++;
}
// copy the rest of left array if necesssary
while (left_curr < left_size) {
a[merged_curr] = left_array[left_curr];
left_curr++;
merged_curr++;
}
// copy the left of right array if necessary
while (right_curr < right_size) {
a[merged_curr] = right_array[right_curr];
right_curr++;
merged_curr++;
}
return true;
}
bool merge_sort(animation_data &ad, int* a, int left_index, int right_index) {
if (left_index < right_index) {
int middle_index = left_index + (right_index - left_index) / 2;
if (!merge_sort(ad, a, left_index, middle_index)) return false;
if (!merge_sort(ad, a, middle_index + 1, right_index)) return false;
if (!merge(ad, a, left_index, middle_index, right_index)) return false;
}
draw_array(ad, a, -1);
return true;
}
int partition(animation_data &ad, int* a, int left_index, int right_index, int &return_pivot_index) {
// set rightmost index as pivot
int pivot_index = right_index;
int store_index = left_index;
for (int i = left_index; i < right_index; i++) {
if (a[i] <= a[pivot_index]) {
swap(a[i], a[store_index]);
store_index++;
if (!draw_array(ad, a, store_index)) return false;
}
}
swap(a[right_index], a[store_index]);
if (!draw_array(ad, a, store_index)) return false;
return_pivot_index = store_index;
return true;
}
bool quick_sort(animation_data &ad, int* a, int left_index, int right_index) {
if (left_index < right_index) {
// choose pivot, partition the array, and call quicksort on each partition
int pivot_index;
if (!partition(ad, a, left_index, right_index, pivot_index)) return false;
if (!quick_sort(ad, a, left_index, pivot_index - 1)) return false;
if (!quick_sort(ad, a, pivot_index + 1, right_index)) return false;
}
draw_array(ad, a, -1);
return true;
}
bool counting_sort(animation_data &ad, int* a) {
// copy a into output for animation
int* output = new int[ad.size];
memcpy(output, a, sizeof(int) * ad.size);
// find max
int max = a[0];
for (int i = 1; i < ad.size; i++) {
if (a[i] > max) {
max = a[i];
}
}
max = max + 1;
// create count array to store the count of each element
int count[max] = {0};
for (int i = 0; i < ad.size; i++) {
count[a[i]]++;
if (!draw_array(ad, a, i)) return false;
}
// take cumulative sum
for (int i = 1; i <= max; i++) {
count[i] += count[i - 1];
}
// do the sorting magic
for (int i = ad.size - 1; i >= 0; i--) {
output[count[a[i]] - 1] = a[i];
if (!draw_array(ad, output, count[a[i] - 1])) return false;
count[a[i]]--;
}
// copy output into a
for (int i = 0; i < ad.size; i++) {
a[i] = output[i];
}
draw_array(ad, a, -1);
return true;
}
bool shell_sort(animation_data &ad, int* a) {
for (int interval = ad.size / 2; interval > 0; interval = interval / 2) {
for (int i = interval; i < ad.size; i++) {
int tmp = a[i];
int j;
for (j = i; (j >= interval) && (a[j - interval] > tmp); j = j - interval) {
a[j] = a[j - interval];
if (!draw_array(ad, a, j)) return false;
}
a[j] = tmp;
if (!draw_array(ad, a, j)) return false;
}
}
draw_array(ad, a, -1);
return true;
}
bool bogo_sort(animation_data &ad, int* a) {
bool sorted = false;
while (!sorted) {
// generate permutation of the array (Fisher-Yates shuffling)
for (int i = ad.size - 1; i >= 0; i--) {
int j = rand() % (i + 1);
swap(a[i], a[j]);
}
if (!draw_array(ad, a, -1)) return false;
// check if permutation is sorted
sorted = true;
for (int i = 0; i < ad.size - 1; i++) {
if (a[i] > a[i + 1]) {
sorted = false;
break;
}
}
}
return true;
}