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.

75
271
867
891
889
324
176
787
368
725
481
258
47
903
614
178
905
674
568
494
122
310
84
992
410
951
48
266
787
476
398
129
138
615
541
480
638
484
594
938
896
445
989
133
298
127
196
602
17
857
781
993
530
831
740
743
70
76
53
534
49
543
145
381
467
689
97
550
799
446
52
879
683
611
673
60
172
756
917
991
381
466
202
552
518
328
82
201
915
542
50
775
880
712
900
355
344
280
146
658
BubbleSort - 0 steps
146
444
665
681
820
190
281
309
644
156
805
10
508
125
711
323
129
932
865
811
161
344
915
233
918
367
231
233
21
912
936
346
799
860
401
241
887
467
234
539
43
715
336
144
111
375
614
366
523
265
587
948
378
378
9
429
573
437
708
697
326
636
720
131
825
867
433
353
380
299
367
171
700
144
471
926
747
838
41
217
618
731
382
219
432
96
750
324
957
49
498
767
809
523
159
211
462
449
172
410
InsertionSort - 0 steps
25
378
317
139
385
687
848
759
349
882
409
734
36
998
781
315
738
97
108
150
163
223
431
231
235
217
626
438
833
930
586
406
327
199
22
470
429
541
694
389
695
215
706
265
377
428
409
432
59
138
392
438
833
820
452
501
754
182
671
467
824
958
158
601
633
954
672
442
27
745
44
749
654
579
942
412
299
969
628
487
594
515
353
201
19
533
288
517
34
85
660
912
432
800
894
505
991
288
438
471
ShellSort - 0 steps
874
58
451
311
671
32
404
291
434
168
804
376
739
568
571
154
116
532
389
843
939
267
924
333
772
747
671
336
239
91
712
265
654
153
728
874
442
66
549
810
816
301
386
963
860
165
55
766
767
477
893
111
885
192
242
94
268
646
737
957
680
621
196
297
931
455
7
775
247
645
646
367
653
197
457
452
118
844
209
361
461
467
490
180
168
944
760
420
92
991
499
812
737
913
426
661
737
841
240
763
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