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.

632
303
217
359
235
511
997
217
276
437
946
60
207
966
728
347
359
152
411
591
633
977
800
455
703
84
113
652
11
512
104
905
235
801
613
404
941
231
395
237
951
546
851
943
479
489
868
131
598
401
738
359
883
394
606
17
715
980
993
592
856
87
644
811
384
515
870
389
404
374
292
384
28
357
298
809
989
345
478
842
291
352
714
362
306
70
108
532
778
715
293
971
450
762
840
733
739
188
772
887
BubbleSort - 0 steps
149
41
514
827
815
780
954
882
150
729
395
653
748
604
313
635
580
762
143
160
791
247
701
196
948
748
428
252
918
903
834
939
822
63
660
144
592
856
976
289
869
776
804
383
1000
504
292
125
472
158
795
723
418
395
806
694
374
225
600
986
484
430
477
871
692
247
581
167
433
249
456
953
649
411
189
547
889
174
680
903
426
454
544
284
711
197
597
89
806
715
782
572
410
374
341
654
113
95
884
777
InsertionSort - 0 steps
469
56
508
112
109
415
585
516
411
537
410
202
390
833
189
317
356
38
903
236
648
852
540
253
670
968
347
762
660
369
707
894
89
987
476
445
598
447
129
397
852
946
19
504
774
739
726
126
64
172
727
871
143
331
339
451
245
61
267
939
649
309
509
190
121
109
364
450
964
364
640
482
800
958
725
382
534
378
163
940
229
312
961
721
497
375
878
894
415
74
776
500
530
420
66
502
306
906
347
172
ShellSort - 0 steps
170
121
65
754
453
108
672
893
139
641
737
992
829
447
800
265
518
522
629
95
458
748
920
922
542
736
755
829
887
787
286
253
864
617
697
118
571
403
728
863
232
73
137
934
315
148
678
300
632
318
959
217
757
351
961
807
738
294
341
207
508
927
414
530
586
149
856
220
625
282
991
698
529
202
722
522
761
407
396
605
411
977
519
157
915
638
382
782
494
355
188
546
948
443
981
725
864
914
649
604
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