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.

373
126
703
165
637
471
849
133
64
445
32
85
414
348
753
443
821
799
501
93
998
702
596
809
621
733
525
744
630
59
977
516
271
897
485
277
213
555
706
239
898
133
748
606
613
712
196
714
426
386
928
727
160
58
903
949
899
661
561
760
266
534
587
468
222
58
913
280
239
481
393
81
964
446
58
306
318
630
509
978
704
766
934
527
778
198
91
422
55
635
721
647
381
43
947
776
657
128
604
83
BubbleSort - 0 steps
199
920
574
819
459
346
817
365
670
615
713
214
328
961
845
578
826
307
904
292
18
784
787
358
259
956
599
60
493
233
718
105
88
48
601
869
276
922
108
141
189
171
253
274
677
721
428
719
824
452
275
328
530
921
981
178
902
408
514
526
879
429
376
253
473
188
855
771
185
239
480
724
264
902
533
718
358
266
830
854
327
347
613
223
312
234
247
551
6
911
557
244
916
534
776
255
506
193
208
695
InsertionSort - 0 steps
481
315
76
562
181
936
614
175
193
236
365
319
265
455
521
405
99
935
661
877
578
650
732
74
813
494
996
79
47
640
215
328
938
300
489
366
404
585
945
638
828
385
856
856
706
547
757
214
668
781
925
652
427
887
768
368
413
785
206
668
242
110
143
205
534
643
955
2
180
902
916
935
917
242
753
294
101
674
6
664
886
164
340
854
496
703
40
9
839
754
195
154
77
217
732
65
63
817
293
458
ShellSort - 0 steps
479
840
146
170
50
52
307
614
401
418
649
48
801
27
454
367
874
573
84
145
170
301
409
529
230
965
808
497
168
680
295
736
376
588
967
423
333
407
489
309
966
869
750
217
788
662
56
403
520
658
230
456
380
176
326
762
789
738
131
379
153
475
336
300
976
500
69
867
71
887
378
185
954
670
467
425
740
255
98
615
857
148
148
350
642
572
592
410
429
786
241
225
605
388
575
287
319
351
137
3
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