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.

409
855
278
980
63
212
242
947
172
477
148
331
346
253
81
8
892
460
330
429
785
18
461
358
42
277
572
142
807
502
786
168
176
281
417
748
917
766
533
973
930
541
64
150
564
521
982
244
681
731
420
104
895
388
820
644
195
405
259
873
489
78
733
722
736
857
43
116
346
830
404
270
233
92
982
34
364
334
194
637
87
595
984
38
498
733
55
305
251
197
611
476
736
479
242
952
490
74
930
182
BubbleSort - 0 steps
398
892
1000
249
99
538
17
161
572
365
112
924
985
90
744
123
498
236
758
839
403
944
708
408
751
812
908
967
522
197
155
307
385
591
426
495
636
741
844
222
157
347
387
724
312
912
618
442
41
718
233
694
332
766
211
56
883
274
101
681
314
252
233
913
486
512
346
660
474
83
358
769
357
616
148
187
626
565
916
599
921
792
861
268
718
187
658
379
675
723
582
690
426
139
865
630
601
881
260
187
InsertionSort - 0 steps
81
574
96
264
131
586
948
516
526
360
798
380
723
495
8
697
366
992
338
169
708
230
590
570
606
825
266
474
446
563
482
732
399
551
421
929
334
627
38
875
852
192
755
293
964
438
413
78
675
956
596
497
551
32
301
48
435
707
420
424
419
119
112
745
550
937
383
723
657
910
290
95
223
924
421
73
745
399
517
321
542
492
574
439
338
284
741
281
269
760
292
60
400
574
360
545
212
720
914
429
ShellSort - 0 steps
407
995
18
549
413
961
302
772
994
217
154
493
563
114
107
977
525
128
529
354
482
606
283
117
499
211
309
272
983
91
77
273
266
912
120
353
370
225
620
784
316
772
630
858
455
730
269
580
226
782
244
288
140
83
457
740
521
672
935
279
837
255
298
715
797
629
94
446
118
384
716
551
571
126
471
85
335
113
39
122
528
845
81
257
979
995
849
355
161
830
359
406
805
612
977
808
679
116
862
517
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