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.

432
950
848
269
440
864
231
273
871
760
828
881
446
655
736
930
303
482
430
650
731
593
883
958
862
719
525
59
84
585
283
194
131
175
79
132
949
599
339
821
79
210
109
849
828
858
215
17
382
925
205
611
329
335
982
629
461
476
353
668
427
726
696
713
275
23
866
120
350
528
768
747
608
687
70
246
287
511
681
89
747
774
868
378
247
19
654
485
451
261
223
715
578
59
382
511
582
743
558
280
BubbleSort - 0 steps
32
947
715
108
846
281
984
700
706
351
152
484
981
781
780
218
455
314
100
950
906
250
117
39
445
915
367
498
434
562
379
390
339
862
828
417
880
341
309
498
451
15
821
519
254
771
591
956
328
202
169
206
102
822
893
840
29
826
185
256
667
896
147
878
939
708
130
430
649
885
353
588
387
605
978
82
205
652
613
169
495
885
244
548
32
570
821
623
321
475
269
796
668
701
401
308
335
221
226
772
InsertionSort - 0 steps
44
994
749
419
703
512
883
641
765
497
939
650
908
39
692
24
412
490
374
564
410
310
270
87
760
225
61
125
772
506
974
109
607
204
862
260
758
766
239
28
671
149
767
551
721
448
427
882
593
248
657
169
2
453
93
718
703
831
414
748
502
245
659
745
273
45
148
60
982
224
255
425
415
695
101
471
782
503
546
61
549
570
926
535
287
195
169
861
143
893
37
530
367
964
160
315
125
659
341
484
ShellSort - 0 steps
944
34
714
423
75
127
603
587
956
248
851
135
905
471
336
980
200
342
810
325
971
797
251
621
510
749
690
615
268
69
479
30
956
135
932
670
502
91
141
930
476
157
384
748
5
955
991
735
320
997
423
102
364
306
216
590
791
30
663
21
407
554
943
920
359
22
200
507
933
666
883
205
693
215
783
971
78
443
307
496
718
586
682
951
67
834
981
339
882
644
419
992
870
99
34
588
859
706
152
688
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