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.

131
179
870
124
124
716
465
300
282
148
808
858
821
915
611
516
730
751
316
110
960
739
493
53
514
559
278
117
403
825
420
924
439
577
996
639
593
162
778
396
847
854
504
205
620
538
43
826
317
627
835
298
847
940
33
917
347
705
643
115
909
444
415
764
323
519
68
450
590
826
258
639
177
191
652
122
404
95
423
90
945
436
950
102
647
101
727
532
1
504
627
413
496
286
220
497
383
321
562
911
BubbleSort - 0 steps
655
829
393
203
395
888
204
407
751
447
526
26
404
445
384
601
176
341
565
51
986
215
416
550
303
603
761
578
100
812
933
368
172
61
237
470
215
47
773
848
510
244
135
923
386
629
852
665
754
379
893
680
377
219
627
726
980
513
143
29
623
791
595
59
761
443
48
855
193
812
120
661
556
728
238
328
402
242
642
206
638
56
510
394
789
592
968
232
107
745
755
625
649
650
278
822
618
599
19
627
InsertionSort - 0 steps
429
570
954
13
703
736
935
445
872
515
393
221
244
169
20
38
762
484
792
737
559
427
227
10
593
305
791
437
950
60
844
171
644
185
146
864
81
936
42
410
357
544
617
166
485
355
536
464
597
45
559
696
144
963
20
82
858
714
246
449
122
228
757
442
394
227
535
301
838
827
352
479
74
39
497
134
286
318
618
329
37
45
238
749
17
137
372
824
389
889
722
847
513
127
727
521
228
682
544
452
ShellSort - 0 steps
538
36
159
53
529
4
46
934
10
829
985
333
789
147
990
48
776
219
774
963
774
613
355
579
376
425
807
230
832
487
382
607
533
302
757
805
762
764
404
476
974
846
202
854
503
522
201
590
364
744
924
47
693
653
160
143
575
216
185
553
122
465
386
439
974
288
546
896
178
2
505
53
733
160
3
767
892
285
373
520
407
824
824
95
133
712
361
452
673
532
79
253
869
785
199
138
37
337
886
369
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