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.

871
605
426
659
114
20
473
165
265
569
399
88
736
61
124
929
17
75
231
60
366
510
999
751
414
441
619
93
44
313
281
751
999
480
448
173
150
697
253
23
191
583
555
778
698
136
826
276
135
117
669
396
523
669
424
420
200
642
78
311
455
822
20
456
586
281
126
658
775
883
650
752
826
669
890
343
110
185
567
324
599
239
340
512
783
353
87
818
903
984
643
620
186
315
103
74
925
375
261
337
BubbleSort - 0 steps
182
933
538
820
756
57
470
738
325
281
143
519
926
78
18
448
907
416
543
991
621
821
575
233
702
955
304
447
513
527
207
210
402
472
190
82
186
756
244
107
388
354
383
238
989
942
223
895
402
103
1000
767
788
692
886
950
281
376
791
925
504
680
546
704
245
176
752
200
802
354
953
287
870
947
470
44
62
24
838
525
887
811
548
177
604
167
641
25
524
484
838
148
448
120
518
50
688
622
310
250
InsertionSort - 0 steps
841
797
814
131
342
351
753
276
644
886
282
839
672
690
964
495
713
236
205
124
421
825
206
777
869
645
205
867
710
1
753
823
28
395
289
305
391
156
521
682
823
388
133
478
946
219
584
778
198
919
950
647
556
602
985
301
675
180
280
908
695
850
754
174
537
968
946
25
887
84
580
955
128
268
950
531
579
419
551
89
813
579
473
811
581
377
477
526
649
332
995
627
841
559
435
343
783
479
789
955
ShellSort - 0 steps
567
141
131
716
265
478
601
201
371
239
106
150
404
827
916
6
337
884
997
931
188
342
725
684
497
727
494
506
399
329
96
972
545
717
57
553
742
130
792
253
569
352
958
937
746
993
287
958
76
691
679
409
642
552
285
510
846
651
493
487
631
308
694
753
965
227
234
570
189
315
292
671
712
366
516
461
198
399
223
428
727
703
556
294
909
938
761
602
175
763
742
987
957
841
266
788
178
129
126
667
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