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.

191
758
768
739
332
183
592
788
156
773
341
225
475
736
883
34
470
233
11
940
700
123
35
802
230
98
89
538
950
953
219
368
681
766
106
691
205
496
306
211
152
756
120
723
444
226
690
347
180
440
908
463
21
392
635
888
894
481
124
878
468
250
323
797
239
538
659
627
950
305
303
921
808
401
15
458
29
73
963
649
443
93
373
975
394
944
790
253
985
692
534
331
130
477
155
513
336
834
330
920
BubbleSort - 0 steps
568
204
690
212
767
246
929
384
939
309
118
140
355
567
524
658
627
708
487
543
931
43
825
385
900
965
9
599
548
832
743
409
626
413
98
14
47
312
145
545
974
731
21
354
79
545
546
301
326
431
851
901
336
68
226
234
524
779
87
306
747
29
539
25
147
254
448
297
137
278
572
51
711
576
807
443
111
815
874
66
796
832
853
414
668
452
77
591
239
939
147
847
498
469
841
208
863
87
162
745
InsertionSort - 0 steps
671
177
376
586
802
971
659
977
537
111
349
684
480
705
40
544
373
324
402
701
200
540
436
174
698
415
438
766
713
735
71
463
457
931
607
991
434
28
229
352
745
120
286
116
355
710
812
568
879
156
305
564
979
167
862
705
100
480
246
124
197
692
446
167
684
345
526
816
356
120
327
999
264
345
486
362
196
955
871
11
733
602
233
411
746
639
741
548
214
664
772
563
923
572
695
657
888
396
385
160
ShellSort - 0 steps
808
812
695
776
370
179
398
914
511
641
585
507
291
538
839
650
180
294
106
490
537
564
215
843
52
88
287
493
60
808
957
100
535
965
604
655
314
790
439
235
858
84
904
972
309
43
625
869
27
646
833
565
545
184
371
233
877
457
212
858
914
476
438
234
799
453
46
896
532
475
311
186
967
728
601
996
310
34
335
502
571
546
41
840
372
341
799
146
648
148
440
868
874
867
912
339
261
309
626
76
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