-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualgo.cpp
More file actions
647 lines (567 loc) · 13.4 KB
/
visualgo.cpp
File metadata and controls
647 lines (567 loc) · 13.4 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
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
// ---------------------------------------------------***** VISUAL ALGO *****-----------------------------//
// HEADERS
#include <graphics.h>
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <windows.h>
// BUBBLE SORT MACROS
#define SIZE 10
//INSERTION SORT MACROS
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define IntiSize 10
// BUBBLE SORT FUCNTION DEFINITIONS/PROTOTYPES
void object(int, int, int, int, int);
void bubble(int);
void iterations(int, int, int, int);
void mixing(int, int);
void gotoxy(int, int);
void displayBubble();
int el[SIZE]; // ARRAY
// INSERTION SORT FUCNTION DEFINITIONS/PROTOTYPES
void element(int, int, int, int, int);
void InsertionSort(int);
void rows(int, int, int, int, bool, int);
void FinalArray(int, int, int);
void displayInsertion();
int ElementsArray[IntiSize]; // ARRAY
int main()
{
int select;
int gd = DETECT, gm;
printf("******* WELCOME TO VISUALGO ******** \n\n");
printf("WE HAVE FOLLOWING VISUALIZATIONS AVALIABLE FOR YOU\n");
printf("1 - LINEAR SEARCH\n");
printf("2 - BINARY SEARCH\n");
printf("3 - BUBBLE SORT\n");
printf("4 - INSERTION SORT\n");
printf("Enter the Choice - ");
scanf("%d", &select);
cleardevice();
// -------------------------------------------------------------------------------- Linear Search Algorithm ------------------------- //
if (select == 1)
{
int i, j, gm;
gd = DETECT;
int x1, y1, x2, y2;
int aim; // stores the number to be seached for
int index;
int gap; //index for storing number of elements
int counter = 0;
char result[index + 1], resultpos[1];
printf("\n");
printf("Enter the number of elements \n");
scanf("%d", &index);
printf("Enter the elements \n");
int elements[index]; // for storing the elements entered by the user
int pos[index], posCount = 0;
for (int z = 0; z < index; z++)
{
scanf("%d", &elements[z]);
}
printf("Enter the Number to searched \n");
scanf("%d", &aim);
cleardevice();
initgraph(&gd, &gm, "C:\\TC\\BGI");
settextstyle(DEFAULT_FONT, HORIZ_DIR, 5);
outtextxy(25, 50, "LINEAR SEARCH");
getch();
settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
for (i = 0; counter <= index; i = i + 70)
{
setcolor(4); // color of rectangle set to RED
rectangle(100 + i, 250, 150 + i, 200); // creating rectangles
setcolor(15); // color of rectangle is set to white
delay(1000);
sprintf(result, "%d", elements[counter]); // changing integer values from elements, to string and storing in in result
if (elements[counter] < 9)
{ // check for better indentation on display
outtextxy(100 + i + 20, 215, result);
}
else if (elements[counter] > 9)
{ // check for better indentation on display
outtextxy(100 + i + 12, 215, result);
}
else
{
outtextxy(100 + i + 15, 215, result); // // check for better indentation on display
}
counter++;
if (counter == index)
{
if (aim == elements[counter - 1])
{
setcolor(2); // setting rectangle color to green
rectangle(100 + i, 250, 150 + i, 200);
posCount++;
pos[posCount] = elements[counter];
}
if (aim != elements[counter - 1])
{
setcolor(15); // setting rectangle color to white
rectangle(100 + i, 250, 150 + i, 200);
}
break;
}
else if (aim == elements[counter - 1])
{
setcolor(2); // setting rectangle color to green
rectangle(100 + i, 250, 150 + i, 200);
posCount++;
pos[posCount] = elements[counter];
continue;
}
else
{
setcolor(15); // setting rectangle color to white
rectangle(100 + i, 250, 150 + i, 200);
}
setcolor(15); // setting rectangle color to white
rectangle(100 + i, 250, 150 + i, 200);
delay(500);
}
delay(300);
outtextxy(50, 280, "Number of matches : ");
sprintf(resultpos, "%d", posCount);
outtextxy(70, 300, resultpos);
getch();
closegraph();
return 0;
}
// ------------------------------------------------------------------- BINARY SEARCH ALGORITHM ----------------------------------- //
else if (select == 2)
{
int i, j, gd = DETECT, gm;
int x1, y1, x2, y2;
int aim; // stores the number to be seached for
int index;
int gap; //index for storing number of elements
int counter = 0;
printf("\n");
printf("Enter the number of elements \n");
scanf("%d", &index);
printf("Enter the elements \n");
int d = 0;
int elements[index]; // for storing the elements entered by the user
int pos[index], posCount = 0;
char result[index + 1], resultpos[1];
char indexNoString[index + 1];
int start;
char startp[128];
int mid;
char midp[128];
int end;
char endp[128];
for (int z = 0; z < index; z++)
{
scanf("%d", &elements[z]);
}
start = 0;
end = index - 1;
printf("Enter the Number to searched \n");
scanf("%d", &aim);
cleardevice();
initwindow(800, 800);
//initgraph(&gd,&gm,"C:\\TC\\BGI");
settextstyle(DEFAULT_FONT, HORIZ_DIR, 5);
outtextxy(25, 50, "BINARY SEARCH");
getch();
settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
//TO BE REMOVED
outtextxy(50, 150, "START");
outtextxy(50, 200, "MID");
outtextxy(50, 250, "END");
outtextxy(50, 315, "ARRAY");
outtextxy(50, 375, "INDEX");
outtextxy(150, 150, ":");
outtextxy(150, 200, ":");
outtextxy(150, 250, ":");
outtextxy(150, 315, ":");
outtextxy(150, 375, ":");
for (i = 0; counter < index; i = i + 70)
{
setcolor(15); // color of rectangle is set to white
rectangle(180 + i, 350, 230 + i, 300); // creating rectangles
sprintf(result, "%d", elements[counter]);
sprintf(indexNoString, "%d", counter); // changing integer values from elements, to string and storing in in result
if (elements[counter] < 9)
{ // check for better indentation on display
outtextxy(180 + i + 20, 315, result);
outtextxy(180 + i + 20, 375, indexNoString);
}
else if (elements[counter] > 9)
{ // check for better indentation on display
outtextxy(180 + i + 12, 315, result);
outtextxy(180 + i + 12, 375, indexNoString);
}
else
{
outtextxy(180 + i + 15, 315, result);
outtextxy(180 + i + 15, 375, indexNoString);
// check for better indentation on display
}
counter++;
}
delay(500);
while (start <= end)
{
delay(400);
mid = (start + end) / 2;
setcolor(2);
sprintf(startp, "%d", start);
sprintf(midp, "%d", mid);
sprintf(endp, "%d", end);
outtextxy(180, 150, startp);
outtextxy(180, 200, midp);
outtextxy(180, 250, endp);
delay(500);
setcolor(1);
rectangle(180 + (70 * start), 300, 230 + (70 * start), 350);
rectangle(180 + (end * 70), 300, 230 + (70 * end), 350);
setcolor(2);
rectangle(180 + (mid * 70), 300, 230 + (70 * mid), 350);
// Check if x is present at mid
if (elements[mid] == aim)
{
d = mid;
break;
}
delay(700);
setcolor(4);
rectangle(180 + (mid * 70), 300, 230 + (70 * mid), 350);
delay(700);
setcolor(15);
rectangle(180 + (mid * 70), 300, 230 + (70 * mid), 350);
rectangle(180 + (70 * 0), 300, 230 + (70 * 0), 350);
rectangle(180 + (end * 70), 300, 230 + (70 * end), 350);
setcolor(0);
outtextxy(180, 150, startp);
outtextxy(180, 200, midp);
outtextxy(180, 250, endp);
// If x greater, ignore left half
if (elements[mid] < aim)
start = mid + 1;
// If x is smaller, ignore right half
else
end = mid - 1;
delay(1000);
}
delay(1300);
setcolor(15);
outtextxy(50, 450, "Match found at position : ");
setcolor(2);
sprintf(resultpos, "%d", d);
outtextxy(70, 500, resultpos);
getch();
closegraph();
}
// ---------------------------------------------------------------------------------- BUBBLE SORT ----------------------------------- //
else if (select == 3)
{
// calling the display function that is written outside the main function
displayBubble();
}
// ------------------------------------------------------------------------------ INSERTION SORT ----------------------------------- //
else if (select == 4)
{
displayInsertion();
}
else
{
cleardevice();
}
}
// ----------BUBBLE SORT FUNCTIONS
void displayBubble()
{
int n, i, d = 0, m = 0;
printf("Enter the No of Elements : ");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Enter the %d Element : ", i + 1);
scanf("%d", &el[i]);
}
initgraph(&d, &m, ""); //INITIALIZING THE GRAPHICS DRIVER
settextstyle(3, 0, 3);
setcolor(WHITE);
line(1, getmaxy() / 2 - 150, getmaxx(), getmaxy() / 2 - 150); // LINE ABOVE GIVEN NUMBER
setcolor(15);
settextstyle(10, 0, 6);
outtextxy(160, 10, "Bubble Sort");
settextstyle(10, 0, 4);
outtextxy(50, 100, "Input Array:");
settextstyle(3, 0, 3);
int x = 50, y = 150;
for (int i = 0; i < n; i++)
{
if (i == 0)
{
object(x, 150, y, 200, el[i]);
getch();
}
else
{
x = y;
y += 120;
object(x, 150, y, 200, el[i]);
getch();
}
}
bubble(n);
cleardevice();
// return 0;
}
void iterations(int n, int p, int q, int index)
{
int x = 50, y = 150;
for (int i = 0; i < n; i++)
{
if (i == index || i == index + 1)
{
setcolor(10);
}
else
{
setcolor(15);
}
if (i == 0)
{
object(x, p, y, q, el[i]);
getch();
}
else
{
x = y;
y += 120;
object(x, p, y, q, el[i]);
getch();
}
}
}
void last_iteration(int n, int p, int q)
{
int x = 50, y = 150;
for (int i = 0; i < n; i++)
{
if (i == 0)
{
object(x, p, y, q, el[i]);
getch();
}
else
{
x = y;
y += 120;
object(x, p, y, q, el[i]);
getch();
}
}
}
void bubble(int n)
{
int i, j, m, temp;
int x, y, p, q;
x = 50;
y = 250;
p = 210;
q = 260;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
char ele1[5];
char ele2[5];
if (el[j] > el[j + 1])
{
int temp;
temp = el[j + 1];
el[j + 1] = el[j];
el[j] = temp;
iterations(n, p, q, j);
p += 60;
q += 60;
}
}
}
/*FROM HERE WE ARE ADDING TEXT Sorted Array AND CHANGING ITS COLOR,
THEN THE FINAL ITERATION WITH GREEN COLOR..*/
settextstyle(10, 0, 4);
outtextxy(x, p + 20, "Sorted Array:");
settextstyle(3, 0, 3);
setcolor(10);
last_iteration(n, p + 70, q + 70);
}
void object(int a, int b, int c, int d, int i)
{
/* OBJECT() -- CREATES THE GIVEN ELEMENT
IN THE RECTANGLE AND DRAWS ON THE SCREEN*/
char s[8];
sprintf(s, "%d", i);
rectangle(a, b, c, d);
settextstyle(2, 0, 6);
outtextxy(a + 40, b + 15, s);
}
// -----------------------------INSERTION SORT FUNCTIONS---
void displayInsertion()
{
int n, i, d = 0, gm, m = 0;
int gd = DETECT;
int xdistance;
printf("Enter the Number of Elements: ");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Enter the %d Element : ", i + 1);
scanf("%d", &ElementsArray[i]);
}
cleardevice();
initwindow(800, 800);
//initwindow(1000,1000);
xdistance = MAX(130 + (n * 110), getmaxx());
/* settextstyle(3,0,3);
setcolor(WHITE); */
setcolor(15);
settextstyle(10, 0, 6);
outtextxy(160, 10, "Insertion Sort");
settextstyle(10, 0, 4);
line(1, getmaxy() / 2 - 315, xdistance, getmaxy() / 2 - 315); // Line After Insertion Sort Heading
getch();
outtextxy(90, 100, "Input Array:");
settextstyle(3, 0, 3);
int x = 90, y = 150;
for (int i = 0; i < n; i++)
{
if (i == 0)
{
element(x, 155, y, 205, ElementsArray[i]);
// getch();
}
else
{
x = y + 50;
y += 110;
element(x, 155, y, 205, ElementsArray[i]);
// getch();
}
delay(250);
}
line(1, getmaxy() / 2 - 192, xdistance, getmaxy() / 2 - 192); // Line After Input Array
getch();
InsertionSort(n);
getch();
}
void rows(int n, int p, int q, int index, bool flag, int off)
{
int x = 90, y = 150, dist = 0;
for (int i = 0; i < n; i++)
{
if (i == index + 1)
{
if (flag)
setcolor(14);
else
setcolor(2);
}
else
{
setcolor(15);
}
if (i == 0)
{
element(x, p, y, q, ElementsArray[i]);
// getch();
}
else
{
x = y + 50;
y += 110;
element(x, p, y, q, ElementsArray[i]);
// getch();
}
delay(250);
if (i == index + 1)
{
setcolor(2);
if (flag == false)
{
rectangle(90 - 10, p - 10, y + 10, q + 10);
}
else
{
dist = ((off * 110) + 150);
rectangle(90 - 10, p - 10, dist + 10, q + 10);
/* dist = ((off*110)+150);
rectangle(90-10,p-10,dist+10,q+10); */
}
}
delay(250);
}
getch();
}
void FinalArray(int n, int p, int q)
{
int x = 90, y = 150;
for (int i = 0; i < n; i++)
{
if (i == 0)
{
element(x, p, y, q, ElementsArray[i]);
// getch();
}
else
{
x = y + 50;
y += 110;
element(x, p, y, q, ElementsArray[i]);
// getch();
}
delay(250);
}
}
void InsertionSort(int n)
{
int i, j, m, temp;
int x, y, p, q;
bool flag;
x = 90;
y = 150;
p = 250;
q = 300;
int key;
int a = 0;
for (i = 1; i < n; i++)
{
key = ElementsArray[i];
j = i - 1;
flag = false;
while (j >= 0 && ElementsArray[j] > key)
{
ElementsArray[j + 1] = ElementsArray[j];
j = j - 1;
flag = true;
}
ElementsArray[j + 1] = key;
a = a + 1;
rows(n, p, q, j, flag, a);
p = q + 30;
q += 80;
}
/*FROM HERE WE ARE ADDING TEXT Sorted Array AND CHANGING ITS COLOR,
THEN THE FINAL ITERATION WITH GREEN COLOR..*/
settextstyle(10, 0, 4);
outtextxy(x, p + 20, "Sorted Array:");
settextstyle(3, 0, 3);
setcolor(2);
FinalArray(n, p + 80, q + 80);
}
void element(int a, int b, int c, int d, int i)
{
/* element() -- CREATES THE GIVEN ELEMENT IN THE RECTANGLE AND DRAWS ON THE SCREEN*/
char s[8];
sprintf(s, "%d", i);
rectangle(a, b, c, d);
settextstyle(2, 0, 6);
outtextxy(a + 25, b + 20, s);
line(a, b + 10, c, b + 10);
}