-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting.cpp
526 lines (469 loc) · 13 KB
/
sorting.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
#include<SDL.h>
#include<iostream>
#include<limits>
#include<time.h>
#include<string>
using namespace std;
const int SCREEN_WIDTH=910;
const int SCREEN_HEIGHT=750;
const int arrSize=130;
const int rectSize=7;
int arr[arrSize];
int Barr[arrSize];
SDL_Window* window=NULL;
SDL_Renderer* renderer=NULL;
bool complete=false;
bool init()
{
bool success=true;
if(SDL_Init(SDL_INIT_VIDEO)<0)
{
cout<<"Couldn't initialize SDL. SDL_Error: "<<SDL_GetError();
success=false;
}
else
{
if(!(SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")))
{
cout<<"Warning: Linear Texture Filtering not enabled.\n";
}
window=SDL_CreateWindow("Sorting Visualizer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(window==NULL)
{
cout<<"Couldn't create window. SDL_Error: "<<SDL_GetError();
success=false;
}
else
{
renderer=SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if(renderer==NULL)
{
cout<<"Couldn't create renderer. SDL_Error: "<<SDL_GetError();
success=false;
}
}
}
return success;
}
void close()
{
SDL_DestroyRenderer(renderer);
renderer=NULL;
SDL_DestroyWindow(window);
window=NULL;
SDL_Quit();
}
void visualize(int x=-1, int y=-1, int z=-1)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
int j=0;
for(int i=0; i<=SCREEN_WIDTH-rectSize; i+=rectSize)
{
SDL_PumpEvents();
SDL_Rect rect={i, 0, rectSize, arr[j]};
if(complete)
{
SDL_SetRenderDrawColor(renderer, 100, 180, 100, 0);
SDL_RenderDrawRect(renderer, &rect);
}
else if(j==x || j==z)
{
SDL_SetRenderDrawColor(renderer, 100, 180, 100, 0);
SDL_RenderFillRect(renderer, &rect);
}
else if(j==y)
{
SDL_SetRenderDrawColor(renderer, 165, 105, 189, 0);
SDL_RenderFillRect(renderer, &rect);
}
else
{
SDL_SetRenderDrawColor(renderer, 170, 183, 184, 0);
SDL_RenderDrawRect(renderer, &rect);
}
j++;
}
SDL_RenderPresent(renderer);
}
void inplaceHeapSort(int* input, int n)
{
for(int i=1; i<n; i++)
{
int childIndex=i;
int parentIndex=(childIndex-1)/2;
while(childIndex>0)
{
if(input[childIndex]>input[parentIndex])
{
int temp=input[parentIndex];
input[parentIndex]=input[childIndex];
input[childIndex]=temp;
}
else
{
break;
}
visualize(parentIndex, childIndex);
SDL_Delay(40);
childIndex=parentIndex;
parentIndex=(childIndex-1)/2;
}
}
for(int heapLast=n-1; heapLast>=0 ; heapLast--)
{
int temp=input[0];
input[0]=input[heapLast];
input[heapLast]=temp;
int parentIndex=0;
int leftChildIndex=2*parentIndex + 1;
int rightChildIndex=2*parentIndex + 2;
while(leftChildIndex<heapLast)
{
int maxIndex=parentIndex;
if(input[leftChildIndex]>input[maxIndex])
{
maxIndex=leftChildIndex;
}
if(rightChildIndex<heapLast && input[rightChildIndex]>input[maxIndex])
{
maxIndex=rightChildIndex;
}
if(maxIndex==parentIndex)
{
break;
}
int temp=input[parentIndex];
input[parentIndex]=input[maxIndex];
input[maxIndex]=temp;
visualize(maxIndex, parentIndex, heapLast);
SDL_Delay(40);
parentIndex=maxIndex;
leftChildIndex=2*parentIndex + 1;
rightChildIndex=2*parentIndex + 2;
}
}
}
int partition_array(int a[], int si, int ei)
{
int count_small=0;
for(int i=(si+1);i<=ei;i++)
{
if(a[i]<=a[si])
{
count_small++;
}
}
int c=si+count_small;
int temp=a[c];
a[c]=a[si];
a[si]=temp;
visualize(c, si);
int i=si, j=ei;
while(i<c && j>c)
{
if(a[i]<= a[c])
{
i++;
}
else if(a[j]>a[c])
{
j--;
}
else
{
int temp_1=a[j];
a[j]=a[i];
a[i]=temp_1;
visualize(i, j);
SDL_Delay(70);
i++;
j--;
}
}
return c;
}
void quickSort(int a[], int si, int ei)
{
if(si>=ei)
{
return;
}
int c=partition_array(a, si, ei);
quickSort(a, si, c-1);
quickSort(a, c+1, ei);
}
void merge2SortedArrays(int a[], int si, int ei)
{
int size_output=(ei-si)+1;
int* output=new int[size_output];
int mid=(si+ei)/2;
int i=si, j=mid+1, k=0;
while(i<=mid && j<=ei)
{
if(a[i]<=a[j])
{
output[k]=a[i];
visualize(i, j);
i++;
k++;
}
else
{
output[k]=a[j];
visualize(i, j);
j++;
k++;
}
}
while(i<=mid)
{
output[k]=a[i];
visualize(-1, i);
i++;
k++;
}
while(j<=ei)
{
output[k]=a[j];
visualize(-1, j);
j++;
k++;
}
int x=0;
for(int l=si; l<=ei; l++)
{
a[l]=output[x];
visualize(l);
SDL_Delay(15);
x++;
}
delete []output;
}
void mergeSort(int a[], int si, int ei)
{
if(si>=ei)
{
return;
}
int mid=(si+ei)/2;
mergeSort(a, si, mid);
mergeSort(a, mid+1, ei);
merge2SortedArrays(a, si, ei);
}
void bubbleSort()
{
for(int i=0; i<arrSize-1; i++)
{
for(int j=0; j<arrSize-1-i; j++)
{
if(arr[j+1]<arr[j])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
visualize(j+1, j, arrSize-i);
}
SDL_Delay(1);
}
}
}
void insertionSort()
{
for(int i=1; i<arrSize; i++)
{
int j=i-1;
int temp=arr[i];
while(j>=0 && arr[j]>temp)
{
arr[j+1]=arr[j];
j--;
visualize(i, j+1);
SDL_Delay(5);
}
arr[j+1]=temp;
}
}
void selectionSort()
{
int minIndex;
for(int i=0;i<arrSize-1;i++)
{
minIndex=i;
for(int j=i+1;j<arrSize;j++)
{
if(arr[j]<arr[minIndex])
{
minIndex=j;
visualize(i, minIndex);
}
SDL_Delay(1);
}
int temp=arr[i];
arr[i]=arr[minIndex];
arr[minIndex]=temp;
}
}
void loadArr()
{
memcpy(arr, Barr, sizeof(int)*arrSize);
}
void randomizeAndSaveArray()
{
unsigned int seed=(unsigned)time(NULL);
srand(seed);
for(int i=0; i<arrSize; i++)
{
int random=rand()%(SCREEN_HEIGHT);
Barr[i]=random;
}
}
void execute()
{
if(!init())
{
cout<<"SDL Initialization Failed.\n";
}
else
{
randomizeAndSaveArray();
loadArr();
SDL_Event e;
bool quit=false;
while(!quit)
{
while(SDL_PollEvent(&e)!=0)
{
if(e.type==SDL_QUIT)
{
quit=true;
complete=false;
}
else if(e.type==SDL_KEYDOWN)
{
switch(e.key.keysym.sym)
{
case(SDLK_q):
quit=true;
complete=false;
cout<<"\nEXITING SORTING VISUALIZER.\n";
break;
case(SDLK_0):
randomizeAndSaveArray();
complete=false;
loadArr();
cout<<"\nNEW RANDOM LIST GENERATED.\n";
break;
case(SDLK_1):
loadArr();
cout<<"\nSELECTION SORT STARTED.\n";
complete=false;
selectionSort();
complete=true;
cout<<"\nSELECTION SORT COMPLETE.\n";
break;
case(SDLK_2):
loadArr();
cout<<"\nINSERTION SORT STARTED.\n";
complete=false;
insertionSort();
complete=true;
cout<<"\nINSERTION SORT COMPLETE.\n";
break;
case(SDLK_3):
loadArr();
cout<<"\nBUBBLE SORT STARTED.\n";
complete=false;
bubbleSort();
complete=true;
cout<<"\nBUBBLE SORT COMPLETE.\n";
break;
case(SDLK_4):
loadArr();
cout<<"\nMERGE SORT STARTED.\n";
complete=false;
mergeSort(arr, 0, arrSize-1);
complete=true;
cout<<"\nMERGE SORT COMPLETE.\n";
break;
case(SDLK_5):
loadArr();
cout<<"\nQUICK SORT STARTED.\n";
complete=false;
quickSort(arr, 0, arrSize-1);
complete=true;
cout<<"\nQUICK SORT COMPLETE.\n";
break;
case(SDLK_6):
loadArr();
cout<<"\nHEAP SORT STARTED.\n";
complete=false;
inplaceHeapSort(arr, arrSize);
complete=true;
cout<<"\nHEAP SORT COMPLETE.\n";
break;
}
}
}
visualize();
}
close();
}
}
bool controls()
{
cout <<"WARNING: Giving repetitive commands may cause latency and the visualizer may behave unexpectedly. Please give a new command only after the current command's execution is done.\n\n"
<<"Available Controls inside Sorting Visualizer:-\n"
<<" Use 0 to Generate a different randomized list.\n"
<<" Use 1 to start Selection Sort Algorithm.\n"
<<" Use 2 to start Insertion Sort Algorithm.\n"
<<" Use 3 to start Bubble Sort Algorithm.\n"
<<" Use 4 to start Merge Sort Algorithm.\n"
<<" Use 5 to start Quick Sort Algorithm.\n"
<<" Use 6 to start Heap Sort Algorithm.\n"
<<" Use q to exit out of Sorting Visualizer\n\n"
<<"PRESS ENTER TO START SORTING VISUALIZER...\n\n"
<<"Or type -1 and press ENTER to quit the program.";
string s;
getline(cin, s);
if(s=="-1")
{
return false;
}
//else if(s=="\n")
//{
// return true;
//}
return true;
}
void intro()
{
cout<<"==============================Sorting Visualizer==============================\n\n"
<<"Visualization of different sorting algorithms in C++ with SDL2 Library. A sorting algorithm is an algorithm that puts the elements of a list in a certain order. While there are a large number of sorting algorithms, in practical implementations a few algorithms predominate.\n"
<<"In this implementation of sorting visualizer, we'll be looking at some of these sorting algorithms and visually comprehend their working.\n"
<<"The sorting algorithms covered here are Selection Sort, Insertion Sort, Bubble Sort, Merge Sort, Quick Sort and Heap Sort.\n"
<<"The list size is fixed to 130 elements. You can randomize the list and select any type of sorting algorithm to call on the list from the given options. Here, all sorting algorithms will sort the elements in ascending order. The sorting time being visualized for an algorithm is not exactly same as their actual time complexities. The relatively faster algorithms like Merge Sort, etc. have been delayed so that they could be properly visualized.\n\n"
<<"Press ENTER to show controls...";
string s;
getline(cin, s);
if(s=="\n")
{
return;
}
}
int main(int argc, char* args[])
{
intro();
while(1)
{
cout<<'\n';
if(controls())
execute();
else
{
cout<<"\nEXITING PROGRAM.\n";
break;
}
}
return 0;
}