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.

600
892
996
730
216
675
701
37
747
204
623
411
824
508
75
93
189
314
362
57
172
454
130
706
926
867
394
211
861
764
505
823
521
925
663
645
519
111
932
318
667
234
490
586
954
242
301
454
881
104
88
606
842
691
212
933
195
799
744
310
414
334
469
717
33
32
323
644
351
789
87
327
229
83
418
811
154
767
931
696
744
25
376
656
356
80
722
327
907
968
609
202
892
389
423
268
498
117
153
71
BubbleSort - 0 steps
637
706
552
76
751
302
137
475
769
24
32
584
100
892
973
277
878
756
287
426
293
13
54
453
161
308
778
460
322
427
473
598
437
840
226
468
728
295
275
732
832
423
322
733
490
303
573
956
442
846
395
783
285
278
288
798
520
824
699
382
745
579
277
115
800
361
139
459
755
469
89
776
291
750
208
421
422
123
55
730
138
466
589
31
159
425
768
581
233
874
242
180
370
100
402
931
336
459
214
14
InsertionSort - 0 steps
538
971
441
19
930
988
111
698
397
894
14
435
155
881
866
96
877
752
695
748
453
150
877
551
899
154
610
131
657
221
188
431
931
622
606
344
745
471
517
753
21
156
46
821
665
614
517
815
637
320
19
423
458
895
625
418
92
884
313
47
877
6
935
104
641
160
470
733
676
608
190
987
635
999
128
695
624
660
151
714
184
677
647
743
72
199
709
216
655
828
591
14
788
388
548
258
527
830
710
676
ShellSort - 0 steps
542
541
911
253
193
208
275
878
628
350
325
499
483
638
113
657
462
588
466
844
610
803
168
412
413
736
579
454
218
370
116
875
630
424
672
353
69
836
300
338
163
578
478
715
491
565
348
6
94
127
627
746
524
567
96
240
776
852
305
943
19
110
516
104
580
525
744
227
688
252
273
867
567
495
75
566
503
387
733
252
480
984
481
715
771
146
264
661
225
411
927
505
881
513
392
639
438
950
35
136
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