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.

241
264
471
76
263
898
542
130
243
995
66
722
739
931
501
148
594
299
436
565
339
636
652
850
450
982
452
58
105
903
97
545
315
478
271
801
710
898
722
998
409
316
753
18
880
837
283
279
817
288
622
638
905
428
392
475
891
444
987
921
170
525
541
426
304
138
50
128
54
965
889
483
840
871
650
827
604
154
15
760
93
125
721
182
498
617
869
181
26
52
469
261
56
932
730
826
347
952
243
634
BubbleSort - 0 steps
241
462
282
5
383
136
191
481
571
446
73
658
778
720
241
750
247
693
544
46
490
743
384
526
483
38
770
129
945
371
662
217
231
474
544
81
94
845
49
725
99
293
912
324
13
208
422
567
543
782
658
381
73
681
839
411
47
116
650
513
625
757
266
242
223
30
781
934
20
85
922
971
785
590
886
700
816
862
692
695
571
304
522
647
949
556
48
935
742
596
97
301
875
185
314
924
802
494
368
501
InsertionSort - 0 steps
552
898
208
735
58
406
317
739
565
270
896
71
572
97
150
256
746
222
30
638
189
969
56
340
187
609
268
444
761
277
731
551
780
106
251
584
421
899
748
887
2
260
227
101
875
79
963
545
402
363
893
748
365
633
621
425
293
293
740
538
603
711
597
553
489
353
684
234
144
257
221
58
169
142
29
52
622
849
467
73
20
789
852
345
142
51
683
627
886
425
422
922
784
158
687
266
26
85
211
739
ShellSort - 0 steps
862
776
887
791
409
664
319
218
188
46
11
88
882
564
191
45
754
599
55
712
394
297
915
296
674
660
244
868
716
478
324
315
563
858
95
297
691
932
243
278
729
424
135
187
350
733
229
294
963
194
895
608
693
681
817
456
860
939
187
186
312
706
4
180
300
988
504
288
489
420
945
293
620
847
87
585
437
88
563
627
825
233
41
840
762
627
937
273
398
845
676
900
515
903
525
881
109
360
629
965
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