-
Notifications
You must be signed in to change notification settings - Fork 1
/
searchAndSort.c
279 lines (228 loc) · 6.88 KB
/
searchAndSort.c
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
#include <stdio.h>
#include <stdlib.h>
/*
Sabrina Rispin, Jun 2017
binSearch, bubbleSort and quickSort modified versions
of Andrew Taylor's COMP1511 S1 2017 code
*/
int linearSearch(int len, int array[], int find);
int binarySearch(int len, int array[], int find);
void bubblesort(int length, int array[]);
void printBubbleSort(int len, int array[], int pos);
void printArray(int len, int array[]);
void printSubArray(int start, int end, int array[]);
void quicksort(int array[], int length);
void quicksort1(int array[], int lo, int hi, int *skip);
int partition(int array[], int lo, int hi);
void pause(int *skip);
int main(int argc, char *argv[]) {
// a simple search example
int sortedArray[10] = {13, 21, 32, 47, 50, 61, 66, 85, 90, 99};
linearSearch(10, sortedArray, 61);
binarySearch(10, sortedArray, 61);
// unsorted array
int unsortedArray[10] = {50, 32, 99, 66, 85, 13, 21, 47, 90, 61};
linearSearch(10, unsortedArray, 61); // will work
binarySearch(10, unsortedArray, 61); // won't work (why?)
bubblesort(10, unsortedArray); // sort array
binarySearch(10, unsortedArray, 61); // binary search works!
int array[10] = {50, 32, 99, 66, 85, 13, 21, 47, 90, 61};
quicksort(array, 10); // quicksort example
return EXIT_SUCCESS;
}
// take in an array + its length as well as a value to search for
// return the index in the array of that value
// returns -1 if not found
int linearSearch(int len, int array[], int find) {
printf("LINEAR SEARCH for %d in ", find);
printArray(len, array);
printf("Press ENTER to step through, enter s to skip: ");
int skip = getchar() == 's' ? 1 : 0;
if (skip) {
getchar();
}
char newline = skip ? '\n' : ' ';
int i = 0;
while (i < len) {
printf("ind: %d, val: %d%c", i, array[i], newline);
if (array[i] == find) {
if (!skip) {
printf("\n");
}
printf("\n%d was found at index %d!\n\n", find, i);
return i;
}
// wait for user to hit enter
pause(&skip);
i++;
}
printf("\n%d was not found in the array\n\n", find);
return -1;
}
int binarySearch(int len, int array[], int find) {
printf("BINARY SEARCH for %d in ", find);
printArray(len, array);
printf("Press ENTER to step through, enter s to (s)kip: ");
int skip = getchar() == 's' ? 1 : 0;
if (skip) {
getchar();
}
char newline = skip ? '\n' : ' ';
int lower = 0;
int upper = len - 1;
while (lower <= upper) {
int mid = (lower + upper)/ 2;
char compare = array[mid] < find ? '<' : '>';
if (array[mid] == find) {
compare = '=';
}
printf("mid ind: %d, val: %d, %d %c %d\t", mid, array[mid], array[mid], compare, find);
if (array[mid] == find) {
printf("%d was found at index %d!\n\n", find, mid);
return mid;
} else if (array[mid] > find) {
printf("Searching lower half...%c", newline);
upper = mid - 1;
} else {
printf("Searching upper half...%c", newline);
lower = mid + 1;
}
// wait for user to hit enter
pause(&skip);
}
printf("\n%d was not found in the array\n\n", find);
return -1;
}
void bubblesort(int length, int array[]) {
printf("BUBBLE SORT ");
printArray(length, array);
printf("Press ENTER to step through, enter s to (s)kip: ");
int skip = getchar() == 's' ? 1 : 0;
if (skip) {
getchar();
}
int swapped = 1;
while (swapped) {
swapped = 0;
for (int i = 1; i < length; i = i + 1) {
printBubbleSort(length, array, i);
if (array[i] < array[i - 1]) {
int tmp = array[i];
array[i] = array[i - 1];
array[i - 1] = tmp;
swapped = 1;
}
// wait for user to hit enter
pause(&skip);
}
}
}
void printBubbleSort(int len, int array[], int pos) {
printArray(len, array);
int i = 0;
while (i < len) {
if (i == pos) {
printf(" |%s| ", array[i] > array[i+1] ? "swap" : " ");
} else if (i + 1 != pos) {
printf(" ");
}
i++;
}
printf("\n\n");
}
void printArray(int len, int array[]) {
printf("[%d", array[0]);
int i = 1;
while (i < len) {
printf(", %2d", array[i]);
i++;
}
printf("]\n");
}
void printSubArray(int start, int end, int array[]) {
printf("[%d", array[start]);
int i = start+1;
while (i < end+1) {
printf(", %2d", array[i]);
i++;
}
printf("]\n");
}
void quicksort(int array[], int length) {
printf("QUICKSORT ");
printArray(length, array);
printf("Press ENTER to step through, enter s to (s)kip: ");
int skip = getchar() == 's' ? 1 : 0;
if (skip) {
getchar();
}
quicksort1(array, 0, length - 1, &skip);
printf("\n\nFinal Array: ");
printArray(length, array);
}
void quicksort1(int array[], int lo, int hi, int *skip) {
if (lo >= hi) {
return;
}
// rearrange array so that
// all elements smaller than pivot value are below it and
// all element larger than pivot value are above it
printf("\nArray before partition: ");
printSubArray(lo, hi, array);
int p = partition(array, lo, hi);
printf("Array after partition: ");
printSubArray(lo, hi, array);
// wait for user to hit enter
pause(skip);
// sort lower part of array
printf("\nQuicksort below pivot: ");
printSubArray(lo, p, array);
quicksort1(array, lo, p, skip);
// wait for user to hit enter
pause(skip);
// sort upper part of array
printf("\nQuicksort above pivot: ");
printSubArray(p+1, hi, array);
quicksort1(array, p + 1, hi, skip);
printf("\nCombining sorted halves:\n");
printSubArray(lo, hi, array);
}
int partition(int array[], int lo, int hi) {
int i = lo;
int j = hi;
// use middle value as pivot
int pivotValue = array[(lo + hi) / 2];
printf("Pivot value: %d\n", pivotValue);
// start from left and right ends
while (1) {
// Look for pair to swap
while (array[i] < pivotValue) {
i = i + 1;
}
while (array[j] > pivotValue) {
j = j - 1;
}
// No pair to swap so return
if (i >= j) {
return j;
}
// and swap them over
int temp = array[i];
array[i] = array[j];
array[j] = temp;
i = i + 1;
j = j - 1;
}
}
void pause(int *skip) {
if (!*skip) {
char c = getchar();
while (c != '\n') {
if (c == 's') {
*skip = 1;
c = '\n';
}
c = getchar();
};
}
}