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.

268
834
963
25
816
572
39
541
151
153
97
17
385
878
6
382
492
416
779
227
135
351
851
220
480
735
131
238
28
516
54
73
480
889
816
537
647
395
474
984
365
682
968
231
935
945
160
156
381
5
347
39
254
359
231
324
177
43
188
426
27
153
270
616
946
819
772
831
63
595
371
903
428
582
347
694
673
868
101
150
478
849
609
792
722
517
101
794
35
921
444
997
960
163
385
368
925
466
124
751
BubbleSort - 0 steps
360
607
98
955
580
319
169
326
962
56
725
388
253
765
740
508
817
998
567
594
359
738
557
387
249
645
410
881
114
350
149
49
887
167
82
608
165
945
430
415
270
276
926
916
476
968
6
391
577
998
501
471
729
643
631
315
489
311
410
132
76
111
817
124
252
802
670
575
136
208
893
686
662
593
373
577
449
617
502
782
448
850
759
699
946
131
756
448
212
326
589
135
403
231
613
635
210
430
664
877
InsertionSort - 0 steps
296
268
208
644
706
106
869
657
457
18
796
874
525
391
918
743
934
266
715
856
451
869
918
573
428
271
542
503
709
567
896
860
678
862
90
16
341
754
829
380
804
228
249
717
863
408
262
944
142
36
863
247
827
550
337
456
494
324
212
855
50
239
844
470
459
870
667
48
490
573
435
529
970
888
500
694
47
61
394
208
457
1000
821
750
217
267
525
554
855
841
294
308
461
73
981
150
783
100
493
903
ShellSort - 0 steps
442
834
197
555
183
657
694
162
40
370
959
84
292
369
598
176
634
451
755
980
622
462
372
466
302
208
500
679
781
893
526
467
787
212
169
162
121
866
868
179
142
264
277
875
878
460
793
19
26
593
592
925
760
874
575
905
14
826
982
696
466
485
733
305
510
865
867
327
915
331
644
740
905
404
203
145
192
204
569
383
798
97
519
594
239
420
800
20
850
735
269
975
70
185
956
431
240
759
77
459
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