skip to content

JavaScript: Sorting Algorithm Comparison

In this article we present a visualizaion of four different JavaScript DHTML sorting classes all of which have been described in more detail in previous articles.

Sorting Algorithm Visualization

Below you will see four scrambled versions of the same image. When you use the controls below to 'solve' the puzzles they will each use a different sorting algorithm as indicated - Bubble, Insertion, Shell and Quick Sort - to rearrange the pieces.

You can watch in real time as the sorting takes place and see an updating counter of the number of steps taken so far - where a 'step' is the process of exchanging two puzzle pieces.

49
905
88
718
746
799
590
400
696
951
956
821
768
768
818
923
657
586
927
448
547
645
896
395
903
312
175
507
372
491
557
278
942
959
635
111
778
262
354
900
931
468
623
999
569
61
298
748
896
854
128
580
172
703
947
343
713
754
586
544
209
842
525
528
852
859
948
145
749
365
347
305
626
263
859
819
365
351
438
113
999
335
637
1000
56
967
264
176
576
599
448
21
180
755
761
763
829
251
98
911
BubbleSort - 0 steps
377
693
710
262
930
506
776
24
212
577
56
851
230
38
541
402
126
924
889
906
72
385
719
952
385
788
681
446
482
511
536
328
662
246
372
363
577
994
373
715
709
213
565
412
249
435
341
389
567
287
967
780
339
62
801
768
553
621
684
855
100
358
200
360
587
629
946
7
563
936
182
513
868
217
108
896
855
317
203
300
615
202
173
855
206
363
922
674
137
757
398
437
796
324
969
997
735
570
488
387
InsertionSort - 0 steps
595
360
973
921
677
885
889
811
914
683
867
792
914
27
432
235
333
65
419
902
182
785
867
376
585
882
252
423
233
697
138
133
717
16
1
401
441
763
554
473
822
632
773
595
532
310
601
538
564
843
691
440
476
333
852
691
605
585
580
373
845
765
920
369
623
190
860
944
377
735
456
366
731
917
821
557
858
562
688
424
430
302
314
442
166
170
243
954
714
854
865
910
52
668
962
810
962
435
256
729
ShellSort - 0 steps
383
25
200
1000
371
841
861
306
43
520
115
477
357
759
452
555
122
234
215
488
592
14
677
486
855
407
1000
83
191
968
792
990
679
311
930
869
95
854
408
332
508
982
414
547
862
77
476
930
524
163
745
157
714
478
339
863
818
673
14
117
394
149
518
276
437
848
995
939
963
965
39
138
28
537
775
393
726
738
314
44
282
548
210
846
519
416
852
852
358
137
125
517
93
892
446
523
677
332
141
955
QuickSort - 0 steps
Controls 1) Select an image; 2) Click 'SOLVE'. * images generated by Stable Diffusion and Midjourney

All of the sorting is powered by JavaScript in your web browser so there is no load at all on the web server. There is also only a single background image being used each time - they haven't been sliced up into smaller squares for the puzzle.

While there are other methods for shuffling and sorting values, the advantage of DHTML sorting - rearranging actual HTML elements within the DOM - is that it preserves any event handlers or other dynamically assigned properties that may have been assigned to the elements.

This is possible because we are working with a 'live' NodeList which means that "changes in the DOM automatically update the collection."

Comparison of Results

As expected, the Bubble Sort and Insertion Sort algorithms are relatively slow requiring a large number of steps to solve the puzzle. This is mainly down to the fact that they can only swap adjacent squares.

The Insertion Sort and Quick Sort algorithms are significantly faster thanks to their more advanced algorithms requiring only a fraction of the number of steps each time to reconfigure the puzzle pieces.

We generally use the Shell Sort algorithm which, despite being slightly slower, is a stable sort, whereas Quick Sort is unstable (a sorting algorithm is said to be stable "when two objects with equal keys appear in the same order in sorted output as they appear in the input unsorted array").

What do we use if for?

Apart from these fascinating visualizations we typically use JavaScript DHTML sorting when presenting tabular data. It allows us to have the table contents sorted by various values on demand without needing to re-request data from the web server.

You can see some examples of this in earlier articles on the subject. The code used here for the visualization has been adapted slightly to insert a delay, but is otherwise identical to the code presented there.

We were able to insert delays into the sorting process by converting the exchange step to use a generator function which is then called repeatedly by setInterval. Generators have the effect of allowing you to 'pause' and 'resume' execution within a function.

Another interesting use case would be maintaining a 'pole position' graphic where race data was being dynamically inserted into the page and the task was to keep the list in the right order - perhaps with a touch of animation.

If you find a use for this code in your website or project please let us know using the comments button below.

< JavaScript

Post your comment or question
top