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.

978
128
883
37
538
19
319
776
715
221
990
593
909
706
389
891
351
694
721
103
590
180
74
492
284
977
796
800
588
672
512
129
683
987
596
577
426
708
157
52
623
176
468
651
864
959
402
548
232
403
177
921
607
205
535
122
463
69
699
426
195
395
294
555
613
677
547
679
677
925
919
764
354
61
998
719
316
628
76
557
146
182
250
92
337
51
398
779
825
762
491
943
369
93
217
379
758
359
984
484
BubbleSort - 0 steps
886
144
506
560
528
391
767
721
835
75
79
346
722
969
737
514
62
856
853
434
529
281
199
467
864
310
57
155
950
324
295
714
284
848
862
759
615
164
214
302
454
787
598
69
219
153
302
281
172
683
80
124
746
842
335
238
826
88
640
349
161
424
998
261
685
863
358
714
896
967
261
542
863
629
537
185
291
995
619
725
798
386
268
376
256
702
619
313
720
487
95
590
977
427
254
389
302
888
403
483
InsertionSort - 0 steps
888
313
311
827
30
364
120
432
423
823
396
691
522
498
929
25
409
263
913
687
804
203
922
836
577
325
68
796
620
907
258
24
119
506
268
127
615
121
435
965
238
479
215
876
16
811
229
823
279
533
744
740
562
583
487
36
479
621
518
554
164
709
547
170
643
223
904
61
165
18
901
38
806
392
294
563
307
468
167
790
853
659
464
192
513
331
106
90
721
780
75
799
687
150
915
482
770
741
632
941
ShellSort - 0 steps
664
972
354
965
633
26
804
719
425
647
411
869
947
424
651
80
746
503
558
686
597
352
730
920
849
527
814
305
304
303
26
88
905
672
617
536
269
648
314
89
479
313
147
392
75
90
885
336
430
656
853
694
448
719
227
568
326
130
379
319
521
42
908
116
167
118
298
603
566
533
975
46
766
254
881
613
751
132
534
820
614
950
253
947
683
584
328
233
431
975
43
777
597
484
934
643
297
4
381
238
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