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.

29
143
184
377
273
493
431
521
822
301
122
848
271
630
214
642
523
637
683
536
145
478
796
807
744
409
707
759
438
144
489
114
44
511
786
36
384
79
526
23
141
684
932
450
877
751
206
89
585
956
933
412
289
716
94
384
226
151
115
366
685
159
768
710
38
331
155
526
608
561
275
784
851
667
865
833
414
469
825
617
505
451
132
500
552
390
598
453
40
588
114
696
945
519
700
137
15
921
190
47
BubbleSort - 0 steps
364
602
465
880
334
868
871
622
413
12
541
835
542
202
592
494
500
673
894
353
538
382
388
267
329
193
865
305
88
901
687
607
948
812
890
329
575
710
750
204
280
587
38
749
424
85
944
498
604
218
497
510
973
355
543
646
182
330
105
124
572
720
675
235
183
811
579
638
480
544
234
165
50
619
773
278
255
453
621
509
911
806
504
665
981
110
89
848
141
571
102
569
908
375
988
650
648
175
351
296
InsertionSort - 0 steps
499
290
823
556
186
626
813
426
803
380
312
576
310
412
816
561
741
845
536
39
176
439
925
244
999
252
495
783
161
994
287
894
552
280
215
977
654
726
518
249
677
748
718
97
755
633
22
544
595
615
433
353
102
907
478
421
940
299
759
649
775
48
454
386
983
811
616
28
202
534
434
671
313
350
889
748
62
6
538
8
445
692
657
867
434
626
179
938
898
762
514
341
670
689
835
625
12
204
192
316
ShellSort - 0 steps
250
583
736
530
871
971
83
725
499
802
182
158
236
276
352
280
731
446
66
267
189
84
124
783
813
593
348
993
883
791
707
890
629
996
758
399
192
953
24
836
4
998
443
934
440
777
559
943
274
275
347
29
859
648
247
776
673
460
986
508
123
171
577
258
238
587
370
989
630
419
330
245
2
407
155
297
634
308
361
100
459
102
907
270
636
170
538
286
275
544
675
543
94
597
839
555
103
366
484
50
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