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.

331
528
65
159
239
436
561
953
564
962
760
195
957
508
948
562
644
943
413
667
437
813
2
815
392
590
516
765
837
374
152
755
259
988
710
197
944
266
450
145
516
406
81
413
390
515
604
260
635
999
902
752
808
728
253
109
575
788
48
783
687
303
71
659
835
756
599
552
44
834
403
129
628
225
36
462
505
569
194
716
617
744
646
800
442
906
694
82
869
644
508
909
883
558
637
651
648
425
304
776
BubbleSort - 0 steps
683
438
796
123
120
971
500
296
890
558
475
551
164
739
230
431
211
166
234
983
546
851
821
481
507
713
763
958
186
365
367
941
240
439
866
322
956
123
599
665
467
391
567
235
236
40
867
692
997
49
42
119
1000
548
975
725
660
458
25
32
901
843
918
956
235
260
842
311
869
823
523
223
148
350
274
796
475
540
646
257
862
36
549
154
757
321
876
823
647
825
481
340
94
262
904
852
812
266
615
101
InsertionSort - 0 steps
54
661
881
108
272
680
70
16
940
677
621
912
324
236
257
827
444
734
900
181
905
336
887
216
453
810
361
387
158
635
661
333
442
165
867
873
151
88
64
873
356
161
232
916
395
1000
280
98
154
64
602
168
598
574
336
578
240
313
839
886
380
378
689
732
48
676
380
907
979
765
150
586
295
341
450
100
5
344
384
617
548
165
197
123
337
601
551
553
326
94
839
799
269
634
612
619
386
530
245
255
ShellSort - 0 steps
949
920
879
803
604
761
84
455
840
819
1000
905
993
964
318
879
156
509
939
293
466
514
286
925
782
727
397
150
88
275
577
935
875
29
122
458
416
950
951
47
159
686
811
398
71
496
594
324
325
642
660
784
625
701
551
85
929
972
854
144
657
314
321
232
25
342
508
463
205
589
24
157
129
237
541
492
595
820
698
591
305
31
64
446
203
992
612
758
753
313
574
864
903
296
884
190
935
970
851
60
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