-
Notifications
You must be signed in to change notification settings - Fork 1
/
visualizer.cpp
652 lines (597 loc) · 16 KB
/
visualizer.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
//
// Created by Sharan on 27/02/20.
// Copyright © 2020 Sharan. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif
#define SORT_NO 5 // Number of sorting algorithms
#define MAX 45 // Number of values in the array
#define COLORS 90 // Max Color Length
/*~~Global Variables~~*/
int a[MAX]; // Sorting Array
double cc[COLORS]; // Color Array
char text1[5];
char text2[5];
int swapflag = 0; // Flag to check if swapping has occured
int i = 0, j = 0; // To iterate through the array
int flag = 0; // For Insertion Sort
int dirflag = 0; // For Ripple Sort, to change direction at the ends
int count = 1; // For Ripple Sort, to keep count of how many are sorted at the end
int sorting = 0; // 1 if Sorted
const char *sort_string[] = {"Bubble Sort", "Selection Sort", "Insertion Sort", "Ripple Sort", "Comb Sort"};
int sort_count = 0; // To cycle through the string
int speed = 10; // Speed of sorting
int arrayFlag = 1;
int backFlag = 1;
int fastslow = 0;
int vertical_index;
/*{~~Function Declarations~~*/
double randomFlaot();
void bitmap_output(int, int, const char *, void *);
void makedelay(int);
void display_text();
void frontPage();
void Initialize();
void display();
void int_str(int rad, char r[]);
int notsorted();
void keyboard(unsigned char key, int x, int y);
void ripplesort();
void bubblesort();
void selectionsort();
void insertionsort();
char *whichmode(char k);
void quickSort(int low, int high);
int partition(int low, int high);
void swap(int *a, int *b);
void combSort();
void shellSort();
/*~~Function Definations~~*/
// Function Definaions
char* whichmode(const int d)
{
if (d == 1)
{
return "Interactive Mode"; /* code */
}
return "Outstanding Mode";
}
// To get random number between 0.0 to 1.0
double randomFlaot()
{
return double(rand()) / (double(RAND_MAX) + 1.0);
}
// Function to display text on screen char by char
void bitmap_output(int x, int y, const char *string, void *font)
{
int len, i;
glRasterPos2f(x, y);
len = (int)strlen(string);
for (i = 0; i < len; i++)
{
glutBitmapCharacter(font, string[i]);
}
}
// Function to integer to string
void int_str(int rad, char r[])
{
snprintf(r, 10, "%d", rad);
}
// Home Screen Static Texts
void display_text()
{
glColor3d(cc[36], cc[37], cc[38]);
bitmap_output(250, 750, "SORTING ALGORITHM VISUALIZER", GLUT_BITMAP_TIMES_ROMAN_24);
// glBegin(GL_LINE_LOOP);
// glVertex2f(145, 700);
// glVertex2f(520, 700);
// glEnd();
char text[20]; // other text small font
glColor3d(cc[39], cc[40], cc[41]);
bitmap_output(15, 700, "This program sorts a random set of integers in ascending order by displaying them graphically", GLUT_BITMAP_HELVETICA_18);
bitmap_output(15, 680, "as bars with varying height", GLUT_BITMAP_HELVETICA_18);
// glColor3f(0.5, 0.5, 0.5);
glColor3d(cc[42], cc[43], cc[44]);
int_str(speed, text);
strcat(text, "ms");
// bitmap_output(620, 510, "Speed:", GLUT_BITMAP_HELVETICA_18);
// strcat(mode, "Mode");
bitmap_output(600, 510, whichmode(fastslow), GLUT_BITMAP_HELVETICA_18);
// bitmap_output(650, 510, text, GLUT_BITMAP_HELVETICA_18);
char iff[50] = "swapping ";
// strcat(texts, " swapping ");
strcat(iff, text1);
strcat(iff, " and ");
strcat(iff, text2);
glBegin(GL_LINE_LOOP);
glColor3d(cc[42], cc[43], cc[44]);
glVertex3f(10.0f, 740.0f, 0.0f);
glColor3d(cc[45], cc[46], cc[47]);
glVertex3f(690.0f, 740.0f, 0.0f);
glColor3d(cc[48], cc[49], cc[50]);
glVertex3f(690.0f, 500.0f, 0.0f);
glColor3d(cc[51], cc[52], cc[53]);
glVertex3f(10.0f, 500.0f, 0.0f);
glEnd();
glBegin(GL_LINES);
glColor3d(cc[47], cc[52], cc[10]);
glVertex2f(450.0, 740.0);
glColor3d(cc[42], cc[17], cc[74]);
glVertex2f(450.0, 500.0);
glEnd();
glColor3d(cc[56], cc[77], cc[36]);
bitmap_output(550.0, 700.0, "STEP", GLUT_BITMAP_TIMES_ROMAN_24);
glColor3d(cc[34], cc[58], cc[28]);
// bitmap_output(470, 650, texts, GLUT_BITMAP_HELVETICA_18);
bitmap_output(470, 600, iff, GLUT_BITMAP_HELVETICA_18);
glColor3d(cc[54], cc[55], cc[56]);
if (sorting == 0) // if not sorting display menu
{
bitmap_output(15, 645, "MENU", GLUT_BITMAP_TIMES_ROMAN_24);
bitmap_output(15, 625, "Press s to SORT", GLUT_BITMAP_HELVETICA_18);
bitmap_output(15, 605, "Press c to SELECT the sort algorithm", GLUT_BITMAP_HELVETICA_18);
bitmap_output(15, 585, "Press r to RANDOMISE", GLUT_BITMAP_HELVETICA_18);
bitmap_output(15, 565, "Esc to QUIT", GLUT_BITMAP_HELVETICA_18);
bitmap_output(15, 545, "Selected sort: ", GLUT_BITMAP_HELVETICA_18);
bitmap_output(80, 545, *(sort_string + sort_count), GLUT_BITMAP_HELVETICA_18);
}
else if (sorting == 1) // while sorting
{
glColor3d(cc[57], cc[58], cc[59]);
bitmap_output(55, 555, "Sorting in progress...", GLUT_BITMAP_HELVETICA_18);
glColor3d(cc[60], cc[61], cc[62]);
bitmap_output(55, 535, "Press p to PAUSE", GLUT_BITMAP_HELVETICA_18);
}
}
// Initailization of values also call whenever r key is pressed
void Initialize()
{
srand(time(0)); // Use current time as seed for random generator
int temp1;
if (arrayFlag == 1)
{
// Reset the array
for (temp1 = 0; temp1 < MAX; temp1++)
{
a[temp1] = rand() % 100 + 1;
// printf("%d ", a[temp1]);
}
arrayFlag = 0;
}
if (backFlag == 1)
{
for (int i = 0; i < COLORS; i++)
{
cc[i] = randomFlaot();
}
// Reset all values
i = j = 0;
dirflag = 0;
count = 1;
flag = 0;
glClearColor(randomFlaot(), randomFlaot(), randomFlaot(), randomFlaot());
backFlag = 0;
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 699, 0, 799);
}
// check elements before calling sorting functions
int notsorted()
{
int q;
for (q = 0; q < MAX - 1; q++)
{
if (a[q] > a[q + 1])
return 1; // Return 1 if not sorted
}
return 0;
}
// Main function for display
void display()
{
int ix, temp;
glClear(GL_COLOR_BUFFER_BIT);
display_text();
char text[10];
for (ix = 0; ix < MAX; ix++)
{
// glColor4d(1, 0, 0, 0);
glBegin(GL_LINE_LOOP);
glColor3d(cc[63], cc[64], cc[65]);
glVertex2f(10 + (700 / (MAX + 1)) * ix, 50);
glColor3d(cc[66], cc[67], cc[68]);
glVertex2f(10 + (700 / (MAX + 1)) * (ix + 1), 50);
glColor3d(cc[69], cc[70], cc[71]);
glVertex2f(10 + (700 / (MAX + 1)) * (ix + 1), 50 + a[ix] * 4);
glColor3d(cc[72], cc[73], cc[74]);
glVertex2f(10 + (700 / (MAX + 1)) * ix, 50 + a[ix] * 4);
glEnd();
int_str(a[ix], text);
// printf("\n%s", text);
glColor3d(cc[75], cc[76], cc[77]);
bitmap_output(12 + (700 / (MAX + 1)) * ix, 30, text, GLUT_BITMAP_HELVETICA_18);
}
if (swapflag == 1 || sorting == 0)
{
glBegin(GL_POLYGON);
glColor3d(cc[75], cc[76], cc[77]);
glColor3d(cc[78], cc[79], cc[80]);
glVertex2f(10 + (700 / (MAX + 1)) * vertical_index, 50);
glColor3d(cc[81], cc[82], cc[83]);
glVertex2f(10 + (700 / (MAX + 1)) * (vertical_index + 1), 50);
glColor3d(cc[84], cc[85], cc[86]);
glVertex2f(10 + (700 / (MAX + 1)) * (vertical_index + 1), 50 + a[vertical_index] * 4);
glColor3d(cc[87], cc[88], cc[89]);
glVertex2f(10 + (700 / (MAX + 1)) * vertical_index, 50 + a[vertical_index] * 4);
glEnd();
swapflag = 0;
}
glFlush();
}
// Timer Function, takes care of sort selection
void makedelay(int)
{
if (sorting)
{
switch (sort_count)
{
case 0:
bubblesort();
break;
case 1:
selectionsort();
break;
case 2:
insertionsort();
break;
case 3:
ripplesort();
break;
case 4:
combSort();
break;
}
}
if (fastslow == 1)
{
sorting = 0;
}
speed = 1;
glutPostRedisplay();
glutTimerFunc(speed, makedelay, 100);
}
// Keyboard Function
void keyboard(unsigned char key, int x, int y)
{
// if (key == 13)
// {
// k = 1;
// Initialize();
// }
// else
if (key == 'k')
{
speed = 50;
}
else if (sorting != 1)
{
switch (key)
{
case 27:
exit(0); // 27 is the ascii code for the ESC key
case 's':
sorting = 1;
break;
case 'i':
fastslow = 1;
break;
case 'o':
fastslow = 0;
break;
case 'r':
arrayFlag = 1;
Initialize();
break;
case 'x':
backFlag = 1;
arrayFlag = 0;
Initialize();
case 'z':
arrayFlag = 1;
backFlag = 1;
Initialize();
break;
case 'c':
sort_count = (sort_count + 1) % SORT_NO;
break;
}
}
else if (sorting == 1)
{
if (key == 'p')
sorting = 0;
speed = 1000;
}
else
Initialize();
}
// Insertion Sort
void insertionsort()
{
int temp;
while (i < MAX)
{
if (flag == 0)
{
j = i;
flag = 1;
}
while (j < MAX - 1)
{
if (a[j] > a[j + 1])
{
swapflag = 1;
temp = a[j];
printf("inside while if ");
a[j] = a[j + 1];
a[j + 1] = temp;
vertical_index = j;
goto A;
}
j++;
if (j == MAX - 1)
{
flag = 0;
}
int_str(a[j], text1);
int_str(a[j + 1], text2);
printf("swap %d and %d\n", a[j], a[j + 1]);
}
i++;
}
sorting = 0;
A:
i = j = 0;
}
// Selection Sort
void selectionsort()
{
int temp, j, min, pos;
while (notsorted())
{
while (i < MAX - 1)
{
min = a[i + 1];
pos = i + 1;
if (i != MAX - 1)
{
for (j = i + 2; j < MAX; j++)
{
if (min > a[j])
{
min = a[j];
vertical_index = i;
pos = j;
}
}
}
printf("\ni=%d min=%d at %d", i, min, pos);
printf("\nchecking %d and %d", min, a[i]);
if (min < a[i])
{
//j=pos;
int_str(min, text1);
int_str(a[i], text2);
printf("\nswapping %d and %d", min, a[i]);
temp = a[pos];
a[pos] = a[i];
a[i] = temp;
// vertical_index = min;
swapflag = 1; //change
goto A;
}
i++;
}
}
sorting = 0;
i = j = 0;
A:
printf("");
}
//Bubble Sort
void bubblesort()
{
int temp;
while (notsorted())
{
while (j < MAX - 1)
{
if (a[j] > a[j + 1])
{
swapflag = 1;
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
vertical_index = j + 1;
int_str(a[j], text1);
int_str(a[j + 1], text2);
goto A;
}
j++;
if (j == MAX - 1)
j = 0;
printf("swap %d and %d\n", a[j], a[j + 1]);
}
}
sorting = 0;
A:
printf("");
}
//Ripple Sort
void ripplesort()
{
int temp;
while (notsorted() && sorting)
{
if (dirflag == 0)
{
while (j < MAX - 1)
{
if (a[j] > a[j + 1])
{
swapflag = 1;
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
vertical_index = j + 1;
goto A;
}
j++;
if (j == MAX - 1)
{
count++;
j = MAX - count;
dirflag = 1 - dirflag;
}
printf("j=%d forward swap %d and %d\n", j, a[j], a[j + 1]);
int_str(a[j], text1);
int_str(a[j + 1], text2);
}
}
else
{
while (j >= 0)
{
if (a[j] > a[j + 1])
{
swapflag = 1;
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
vertical_index = j + 1;
goto A;
}
j--;
if (j == 0)
{
dirflag = 1 - dirflag;
}
printf("j=%d backward swap %d and %d\n", j, a[j], a[j + 1]);
int_str(a[j], text1);
int_str(a[j + 1], text2);
}
}
}
sorting = 0;
A:
printf("");
}
//=========------------==================------------
// void selectionsort()
// {
// int i, j, min_idx;
// while (notsorted())
// {
// // One by one move boundary of unsorted subarray
// for (i = 0; i < MAX - 1; i++)
// {
// // Find the minimum element in unsorted array
// min_idx = i;
// for (j = i + 1; j < MAX; j++)
// if (a[j] < a[min_idx])
// min_idx = j;
// // Swap the found minimum element with the first element
// swap(&a[min_idx], &a[i]);
// swapflag = 1;
// vertical_index = min_idx;
// goto A;
// }
// }
// sorting = 0;
// A:
// printf("");
// }
// To find gap between elements
int getNextGap(int gap)
{
// Shrink gap by Shrink factor
gap = (gap * 10) / 13;
if (gap < 1)
return 1;
return gap;
}
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
// Function to sort a[0..n-1] using Comb Sort
void combSort()
{
// Initialize gap
int gap = MAX;
// Initialize swapped as true to make sure that
// loop runs
bool swapped = true;
// Keep running while gap is more than 1 and last
// iteration caused a swap
while (notsorted())
{
printf("not done");
while (gap != 1 || swapped == true)
{
// Find next gap
gap = getNextGap(gap);
// Initialize swapped as false so that we can
// check if swap happened or not
swapped = false;
// Compare all elements with current gap
for (int j = 0; j < MAX - gap; j++)
{
if (a[j] > a[j + gap])
{
swap(&a[j], &a[j + gap]);
swapflag = 1;
swapped = true;
vertical_index = j + gap;
int_str(a[j], text1);
int_str(a[j + gap], text2);
goto A;
}
}
}
}
sorting = 0;
A:
printf("");
}
// Main Function
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(2000, 1000);
glutInitWindowPosition(0, 0);
glutCreateWindow("SORTING ALGORITHM VISUALIZER");
Initialize();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutTimerFunc(1000, makedelay, 1);
glutMainLoop();
}