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.

314
31
108
599
874
776
812
111
271
951
671
387
47
569
756
930
352
81
108
795
796
650
730
704
101
984
178
341
447
408
543
475
724
214
184
786
3
786
547
105
800
417
397
466
930
563
336
258
747
889
592
68
985
603
857
271
970
968
132
967
258
154
275
179
70
31
762
405
94
287
590
543
418
96
589
441
278
622
530
268
954
641
183
714
995
306
622
827
735
505
42
217
413
274
784
489
882
455
985
405
BubbleSort - 0 steps
498
14
310
668
653
628
261
861
652
369
594
237
885
823
621
18
514
562
621
35
752
328
441
863
209
992
913
127
228
473
873
595
313
932
287
510
993
101
204
240
309
791
353
846
815
762
149
542
135
996
927
361
170
880
22
86
645
657
41
31
596
380
747
349
51
674
158
697
738
172
11
865
501
950
421
612
864
654
308
608
363
890
533
300
696
833
840
826
602
709
998
633
478
668
524
828
275
77
949
370
InsertionSort - 0 steps
906
156
959
544
726
528
350
597
779
442
755
831
468
306
549
432
998
853
26
155
708
42
285
284
847
761
926
633
200
179
619
82
791
864
813
149
887
179
559
926
504
987
998
238
720
672
721
461
691
797
680
930
65
229
982
714
821
969
444
508
580
232
242
310
736
616
542
610
152
721
440
526
68
160
97
2
220
55
1000
896
104
2
732
243
221
992
347
463
360
706
447
622
843
323
20
848
621
695
573
462
ShellSort - 0 steps
723
139
399
760
113
9
764
286
175
198
774
464
80
277
421
853
702
451
148
817
134
344
154
534
246
940
281
818
777
317
821
270
67
740
202
837
619
463
773
159
357
647
28
763
979
377
742
255
98
23
157
433
262
896
673
890
254
278
90
857
757
729
886
178
450
415
347
414
95
312
926
433
974
812
668
676
182
510
388
538
578
524
744
91
11
186
182
279
96
316
121
6
982
578
958
206
170
581
941
491
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