-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.cpp
698 lines (644 loc) · 16.3 KB
/
sort.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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/* ORIGINAL CREATOR: Juan Villasenor
* Feel free to use & modify :)
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/Xdbe.h>
#include <chrono>
int amount = 100;
// Bigger BARWIDTH number = wider rectangles
// Smaller BARWIDTH number = less wide
#define BARWIDTH 30
#define SWAP(x,y) {int temp=x; x=y; y=temp;}
//Holds all the global vars, list of nums is here
class Global {
public:
int xres = 1280;
int yres = 720;
int* list;
double width;
bool sorting = false;
Global()
{
list = new int[amount];
srand((unsigned)time(NULL));
for(int i=0; i<amount; i++)
list[i] = rand() % yres;
width = xres/amount;
}
void init()
{
srand((unsigned)time(NULL));
amount = xres / BARWIDTH;
list = new int[amount];
for(int i=0; i<amount; i++)
list[i] = rand() % yres;
width = xres/amount;
}
~Global()
{
delete list;
list = NULL;
}
} g;
//holds x11 properties
class X11_wrapper {
private:
Display *dpy;
Window win;
GC gc;
XdbeBackBuffer backBuffer;
XdbeSwapInfo swapInfo;
public:
X11_wrapper() {
XSetWindowAttributes attributes;
XdbeBackBufferAttributes *backAttr;
dpy = XOpenDisplay(NULL);
attributes.event_mask = ExposureMask | StructureNotifyMask |
PointerMotionMask | ButtonPressMask |
ButtonReleaseMask | KeyPressMask | KeyReleaseMask;
attributes.backing_store = Always;
attributes.save_under = True;
attributes.override_redirect = False;
attributes.background_pixel = 0x00000000;
Window root = DefaultRootWindow(dpy);
XWindowAttributes main;
XGetWindowAttributes(dpy, root, &main);
g.xres = main.width;
g.yres = main.height;
g.init();
//Create a window
win = XCreateWindow(dpy, root, 0, 0, g.xres, g.yres, 0,
CopyFromParent, InputOutput, CopyFromParent,
CWBackingStore | CWOverrideRedirect | CWEventMask |
CWSaveUnder | CWBackPixel, &attributes);
//Create gc
gc = XCreateGC(dpy, win, 0, NULL);
//Get back buffer and attributes
backBuffer = XdbeAllocateBackBufferName(dpy, win, XdbeUndefined);
backAttr = XdbeGetBackBufferAttributes(dpy, backBuffer);
swapInfo.swap_window = backAttr->window;
swapInfo.swap_action = XdbeUndefined;
XFree(backAttr);
//Map and raise window
setWindowTitle();
XMapWindow(dpy, win);
XRaiseWindow(dpy, win);
}
~X11_wrapper() {
if(!XdbeDeallocateBackBufferName(dpy, backBuffer)) {
fprintf(stderr,"Error : unable to deallocate back buffer.\n");
}
XFreeGC(dpy, gc);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
}
void swapBuffers() {
XdbeSwapBuffers(dpy, &swapInfo, 1);
usleep(4000);
}
bool getPending() {
return XPending(dpy);
}
void getNextEvent(XEvent *e) {
XNextEvent(dpy, e);
}
void set_color_3i(int r, int g, int b) {
unsigned long cref = 0L;
cref += r;
cref <<= 8;
cref += g;
cref <<= 8;
cref += b;
XSetForeground(dpy, gc, cref);
}
void setWindowTitle() {
char ts[256];
sprintf(ts, "dbeCloth R=Replay %i x %i", g.xres, g.yres);
XStoreName(dpy, win, ts);
}
void clear_screen() {
//XClearWindow(dpy, backBuffer);
XSetForeground(dpy, gc, 0x00050505);
XFillRectangle(dpy, backBuffer, gc, 0, 0, g.xres, g.yres);
}
void drawString(int x, int y, const char *message) {
XDrawString(dpy, backBuffer, gc, x, y, message, strlen(message));
}
void drawPoint(int x, int y) {
XDrawPoint(dpy, backBuffer, gc, x, y);
}
void drawLine(int x0, int y0, int x1, int y1) {
XDrawLine(dpy, backBuffer, gc, x0, y0, x1, y1);
}
void drawRectangle(double x, double y, int w, int h) {
//x,y is upper-left corner
XDrawRectangle(dpy, backBuffer, gc, x, y, w, h);
}
void fillRectangle(double x, double y, int w, int h) {
//x,y is upper-left corner
XFillRectangle(dpy, backBuffer, gc, x, y, w, h);
}
} x11;
void render();
void physics();
void check_resize(XEvent *e);
void check_mouse(XEvent *e);
int check_keys(XEvent *e);
int main()
{
srand((unsigned)time(NULL));
int done = 0;
printf("\x1b[1m MENU OPTIONS ON TOP LEFT OF SCREEN\n");
sleep(2);
while(!done) {
while(x11.getPending()) {
XEvent e;
x11.getNextEvent(&e);
check_resize(&e);
check_mouse(&e);
done = check_keys(&e);
}
//rendering every frame
render();
x11.swapBuffers();
}
return 0;
}
void check_resize(XEvent *e)
{
if (e->type != ConfigureNotify)
return;
XConfigureEvent xce = e->xconfigure;
g.xres = xce.width;
g.yres = xce.height;
g.init();
x11.setWindowTitle();
}
void check_mouse(XEvent *e)
{
static int savex = 0;
static int savey = 0;
//
if (e->type == ButtonRelease) {
return;
}
if (e->type == ButtonPress) {
if (e->xbutton.button==1) {
//Left button is down
}
if (e->xbutton.button==3) {
//Right button is down
}
}
if (savex != e->xbutton.x || savey != e->xbutton.y) {
//Mouse moved
savex = e->xbutton.x;
savey = e->xbutton.y;
}
}
void shuffle()
{
for(int i=0; i<amount; i++) {
int idx1 = rand() % amount;
int idx2 = rand() % amount;
SWAP(g.list[idx1], g.list[idx2]);
render();
x11.swapBuffers();
}
}
void bubblesort()
{
auto start = std::chrono::high_resolution_clock::now();
bool swapped = true;
while(swapped) {
swapped = false;
for(int i=0; i<amount-1; i++) {
if(g.list[i] > g.list[i+1]) {
SWAP(g.list[i], g.list[i+1]);
swapped = true;
render();
x11.swapBuffers();
}
if(g.list[i] < g.list[i-1]) {
SWAP(g.list[i], g.list[i-1]);
swapped = true;
render();
x11.swapBuffers();
}
}
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end - start;
printf("\nBubble sort elapsed time was %.2f seconds\n", elapsed.count()/1000.0);
}
void cocktailsort()
{
bool swapped = true;
auto start = std::chrono::high_resolution_clock::now();
int endbound = amount, startbound = 0;
int i=0;
while(swapped) {
swapped = false;
for(i=startbound; i<endbound; i++) {
if (g.list[i] > g.list[i+1]) {
SWAP(g.list[i], g.list[i+1]);
swapped = true;
render();
x11.swapBuffers();
}
}
for(i-=1; i>startbound; i--) {
if (g.list[i] < g.list[i-1]) {
SWAP(g.list[i], g.list[i-1]);
swapped = true;
render();
x11.swapBuffers();
}
}
startbound++;
endbound--;
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end - start;
printf("\nCocktail sort elapsed time was %.2f seconds\n", elapsed.count()/1000.0);
}
void insertionsort()
{
int i, key, j;
auto start = std::chrono::high_resolution_clock::now();
for(i=1; i<amount; i++) {
key = g.list[i];
j = i-1;
while(j >= 0 && g.list[j] > key) {
g.list[j+1] = g.list[j];
j--;
render();
x11.swapBuffers();
}
g.list[j+1] = key;
render();
x11.swapBuffers();
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end - start;
printf("\nInsertion sort elapsed time was %.2f seconds\n", elapsed.count()/1000.0);
}
void selectionsort()
{
auto start = std::chrono::high_resolution_clock::now();
for(int i=0; i<amount-1; i++) {
for(int j=i+1; j<amount; j++)
if (g.list[j] < g.list[i]) {
SWAP(g.list[i], g.list[j]);
render();
x11.swapBuffers();
}
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end - start;
printf("\nSelection sort elapsed time was %.2f seconds\n", elapsed.count()/1000.0);
}
int partition (int low, int high)
{
int pivot = g.list[high];
int i = (low-1);
for (int j = low; j <= high - 1; j++) {
if (g.list[j] < pivot) {
i++;
SWAP(g.list[i], g.list[j]);
render();
x11.swapBuffers();
}
}
SWAP(g.list[i + 1], g.list[high]);
render();
x11.swapBuffers();
return (i + 1);
}
void quicksort(int low, int high)
{
if (low < high)
{
int pi = partition(low, high);
quicksort(low, pi - 1);
quicksort(pi+1, high);
}
}
void shellsort()
{
int h = amount / 2;
int key, j;
auto start = std::chrono::high_resolution_clock::now();
for(h; h>0; h /= 2) {
for(int i=h; i<amount; i++) {
key = g.list[i];
for(j=i; j>=h && g.list[j-h] > key; j-=h) {
g.list[j] = g.list[j-h];
render();
x11.swapBuffers();
}
g.list[j] = key;
render();
x11.swapBuffers();
}
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end - start;
printf("\nShell sort elapsed time was %.2f seconds\n", elapsed.count()/1000.0);
}
void count()
{
auto start = std::chrono::high_resolution_clock::now();
int* temp = new int[g.yres];
for(int i=0; i<g.yres; i++)
temp[i] = 0;
for(int i=0; i<amount; i++) {
temp[g.list[i]] += 1;
}
int j=0;
for(int i=0; i<g.yres; i++) {
while(temp[i] > 0) {
g.list[j] = i;
j++;
temp[i]--;
render();
x11.swapBuffers();
}
}
delete temp;
temp = NULL;
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end - start;
printf("\nCount sort elapsed time was %.2f seconds\n", elapsed.count()/1000.0);
}
void doubleSelectionsort()
{
auto start = std::chrono::steady_clock::now();
int front=0, end=amount-1, min, max, minIdx, maxIdx;
for(front; front<end;) {
min = g.list[front];
max = g.list[end];
maxIdx = end;
minIdx = front;
for(int j=front; j<=end; j++) {
if (g.list[j] >= max) {
max = g.list[j];
maxIdx = j;
}
else if (g.list[j] <= min) {
min = g.list[j];
minIdx = j;
}
}
if (g.list[minIdx] == g.list[end] && g.list[front] == g.list[maxIdx]) {
SWAP(g.list[minIdx], g.list[maxIdx]);
render();
x11.swapBuffers();
} else if (g.list[maxIdx] == g.list[front]) {
SWAP(g.list[end], g.list[maxIdx]);
render();
x11.swapBuffers();
SWAP(g.list[front], g.list[minIdx]);
render();
x11.swapBuffers();
} else {
SWAP(g.list[front], g.list[minIdx]);
render();
x11.swapBuffers();
SWAP(g.list[end], g.list[maxIdx]);
render();
x11.swapBuffers();
}
front++;
end--;
}
auto endTime = std::chrono::steady_clock::now();
std::chrono::duration<double, std::milli> elapsed = endTime - start;
printf("\nDouble Selection sort elapsed time was %.2f seconds\n", elapsed.count()/1000.0);
return;
}
void merge(int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for(i = 0; i < n1; i++)
L[i] = g.list[l+i];
for(j = 0; j < n2; j++)
R[j] = g.list[m+1+j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j]) {
g.list[k] = L[i];
i++;
} else {
g.list[k] = R[j];
j++;
}
k++;
render();
x11.swapBuffers();
}
while(i < n1) {
g.list[k] = L[i];
i++;
k++;
render();
x11.swapBuffers();
}
while(j < n2) {
g.list[k] = R[j];
j++;
k++;
render();
x11.swapBuffers();
}
}
void mergesort(int l, int r)
{
if (l < r) {
int m = l+(r-l)/2;
mergesort(l, m);
mergesort(m+1, r);
merge(l, m, r);
}
}
void heapify(int n, int i)
{
int largest = i;
int child1 = 2*i + 1;
int child2 = 2*i + 2;
if (child1<n && g.list[child1] > g.list[largest])
largest = child1;
if (child2<n && g.list[child2] > g.list[largest])
largest = child2;
if (largest != i) {
SWAP(g.list[i], g.list[largest]);
render();
x11.swapBuffers();
heapify(n, largest);
}
}
void heapsort()
{
int n = amount;
for(int i=n/2-1; i>=0; i--)
heapify(n, i);
for(int i=n-1; i>=0; i--) {
SWAP(g.list[0], g.list[i]);
render();
x11.swapBuffers();
heapify(i, 0);
}
}
int check_keys(XEvent *e)
{
if (e->type != KeyPress && e->type != KeyPress)
return 0;
int key = XLookupKeysym(&e->xkey, 0);
switch(key) {
case XK_1:
shuffle();
break;
case XK_c:
g.sorting = true;
count();
g.sorting = false;
break;
case XK_q: {
g.sorting = true;
auto start = std::chrono::high_resolution_clock::now();
quicksort(0, amount-1);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end - start;
printf("\nQuick sort elapsed time was %.2f seconds\n", elapsed.count()/1000.0);
g.sorting = false;
break;
}
case XK_b:
g.sorting = true;
bubblesort();
g.sorting = false;
break;
case XK_w:
g.sorting = true;
shellsort();
g.sorting = false;
break;
case XK_o:
g.sorting = true;
cocktailsort();
g.sorting = false;
break;
case XK_i:
g.sorting = true;
insertionsort();
g.sorting = false;
break;
case XK_s:
g.sorting = true;
selectionsort();
g.sorting = false;
break;
case XK_d:
g.sorting = true;
doubleSelectionsort();
g.sorting = false;
break;
case XK_m: {
g.sorting = true;
auto start = std::chrono::high_resolution_clock::now();
mergesort(0,amount);
g.sorting = false;
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end - start;
printf("\nMerge sort elapsed time was %.2f seconds\n", elapsed.count()/1000.0);
break;
}
case XK_h: {
g.sorting = true;
auto start = std::chrono::high_resolution_clock::now();
heapsort();
g.sorting = false;
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end - start;
printf("\nHeap sort elapsed time was %.2f seconds\n", elapsed.count()/1000.0);
break;
}
case XK_Escape:
return(1);
}
return(0);
}
void showmenu()
{
int y = 20;
int inc = 16;
char buf[32];
sprintf(buf, "%d elements in list", amount);
x11.set_color_3i(0, 255, 0);
if (!g.sorting) {
x11.drawString(10, y, "Menu");
y+= inc;
x11.drawString(10, y, "======");
y+= inc;
x11.drawString(10, y, "ESC - Exit program");
y+= inc;
x11.drawString(10, y, "1 - Shuffle list");
y+= inc;
x11.drawString(10, y, "B - Bubble sort");
y+= inc;
x11.drawString(10, y, "O - Cocktail sort");
y+= inc;
x11.drawString(10, y, "S - Selection sort");
y+= inc;
x11.drawString(10, y, "I - Insertion sort");
y+= inc;
x11.drawString(10, y, "M - Merge sort");
y+= inc;
x11.drawString(10, y, "H - Heap sort");
y+= inc;
x11.drawString(10, y, "Q - Quick sort");
y+= inc;
x11.drawString(10, y, "D - Double Selection sort");
y+= inc;
x11.drawString(10, y, "C - Counting sort");
y+= inc;
x11.drawString(10, y, "W - Shell sort");
x11.set_color_3i(255, 0, 0);
y+= inc;
x11.drawString(10, y, buf);
}
else {
x11.drawString(10, y, "Currently sorting....");
y+= inc;
x11.drawString(10, y, "Wait until list is sorted for button input");
x11.set_color_3i(255, 0, 0);
y+= inc;
x11.drawString(10, y, buf);
}
}
void render()
{
x11.clear_screen();
double xpos = 0;
for(int i=0; i<amount; i++) {
x11.set_color_3i(255, 0, 0);
x11.fillRectangle(xpos, g.yres-g.list[i], g.width, g.yres);
x11.set_color_3i(255, 255, 255);
x11.drawRectangle(xpos, g.yres-g.list[i], g.width, g.yres);
xpos += g.width;
}
showmenu();
}