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.

837
286
820
90
921
995
855
622
10
381
307
810
778
86
778
805
58
907
180
690
898
463
200
101
555
489
358
335
310
765
791
932
375
333
891
998
295
564
537
660
871
476
479
160
430
700
354
58
769
41
648
688
598
398
661
510
397
143
952
654
735
262
288
266
148
325
704
954
903
636
824
618
402
18
305
537
993
678
193
785
785
702
912
313
682
338
775
153
794
308
989
153
193
717
585
977
696
814
461
123
BubbleSort - 0 steps
776
504
163
973
84
733
148
586
908
963
334
740
311
403
285
435
121
752
206
818
3
241
274
733
31
680
538
356
285
24
470
427
756
912
65
458
354
259
672
869
431
488
142
259
478
450
933
611
93
272
350
959
224
621
532
130
422
720
697
453
198
892
935
722
649
276
649
905
168
215
722
386
229
317
404
261
247
803
514
557
780
56
783
41
593
559
596
622
61
449
569
273
924
72
444
817
758
236
232
898
InsertionSort - 0 steps
110
549
805
563
792
795
800
67
389
504
308
435
71
619
184
527
521
165
714
905
688
75
145
66
427
931
762
42
354
544
497
554
864
503
379
780
941
411
953
857
880
265
253
835
167
685
641
854
401
913
790
605
467
643
284
523
423
123
604
55
378
187
260
980
643
39
958
509
242
205
118
372
992
337
373
470
167
521
332
537
869
377
887
283
483
428
602
105
605
380
49
574
705
244
618
295
523
341
307
869
ShellSort - 0 steps
268
942
830
851
226
792
685
31
426
96
916
940
536
817
350
45
129
161
479
370
156
167
714
256
87
870
339
352
454
529
978
181
121
800
275
603
245
739
409
175
401
80
90
947
161
40
380
947
266
42
331
695
981
6
501
980
558
547
599
877
710
304
580
301
882
395
340
11
726
866
351
568
835
815
570
886
244
953
637
799
568
487
355
122
990
1000
807
967
643
717
128
929
186
546
439
389
209
896
401
44
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