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.

202
601
14
806
341
553
216
556
591
924
779
438
380
410
451
376
497
166
697
660
287
370
219
898
856
541
552
41
7
650
782
388
95
824
534
714
697
287
161
87
946
894
738
857
592
635
657
336
284
144
520
530
770
828
540
641
37
878
526
396
465
212
649
929
130
261
140
283
699
295
372
602
952
790
161
106
914
906
907
436
972
438
576
156
144
306
939
211
549
154
577
762
962
482
59
844
394
503
374
717
BubbleSort - 0 steps
585
461
739
371
773
17
311
955
46
888
597
708
402
354
391
478
534
27
722
435
375
206
685
127
233
808
649
760
683
383
921
480
609
922
92
191
476
22
964
112
726
670
257
995
386
213
562
941
201
408
943
251
142
489
69
19
584
884
227
676
496
72
287
962
51
225
440
191
271
272
297
335
148
1000
418
897
766
932
202
381
798
419
790
808
77
527
931
968
241
164
332
693
24
727
848
92
288
349
628
876
InsertionSort - 0 steps
741
510
484
899
547
168
514
419
87
212
667
847
316
136
102
285
364
207
652
573
802
524
132
786
922
296
754
407
851
394
366
85
940
895
142
890
781
645
820
140
901
332
361
949
525
534
568
229
163
78
703
305
432
234
982
154
865
254
560
626
488
953
247
154
293
311
809
57
293
720
86
162
305
907
3
63
409
524
816
238
555
673
837
12
686
993
94
931
125
968
139
551
223
772
912
805
129
255
451
996
ShellSort - 0 steps
841
887
417
317
795
778
702
479
622
47
493
538
93
575
447
689
572
713
135
780
469
262
735
540
296
947
286
724
883
598
332
36
510
777
990
652
423
393
457
109
447
802
181
889
484
789
25
761
295
190
178
781
73
721
840
632
95
431
540
783
812
698
317
165
373
883
504
773
214
145
540
682
316
458
730
483
765
674
746
686
808
624
812
329
740
322
617
177
731
402
220
164
914
760
445
460
248
176
347
917
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