Skip to content

Commit a0af6d2

Browse files
committed
Added shell sort
1 parent a63a36b commit a0af6d2

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

sort.cpp

+44-2
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ void insertionsort()
304304
x11.swapBuffers();
305305
}
306306
g.list[j+1] = key;
307+
render();
308+
x11.swapBuffers();
307309
}
308310
auto end = std::chrono::high_resolution_clock::now();
309311
std::chrono::duration<double, std::milli> elapsed = end - start;
@@ -356,6 +358,30 @@ void quicksort(int low, int high)
356358
}
357359
}
358360

361+
void shellsort()
362+
{
363+
int h = amount / 2;
364+
int key, j;
365+
auto start = std::chrono::high_resolution_clock::now();
366+
367+
for(h; h>0; h /= 2) {
368+
for(int i=h; i<amount; i++) {
369+
key = g.list[i];
370+
for(j=i; j>=h && g.list[j-h] > key; j-=h) {
371+
g.list[j] = g.list[j-h];
372+
render();
373+
x11.swapBuffers();
374+
}
375+
g.list[j] = key;
376+
render();
377+
x11.swapBuffers();
378+
}
379+
}
380+
auto end = std::chrono::high_resolution_clock::now();
381+
std::chrono::duration<double, std::milli> elapsed = end - start;
382+
printf("\nShell sort elapsed time was %.2f seconds\n", elapsed.count()/1000.0);
383+
}
384+
359385
void count()
360386
{
361387
auto start = std::chrono::high_resolution_clock::now();
@@ -514,8 +540,9 @@ void heapify(int n, int i)
514540
}
515541
}
516542

517-
void heapsort(int n)
543+
void heapsort()
518544
{
545+
int n = amount;
519546
for(int i=n/2-1; i>=0; i--)
520547
heapify(n, i);
521548
for(int i=n-1; i>=0; i--) {
@@ -555,6 +582,11 @@ int check_keys(XEvent *e)
555582
bubblesort();
556583
g.sorting = false;
557584
break;
585+
case XK_w:
586+
g.sorting = true;
587+
shellsort();
588+
g.sorting = false;
589+
break;
558590
case XK_o:
559591
g.sorting = true;
560592
cocktailsort();
@@ -588,7 +620,7 @@ int check_keys(XEvent *e)
588620
case XK_h: {
589621
g.sorting = true;
590622
auto start = std::chrono::high_resolution_clock::now();
591-
heapsort(amount);
623+
heapsort();
592624
g.sorting = false;
593625
auto end = std::chrono::high_resolution_clock::now();
594626
std::chrono::duration<double, std::milli> elapsed = end - start;
@@ -606,6 +638,8 @@ void showmenu()
606638
{
607639
int y = 20;
608640
int inc = 16;
641+
char buf[32];
642+
sprintf(buf, "%d elements in list", amount);
609643
x11.set_color_3i(0, 255, 0);
610644
if (!g.sorting) {
611645
x11.drawString(10, y, "Menu");
@@ -633,11 +667,19 @@ void showmenu()
633667
x11.drawString(10, y, "D - Double Selection sort");
634668
y+= inc;
635669
x11.drawString(10, y, "C - Counting sort");
670+
y+= inc;
671+
x11.drawString(10, y, "W - Shell sort");
672+
x11.set_color_3i(255, 0, 0);
673+
y+= inc;
674+
x11.drawString(10, y, buf);
636675
}
637676
else {
638677
x11.drawString(10, y, "Currently sorting....");
639678
y+= inc;
640679
x11.drawString(10, y, "Wait until list is sorted for button input");
680+
x11.set_color_3i(255, 0, 0);
681+
y+= inc;
682+
x11.drawString(10, y, buf);
641683
}
642684
}
643685

0 commit comments

Comments
 (0)