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.

541
383
951
343
535
979
236
556
1000
457
575
168
318
723
879
200
285
784
840
897
464
244
953
388
190
921
98
624
264
307
734
962
714
72
704
290
636
263
987
142
917
698
201
586
330
101
832
753
773
560
572
943
28
927
222
765
445
260
711
417
155
384
924
502
280
679
179
647
711
709
829
834
901
11
207
91
496
583
122
770
880
472
247
538
810
36
754
481
428
400
545
679
530
588
355
285
44
498
921
991
BubbleSort - 0 steps
628
394
406
457
259
99
30
566
133
790
206
638
107
754
140
923
851
293
272
388
964
52
713
182
812
835
899
715
323
12
929
473
90
402
150
671
868
131
116
190
884
540
807
462
766
200
520
8
986
912
365
806
487
188
3
889
309
392
898
246
94
621
802
436
996
439
606
569
156
587
1
159
614
21
525
115
103
902
930
644
696
348
586
550
362
854
814
825
122
547
228
634
706
601
663
751
633
291
713
145
InsertionSort - 0 steps
421
626
798
263
771
437
289
847
805
846
718
976
963
69
870
354
565
552
659
965
575
282
210
880
551
6
199
515
178
930
741
451
329
153
244
378
594
254
188
268
472
696
88
711
739
117
589
813
858
948
718
723
223
24
356
45
540
177
321
431
408
856
844
9
159
361
317
807
895
290
281
634
602
351
334
208
464
737
460
208
339
352
395
607
467
781
769
917
134
725
129
177
825
620
526
220
814
104
228
766
ShellSort - 0 steps
982
147
100
152
899
116
906
148
862
639
932
920
774
829
853
883
290
140
307
517
836
687
571
521
176
451
257
370
954
402
518
672
68
241
152
651
568
6
784
195
658
694
354
419
449
806
943
965
166
318
633
769
396
310
723
193
534
114
302
808
935
670
399
193
696
896
787
571
578
252
279
847
202
116
954
148
198
715
334
950
845
468
866
374
195
440
445
229
758
491
455
680
65
864
890
918
989
871
769
713
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