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.

437
398
451
103
954
512
209
243
976
487
670
137
889
309
793
925
907
202
985
883
524
253
208
834
390
306
485
917
544
992
459
413
794
988
594
786
272
487
107
776
741
404
838
984
924
513
80
32
785
583
783
393
520
21
239
982
761
25
190
825
226
210
62
543
628
328
461
919
340
130
610
404
675
828
856
545
8
599
341
737
656
377
788
541
738
953
642
169
631
473
427
922
219
248
82
326
211
589
310
484
BubbleSort - 0 steps
398
182
932
656
506
599
920
79
460
269
27
858
698
88
13
64
211
81
605
33
972
122
571
25
293
545
577
6
433
125
69
992
81
338
367
456
920
548
793
35
229
929
698
732
955
848
434
746
46
883
900
930
147
647
166
575
156
329
960
421
965
567
786
109
338
427
349
603
356
658
567
7
313
606
307
347
156
316
65
631
513
499
922
971
931
531
150
783
451
105
638
678
854
705
376
376
200
885
814
720
InsertionSort - 0 steps
827
536
934
472
733
430
500
796
525
474
74
931
361
754
903
689
22
459
529
395
869
460
25
862
241
263
160
335
601
317
982
33
817
174
425
217
187
344
320
953
517
945
660
91
624
411
415
840
366
869
465
688
633
423
335
817
60
984
112
734
387
604
375
319
864
903
268
57
230
758
526
625
212
391
87
592
650
397
913
340
600
565
333
984
635
312
783
558
316
112
183
822
767
784
601
181
338
740
295
214
ShellSort - 0 steps
661
974
330
768
599
191
382
549
680
395
724
124
706
766
153
34
372
402
912
750
289
993
924
253
731
996
905
327
736
990
608
484
369
561
505
333
814
514
490
658
950
703
459
958
51
907
268
762
605
58
687
332
869
810
135
23
25
918
364
772
863
801
825
482
471
997
149
359
257
985
450
223
733
643
75
469
986
597
235
44
905
3
261
309
407
2
696
380
546
588
738
704
498
279
404
638
19
493
37
59
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