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.

154
304
767
287
871
34
646
351
996
438
957
511
718
105
231
236
207
14
956
221
851
805
331
620
624
549
132
32
382
184
235
389
551
526
187
826
958
615
880
6
671
959
929
736
837
806
714
117
482
148
903
760
487
553
219
127
599
872
251
995
865
362
930
783
90
674
657
111
240
758
20
837
15
389
421
672
741
700
896
290
377
249
901
357
457
990
128
940
552
178
797
213
927
12
137
878
378
442
140
441
BubbleSort - 0 steps
544
745
691
372
315
642
327
845
793
933
384
820
846
467
1000
453
924
552
896
704
751
404
52
735
278
697
248
403
72
830
806
308
302
35
510
865
705
238
193
464
47
788
444
116
255
248
216
451
196
458
29
951
389
747
30
329
810
173
575
842
386
720
250
954
429
545
694
450
850
295
932
80
31
158
188
191
447
172
914
373
919
762
315
305
738
797
296
577
874
908
845
311
685
62
567
711
924
419
408
697
InsertionSort - 0 steps
634
929
300
987
26
918
883
381
512
403
1
528
547
327
89
129
72
116
329
678
231
515
406
687
943
529
524
837
179
888
625
329
400
627
54
914
130
514
488
374
566
901
392
259
898
652
713
27
459
656
172
543
476
610
191
927
797
407
871
353
444
462
857
629
292
983
886
956
255
751
405
439
929
996
435
900
144
173
393
90
855
347
548
328
726
483
887
45
442
458
317
303
571
181
241
971
251
287
716
685
ShellSort - 0 steps
631
709
778
904
138
345
66
384
341
980
584
712
161
369
534
592
660
646
840
517
947
988
517
242
361
116
113
147
462
801
719
778
271
149
811
410
527
192
948
730
206
644
542
4
486
20
96
771
617
550
982
52
168
573
627
66
705
629
327
563
994
278
860
789
569
457
446
956
121
747
787
672
321
197
540
278
116
485
923
572
828
325
514
474
659
880
452
981
81
364
917
676
667
514
307
346
315
71
803
974
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