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.

527
229
55
410
435
837
329
271
547
194
223
48
90
482
383
793
37
381
103
271
690
9
481
487
84
328
713
293
628
417
128
101
421
511
396
301
994
676
909
316
3
607
411
693
349
295
536
351
965
570
578
630
868
637
699
297
890
349
845
729
976
606
123
479
551
138
492
376
696
347
140
964
505
668
872
534
391
675
740
525
641
768
121
424
173
851
398
320
94
26
918
56
921
11
797
456
208
972
900
173
BubbleSort - 0 steps
893
268
916
847
518
777
408
840
789
274
331
813
499
588
482
232
262
776
483
145
297
79
304
331
769
842
36
701
308
39
213
850
826
917
200
724
882
398
678
835
991
784
325
990
606
124
897
696
858
378
858
175
671
362
28
467
477
851
31
478
876
165
26
637
410
204
535
117
521
987
407
263
426
492
553
525
21
314
27
560
473
339
60
948
755
704
568
585
795
212
693
606
669
569
967
377
876
56
304
706
InsertionSort - 0 steps
422
683
427
339
147
85
528
488
697
620
919
578
853
972
350
368
132
398
26
339
323
731
43
677
562
532
381
170
402
520
871
937
472
40
472
823
393
679
433
31
607
727
35
21
743
822
963
293
8
475
631
231
292
75
637
945
636
905
171
612
911
757
103
98
109
764
283
550
649
784
611
328
555
319
101
706
742
88
236
844
806
703
596
809
399
189
511
927
135
43
782
141
668
645
470
764
382
176
478
675
ShellSort - 0 steps
334
2
352
695
768
307
264
404
70
327
691
308
354
8
175
619
932
826
62
962
760
209
791
256
548
156
940
65
881
924
277
923
1
759
39
847
780
219
958
698
167
88
377
737
701
842
300
355
690
207
321
692
279
896
71
695
59
660
611
199
731
582
335
432
440
966
721
582
742
626
883
458
242
40
840
113
683
156
745
140
569
99
1
741
802
361
12
457
420
522
523
526
313
469
673
380
310
825
32
555
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