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.

40
27
511
547
945
423
923
937
812
152
153
604
13
858
928
284
880
396
414
541
365
89
822
780
883
159
529
350
959
274
385
427
594
191
118
237
746
679
448
271
198
475
533
674
680
511
174
507
821
236
362
676
955
101
336
220
564
849
167
603
965
826
485
630
912
910
353
858
794
861
356
842
447
424
994
393
801
453
601
801
325
483
592
565
350
584
139
657
405
207
195
912
332
321
373
576
22
419
96
467
BubbleSort - 0 steps
895
595
882
331
463
924
823
44
401
142
593
523
288
727
717
984
628
4
981
694
305
220
375
540
688
414
450
226
544
513
237
219
842
232
914
103
488
66
410
747
327
226
812
51
421
180
256
796
838
964
327
711
441
407
858
556
980
639
354
923
729
845
472
701
159
192
467
889
196
962
70
405
757
682
442
288
562
479
792
784
568
559
346
693
652
776
516
118
971
707
61
6
56
242
668
486
357
772
339
771
InsertionSort - 0 steps
861
765
717
83
12
113
890
456
125
659
84
178
819
964
402
375
884
15
501
475
324
2
969
645
614
180
612
248
865
887
941
16
616
577
116
672
944
232
283
491
7
181
349
839
860
17
851
508
316
616
68
663
891
187
726
428
150
989
323
146
17
970
301
125
82
499
210
114
23
726
670
302
861
473
631
1000
762
293
827
845
643
69
744
188
996
132
537
853
989
99
412
249
324
923
516
79
320
14
307
309
ShellSort - 0 steps
553
978
284
358
891
401
422
826
514
315
406
310
614
893
761
258
729
468
499
232
657
657
609
318
883
840
270
528
209
451
151
547
161
516
416
467
963
393
659
61
669
605
407
618
406
686
597
107
188
546
522
845
244
109
559
200
914
952
588
793
319
79
84
664
333
38
828
484
814
655
400
438
740
942
327
857
292
486
180
493
267
325
336
931
7
579
665
995
107
814
877
817
553
506
757
694
554
145
262
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