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.

563
939
553
423
497
806
772
692
763
3
144
987
573
274
881
367
305
210
967
772
415
345
955
38
494
379
274
239
528
635
316
89
815
585
628
802
260
815
397
279
651
777
262
155
789
657
71
853
333
418
523
182
279
940
825
818
826
985
168
150
572
340
172
842
141
131
484
383
822
712
361
940
416
234
917
880
929
922
706
569
192
763
770
588
30
298
969
187
87
880
855
562
652
29
662
568
396
273
395
816
BubbleSort - 0 steps
426
872
52
319
2
770
959
932
409
638
782
930
938
545
904
807
219
544
77
944
895
634
801
168
867
987
805
157
924
76
247
945
354
901
751
138
269
292
484
374
431
14
764
986
398
694
190
857
289
788
717
512
737
390
888
528
488
757
681
763
111
838
211
395
490
538
648
498
558
568
386
483
194
629
560
62
696
31
473
252
294
554
387
393
580
767
643
507
383
23
447
628
798
693
858
47
688
604
741
402
InsertionSort - 0 steps
67
304
326
73
712
987
294
568
4
812
653
953
833
125
972
42
397
193
537
982
254
464
784
987
214
539
150
436
360
720
264
261
543
461
37
464
35
383
154
570
579
768
146
449
461
63
81
89
89
999
712
360
658
232
46
376
608
108
408
6
197
984
926
386
714
460
156
131
940
499
197
766
839
596
282
932
846
314
922
679
297
151
851
80
418
78
683
787
249
149
351
491
901
521
446
398
528
379
976
834
ShellSort - 0 steps
168
553
616
669
128
526
538
344
475
591
19
261
483
766
588
879
986
311
199
751
678
245
756
110
156
510
30
848
288
567
575
918
412
737
531
113
40
315
259
465
917
367
199
654
8
320
253
258
17
774
876
247
558
726
311
198
368
167
281
670
567
994
29
332
880
613
902
893
256
448
68
624
182
470
245
686
516
215
127
730
156
951
100
787
306
170
640
151
502
213
574
978
77
411
127
220
827
76
280
246
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