-
Notifications
You must be signed in to change notification settings - Fork 1
/
insertion.c
322 lines (290 loc) · 7.6 KB
/
insertion.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
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
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
// printf ANSI colors
#define COLOR_RED "\x1b[31m"
#define COLOR_GREEN "\x1b[32m"
#define COLOR_YELLOW "\x1b[33m"
#define COLOR_BLUE "\x1b[34m"
#define COLOR_MAGENTA "\x1b[35m"
#define COLOR_CYAN "\x1b[36m"
#define COLOR_RESET "\x1b[0m"
// constants
#define MAX_LEN 100
#define MAX_DELAY 1000
#define RANDOM_SIZE_FACTOR 10
#define HIDE -10 // for done/source/slot/dest
// strings
#define VERBOSE_STRING "-v"
// globals with default values for user inputs
static int delay_milliseconds = 0;
static bool verbose = false;
// prototypes
bool is_valid(int argc, string argv[]);
void randomize_array(int len, int array[]);
void delay_ms(int duration);
void print_array(int len, int array[], int done, int source,
int slot, int dest);
void sort_array(int len, int array[]);
void insert(int array[], int source, int dest);
/*
* MAIN
*/
int main(int argc, string argv[])
{
// bad input
if (!is_valid(argc, argv))
{
printf(COLOR_RED);
printf("Usage: insertion <size 1-%i> ", MAX_LEN);
printf("<delay ms 1-%i> \n", MAX_DELAY);
printf(COLOR_RESET);
// failure
return 1;
}
// no buffer for printed text, allowing partial lines
setbuf(stdout, NULL);
// good input
int len = atoi(argv[1]);
int data[len];
randomize_array(len, data);
sort_array(len, data);
// success
return 0;
}
/*
* Check command line inputs
*/
bool is_valid(int argc, string argv[])
{
// allow 2-4 arguments only
if (argc < 2 || argc > 4)
{
return false;
}
// require valid array length as second argument
int len = atoi(argv[1]);
// outside of the acceptable range
if (len < 1 || len > MAX_LEN)
{
return false;
}
// third argument is present
if (argc > 2)
{
// optional argument for delay
int delay = atoi(argv[2]);
// outside of the acceptible range
if (delay < 1 || delay > MAX_DELAY)
{
return false;
}
// valid; override the global
else
{
delay_milliseconds = delay;
}
}
// fourth argument is present
if (argc > 3)
{
// optional argument for verbose
string output = argv[3];
// doesn't match the expected input
if (strcmp(output, VERBOSE_STRING) != 0)
{
return false;
}
// valid; override the global
else
{
verbose = output;
}
}
//success
return true;
}
/*
* Fill array with random numbers
*/
void randomize_array(int len, int array[])
{
// random seed based on time
srand(time(NULL));
for (int i = 0; i < len; i++)
{
int value = rand() % (len * RANDOM_SIZE_FACTOR);
array[i] = value;
}
}
/*
* Delay for a given number of milliseconds
*/
void delay_ms(int delay_duration)
{
clock_t start = clock();
clock_t now = clock();
// convert to milliseconds
clock_t wait = delay_duration * (CLOCKS_PER_SEC / 1000);
while (now < start + wait)
{
// update time
now = clock();
}
}
/*
* Print out the current state of the sort
*/
void print_array(int len, int array[], int done, int source,
int slot, int dest)
{
// clear the screen if not verbose
if (!verbose)
{
system("clear");
}
// print one extra to enable last slot bracket at the end
for (int i = 0; i <= len; i++)
{
// brackets
if (i == slot)
{
printf(COLOR_YELLOW);
printf("[");
printf(COLOR_RESET);
}
// hide brackets when slot starts as -1
else if (i == slot + 1 && slot >= 0)
{
printf(COLOR_YELLOW);
printf("]");
printf(COLOR_RESET);
}
else
{
printf(" ");
}
// colors for the element
if (i < done)
{
printf(COLOR_GREEN);
// shifted value color supercedes done color
if (i > slot && slot >= 0)
{
printf(COLOR_YELLOW);
}
if (i >= dest && dest >=0)
{
printf(COLOR_YELLOW);
}
}
// has to be if instead of else if so source color
// can supercede dest color after source is inserted
if (i == source)
{
printf(COLOR_RED);
// dest color supercedes done color
if (i == dest)
{
printf(COLOR_YELLOW);
}
}
// skip very last one; only included for brackets
if (i < len)
{
// actually print the element
printf("%i", array[i]);
}
printf(COLOR_RESET);
}
// print new line
printf("\n");
// pause to display data
delay_ms(delay_milliseconds);
}
/*
* Insert source at destination and shift bounded values
*/
void insert(int array[], int source, int dest)
{
// value not actually moving; do nothing
if (source == dest)
{
return;
}
// store source in temp
int temp = array[source];
// for values between source and dest
for (int i = source; i > dest; i--)
{
// shift values one to the right
array[i] = array[i - 1];
}
// once i == dest (default)
// restore dest from temp
array[dest] = temp;
}
/*
* Insertion sort
*/
void sort_array(int len, int array[])
{
// show unsorted array, no done/source/slot/dest
print_array(len, array, HIDE, HIDE, HIDE, HIDE);
// build the outer loop to check each source
// first value is always done by default on first pass
for (int source = 1; source < len; source++)
{
int done = source;
int slot = source;
// show start of pass with done, no source/slot/dest
print_array(len, array, done, HIDE, HIDE, HIDE);
// loop through slots to find the dest for this source
while (slot >= 0)
{
// show done/source/slot
print_array(len, array, done, source, slot, HIDE);
// found a smaller value; can insert
if (array[slot - 1] <= array[source])
{
// show done/source/slot/dest
print_array(len, array, done, source, slot,
slot);
// insert at one slot to the right
insert(array, source, slot);
// don't redraw if source moved to same spot
if (slot != source)
{
// show finished pass with swapped values
// show done/source, no slot/dest
print_array(len, array, done + 1, slot, HIDE,
HIDE);
}
// done!
break;
}
// ran out of slots; insert 0
else if (slot == 0)
{
// show done/source/slot/dest
print_array(len, array, done, source, slot,
slot);
//insert at the beginning
insert(array, source, 0);
// done!
break;
}
// keep looking by continuing the loop
else
{
// move one more slot to the left
slot--;
}
}
}
// show all values as done, no source/slot/dest
print_array(len, array, len, HIDE, HIDE, HIDE);
// show sorted array, no done/source/slot/dest
print_array(len, array, HIDE, HIDE, HIDE, HIDE);
}