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.

498
52
563
334
823
934
514
138
699
563
427
747
573
402
961
733
963
232
381
688
396
61
372
508
826
569
300
593
756
903
924
406
12
566
123
400
823
185
558
894
427
250
637
962
251
913
520
155
445
237
992
509
301
164
131
591
706
57
728
744
355
505
508
901
814
70
464
833
803
677
343
470
187
64
726
644
453
859
534
148
135
406
913
428
952
69
567
186
237
729
723
912
496
45
528
64
982
186
715
712
BubbleSort - 0 steps
614
379
154
74
781
727
935
961
573
834
228
535
705
917
543
487
381
784
322
757
772
563
1
236
455
270
602
916
946
639
563
532
800
716
437
309
305
861
869
895
450
327
155
177
315
458
563
993
723
92
100
95
463
786
291
514
593
460
570
177
979
219
721
185
378
122
507
91
114
907
313
1
958
686
341
184
832
901
611
854
344
686
196
439
353
862
978
576
869
370
142
746
524
120
970
696
751
368
852
853
InsertionSort - 0 steps
452
169
489
968
289
85
721
548
294
760
730
591
464
561
518
214
290
674
500
582
769
623
125
319
596
879
622
99
980
849
183
925
206
840
500
226
595
905
914
791
986
45
809
571
231
447
206
391
867
900
961
362
700
428
7
146
441
298
100
148
47
843
87
848
900
537
904
198
768
100
614
676
865
605
840
965
693
270
815
562
112
755
661
618
891
348
418
261
418
81
985
566
924
672
697
140
455
424
853
141
ShellSort - 0 steps
202
318
385
593
216
884
958
624
325
859
604
141
204
577
532
182
851
749
165
267
491
76
988
9
631
369
732
361
995
254
393
347
129
32
405
539
927
154
23
644
54
900
51
890
323
129
843
196
871
303
253
559
281
101
567
794
108
730
243
647
661
750
782
809
417
7
840
309
30
152
350
573
150
441
226
195
315
394
108
350
518
126
656
290
866
782
946
947
210
427
130
142
759
432
471
724
711
606
573
403
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