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.

529
769
535
151
39
394
366
142
736
134
611
697
726
73
586
852
215
136
379
616
864
6
519
311
970
956
825
744
867
862
53
351
408
279
710
797
837
388
496
665
871
674
519
88
378
986
887
812
27
384
378
784
466
959
235
782
583
214
534
594
831
24
227
156
367
169
304
310
595
927
211
809
207
590
268
59
365
484
24
149
849
456
139
198
236
538
678
542
83
589
216
113
420
473
5
338
4
547
192
102
BubbleSort - 0 steps
694
822
795
57
394
823
224
748
385
917
299
456
535
982
900
585
150
979
610
376
992
3
745
55
32
488
905
783
521
291
647
617
263
573
827
415
433
812
702
43
845
611
982
999
555
51
455
323
427
668
138
412
314
552
105
285
261
950
603
175
156
584
530
21
955
103
860
131
621
846
568
699
361
871
752
285
802
179
207
866
598
738
299
626
141
837
506
235
845
633
398
231
702
376
240
982
511
389
257
811
InsertionSort - 0 steps
959
827
664
561
744
276
633
540
793
473
132
640
327
709
486
279
978
102
643
57
151
22
834
591
503
103
848
576
546
793
831
926
600
568
726
123
329
772
101
9
505
866
833
54
47
518
650
986
781
596
113
106
917
209
440
889
203
587
528
305
317
688
300
736
471
810
594
594
958
285
307
252
395
433
837
43
96
909
165
301
171
614
380
141
862
491
135
806
555
769
751
826
565
768
249
144
863
283
320
244
ShellSort - 0 steps
877
598
463
80
856
957
254
174
992
465
143
946
635
344
156
789
167
409
936
184
878
826
999
924
49
880
235
258
25
763
564
610
20
24
557
819
785
490
862
360
540
941
401
615
473
111
919
21
754
780
260
328
330
357
88
693
204
869
19
645
287
703
684
508
734
910
172
291
266
364
155
493
250
714
292
128
183
584
26
230
20
975
228
513
221
522
166
879
501
643
172
292
9
732
462
494
293
688
323
843
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