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.

256
436
174
485
644
642
720
252
878
755
815
9
666
503
404
823
497
488
793
714
299
226
63
717
975
501
637
201
877
34
441
32
789
723
795
705
975
447
566
857
979
424
267
914
15
809
456
56
617
388
716
540
81
281
296
123
639
487
435
727
860
24
990
229
599
491
499
551
603
539
337
313
347
429
97
114
113
329
467
117
951
467
34
549
94
702
690
266
673
735
859
229
682
862
932
353
428
187
532
160
BubbleSort - 0 steps
830
681
559
541
309
979
191
711
565
578
433
412
475
222
479
70
811
132
728
804
529
705
676
325
588
285
400
308
795
623
622
458
548
943
255
204
162
916
57
621
878
519
824
480
591
150
295
235
337
247
906
150
234
824
54
995
767
558
776
93
708
385
738
389
345
794
221
195
368
86
282
833
701
913
290
777
995
49
125
655
755
213
596
539
82
481
711
902
77
490
861
736
522
437
264
508
266
288
192
124
InsertionSort - 0 steps
70
779
529
523
100
391
288
113
930
51
136
174
701
812
637
813
634
913
108
262
732
629
392
489
475
228
251
281
932
573
795
587
303
971
157
788
704
381
117
886
575
644
676
901
662
237
183
196
934
123
975
483
9
655
875
652
394
204
840
565
427
157
95
884
626
185
339
629
165
492
417
776
369
398
744
62
739
298
686
791
536
697
112
320
895
230
449
354
105
826
989
662
965
62
462
876
688
65
965
282
ShellSort - 0 steps
297
120
329
798
27
150
125
490
277
180
978
942
972
311
490
875
112
624
431
848
766
496
611
547
704
604
526
880
33
163
315
310
401
419
628
645
74
696
612
35
323
209
699
699
901
175
798
234
251
517
447
711
521
32
229
740
183
590
643
338
312
75
987
829
671
701
491
96
690
187
786
1
15
18
151
901
563
534
499
763
750
565
796
679
874
816
438
779
934
730
271
413
771
145
934
306
69
217
771
585
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