@@ -304,6 +304,8 @@ void insertionsort()
304
304
x11.swapBuffers ();
305
305
}
306
306
g.list [j+1 ] = key;
307
+ render ();
308
+ x11.swapBuffers ();
307
309
}
308
310
auto end = std::chrono::high_resolution_clock::now ();
309
311
std::chrono::duration<double , std::milli> elapsed = end - start;
@@ -356,6 +358,30 @@ void quicksort(int low, int high)
356
358
}
357
359
}
358
360
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 (" \n Shell sort elapsed time was %.2f seconds\n " , elapsed.count ()/1000.0 );
383
+ }
384
+
359
385
void count ()
360
386
{
361
387
auto start = std::chrono::high_resolution_clock::now ();
@@ -514,8 +540,9 @@ void heapify(int n, int i)
514
540
}
515
541
}
516
542
517
- void heapsort (int n )
543
+ void heapsort ()
518
544
{
545
+ int n = amount;
519
546
for (int i=n/2 -1 ; i>=0 ; i--)
520
547
heapify (n, i);
521
548
for (int i=n-1 ; i>=0 ; i--) {
@@ -555,6 +582,11 @@ int check_keys(XEvent *e)
555
582
bubblesort ();
556
583
g.sorting = false ;
557
584
break ;
585
+ case XK_w:
586
+ g.sorting = true ;
587
+ shellsort ();
588
+ g.sorting = false ;
589
+ break ;
558
590
case XK_o:
559
591
g.sorting = true ;
560
592
cocktailsort ();
@@ -588,7 +620,7 @@ int check_keys(XEvent *e)
588
620
case XK_h: {
589
621
g.sorting = true ;
590
622
auto start = std::chrono::high_resolution_clock::now ();
591
- heapsort (amount );
623
+ heapsort ();
592
624
g.sorting = false ;
593
625
auto end = std::chrono::high_resolution_clock::now ();
594
626
std::chrono::duration<double , std::milli> elapsed = end - start;
@@ -606,6 +638,8 @@ void showmenu()
606
638
{
607
639
int y = 20 ;
608
640
int inc = 16 ;
641
+ char buf[32 ];
642
+ sprintf (buf, " %d elements in list" , amount);
609
643
x11.set_color_3i (0 , 255 , 0 );
610
644
if (!g.sorting ) {
611
645
x11.drawString (10 , y, " Menu" );
@@ -633,11 +667,19 @@ void showmenu()
633
667
x11.drawString (10 , y, " D - Double Selection sort" );
634
668
y+= inc;
635
669
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);
636
675
}
637
676
else {
638
677
x11.drawString (10 , y, " Currently sorting...." );
639
678
y+= inc;
640
679
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);
641
683
}
642
684
}
643
685
0 commit comments