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.

598
409
909
750
379
712
33
859
329
410
441
419
491
141
559
840
893
150
173
426
814
763
429
301
318
725
685
40
609
829
378
459
10
209
915
741
328
1
661
430
202
397
444
438
348
850
651
503
383
624
382
497
920
879
256
462
613
61
215
893
747
594
420
612
571
580
117
160
689
33
171
561
69
905
899
719
252
583
139
442
663
75
365
874
754
326
866
331
228
43
558
106
8
579
687
89
234
6
60
706
BubbleSort - 0 steps
52
277
225
594
809
709
220
419
93
210
767
648
130
530
783
636
516
726
156
135
230
241
174
177
421
579
627
5
805
250
412
442
152
354
440
778
669
155
252
889
79
529
771
467
861
96
813
441
138
810
910
738
23
123
34
376
721
312
732
347
351
706
649
571
927
788
62
465
93
577
596
870
552
635
72
911
224
620
358
524
782
362
704
484
419
470
518
930
365
883
693
828
742
339
754
14
249
306
454
13
InsertionSort - 0 steps
94
811
781
176
47
486
260
160
461
254
128
977
776
353
1
855
563
259
314
486
530
991
116
314
370
838
59
49
212
948
613
874
736
885
2
136
334
698
488
807
341
321
866
743
587
756
898
823
962
365
406
800
78
698
984
308
94
947
497
476
472
149
850
24
234
628
241
513
419
619
585
824
139
794
493
807
92
841
552
487
760
530
175
42
882
700
199
374
885
818
391
750
45
445
986
669
404
403
470
194
ShellSort - 0 steps
645
925
329
246
498
443
635
427
232
104
57
257
808
702
942
249
855
887
591
705
427
557
706
518
351
740
285
131
148
268
356
684
401
191
327
261
813
86
180
438
508
275
785
773
465
637
588
135
653
173
271
731
63
897
833
52
560
626
767
745
6
853
517
71
394
388
143
238
130
812
549
215
708
522
508
802
947
770
683
480
926
72
373
542
331
141
475
703
456
801
666
440
248
768
263
213
170
544
414
653
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