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.

582
906
66
496
59
607
515
642
982
634
878
25
799
689
695
163
203
308
961
941
331
43
160
179
791
908
643
915
334
150
39
979
421
716
194
603
355
601
518
52
492
543
774
563
628
307
620
793
689
980
481
23
251
76
793
986
337
275
689
559
260
887
230
791
812
653
962
565
814
735
36
442
859
850
452
38
883
492
881
641
33
969
583
403
740
785
238
525
971
814
960
373
162
269
574
769
117
114
478
858
BubbleSort - 0 steps
440
566
955
96
801
992
768
819
570
979
651
558
265
517
280
930
674
523
474
892
627
829
828
298
354
549
36
705
650
487
391
697
529
213
698
241
889
284
670
27
718
182
908
491
545
911
558
939
929
759
126
498
326
123
914
184
253
52
783
641
955
407
971
56
838
607
66
155
59
432
696
690
466
930
158
220
471
221
603
739
975
529
646
12
231
439
670
584
107
205
164
925
725
333
480
506
263
874
234
544
InsertionSort - 0 steps
50
315
953
791
611
69
199
894
514
569
559
289
485
452
995
907
678
551
758
870
700
515
728
547
579
368
967
786
586
275
971
995
295
103
369
205
918
627
181
715
285
123
191
827
448
113
801
377
993
517
769
789
544
486
120
284
352
645
673
239
231
632
761
353
84
727
660
922
111
982
256
46
897
29
759
659
804
459
177
287
218
552
824
55
746
740
498
8
237
150
145
795
266
495
131
848
970
673
913
628
ShellSort - 0 steps
544
950
144
485
675
170
917
954
572
546
577
560
462
533
790
540
998
347
324
590
533
347
114
221
916
657
561
168
799
419
815
763
449
266
289
257
294
958
9
398
914
1
681
465
20
351
309
362
1000
17
303
142
701
151
154
492
782
490
733
343
534
82
82
240
471
557
677
626
34
677
951
300
430
593
598
597
912
440
231
862
49
699
821
636
897
56
307
356
544
413
972
324
439
957
94
467
797
851
440
610
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