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.

474
919
768
552
336
445
78
382
195
40
636
716
214
990
899
913
706
271
620
943
135
275
780
511
781
267
834
551
554
438
721
140
829
460
600
847
377
961
25
143
852
457
659
317
62
627
817
198
626
743
950
200
189
358
489
387
36
285
443
738
289
482
284
603
404
76
121
357
864
954
628
155
13
205
344
497
153
232
752
631
878
851
595
590
860
466
698
268
154
690
121
215
836
584
85
502
986
518
627
442
BubbleSort - 0 steps
860
322
113
496
667
493
928
174
201
922
189
507
70
883
487
350
640
107
721
565
960
837
320
192
308
595
847
469
423
380
309
566
22
37
683
224
920
37
245
71
373
73
441
453
3
116
275
14
972
422
198
192
729
309
450
842
947
442
409
714
708
247
741
912
666
126
344
651
959
142
944
288
66
183
68
628
654
993
680
575
630
137
36
226
614
726
424
833
197
947
316
600
343
337
976
278
546
350
267
926
InsertionSort - 0 steps
410
769
583
313
263
803
179
731
1000
805
166
376
830
745
971
224
933
222
375
143
818
149
569
244
294
221
468
535
314
509
367
861
599
837
928
927
866
864
336
875
153
794
775
534
584
955
625
292
93
907
957
532
123
563
3
550
693
594
479
879
841
873
204
218
769
319
993
304
258
253
684
114
477
486
638
900
992
237
642
728
905
880
22
355
995
985
615
257
341
887
806
769
565
895
982
92
184
734
68
56
ShellSort - 0 steps
305
326
748
107
972
123
978
175
773
885
186
103
297
52
434
330
765
307
520
942
360
189
281
85
449
373
161
380
784
291
582
960
650
811
760
471
599
849
722
498
454
900
700
435
743
576
979
426
645
622
36
581
514
102
707
57
362
21
664
530
46
703
231
635
771
238
68
691
113
652
773
121
274
362
998
780
746
340
48
558
258
565
836
67
243
712
571
380
500
23
997
10
623
863
959
714
940
385
878
795
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