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.

62
239
408
644
634
801
999
48
419
99
745
632
474
981
669
546
136
728
518
692
736
51
824
508
204
568
242
889
923
387
689
791
559
143
539
475
946
212
179
953
267
445
292
397
488
625
580
735
7
847
910
788
421
718
165
931
754
876
740
591
121
376
630
62
28
523
195
398
565
646
988
697
941
836
504
996
986
999
465
8
175
938
759
1000
641
668
1000
580
238
230
273
788
838
219
837
106
91
566
208
850
BubbleSort - 0 steps
861
593
192
862
160
88
256
467
974
87
594
847
123
763
418
590
184
601
277
87
775
181
508
321
473
100
370
283
338
810
625
477
891
722
932
337
108
661
212
183
954
22
138
855
461
64
406
41
925
116
342
424
644
836
108
446
714
977
474
853
584
640
19
141
289
933
618
11
346
81
906
116
650
338
687
686
10
890
182
901
411
76
820
234
27
799
292
212
668
887
921
898
177
667
715
91
672
636
872
271
InsertionSort - 0 steps
427
552
406
36
432
711
624
115
522
176
493
903
331
41
929
275
875
385
452
812
619
798
14
393
367
345
377
93
29
859
350
938
353
589
947
98
123
661
459
237
418
701
383
215
63
816
442
336
20
394
975
980
919
873
315
436
874
801
49
164
794
268
807
101
287
509
835
651
275
828
233
16
272
351
83
841
20
823
374
220
682
414
36
235
213
596
560
563
795
452
856
822
365
940
834
681
8
959
771
919
ShellSort - 0 steps
139
202
51
579
444
467
912
144
819
793
616
233
184
702
63
181
940
192
82
628
479
12
803
963
431
394
599
580
166
109
198
215
670
506
717
115
496
300
36
879
662
326
181
219
290
11
267
591
3
728
849
227
123
24
354
604
96
155
678
486
736
696
682
508
289
118
139
837
216
905
679
396
3
726
85
224
570
400
401
938
529
375
782
407
478
480
254
759
734
251
110
16
475
619
542
997
707
946
101
154
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