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.

611
292
455
122
63
891
28
637
89
702
239
732
48
15
998
848
51
652
56
926
742
662
896
609
284
305
144
454
799
140
120
269
584
967
953
180
838
299
33
502
43
981
771
393
992
330
132
789
513
618
708
819
408
557
88
538
667
70
90
952
422
736
967
270
429
653
485
761
895
500
779
656
674
714
686
289
833
214
380
799
823
34
470
294
115
848
238
540
60
366
106
39
250
247
103
771
87
249
176
747
BubbleSort - 0 steps
985
198
466
390
414
183
387
91
433
993
353
85
161
947
626
526
518
743
361
680
195
221
148
83
23
765
926
14
318
693
525
501
853
180
929
529
70
871
962
935
378
601
634
28
807
938
32
309
609
678
785
282
708
654
681
22
999
470
958
259
902
68
108
423
559
503
635
850
241
526
878
843
463
592
933
734
725
884
313
469
908
182
780
100
464
503
943
112
866
252
178
145
843
563
265
631
190
770
41
562
InsertionSort - 0 steps
283
762
379
526
47
212
995
934
239
77
69
96
438
757
720
211
21
31
967
723
702
973
681
559
315
718
735
111
740
362
691
924
121
839
932
841
169
308
492
620
445
27
596
161
77
443
898
824
279
502
766
831
839
861
603
573
947
675
132
477
843
817
382
639
802
399
993
961
503
824
248
742
515
446
138
10
176
236
215
182
371
512
541
114
402
984
349
863
995
126
566
411
516
527
442
121
829
458
708
907
ShellSort - 0 steps
226
214
540
990
593
376
297
643
697
40
511
653
997
279
485
330
84
917
138
247
287
999
450
655
232
785
362
364
521
243
736
957
117
82
914
48
720
473
890
124
461
467
492
770
724
93
895
662
644
80
323
73
731
993
372
743
809
75
886
47
768
336
842
308
374
221
605
304
12
439
627
259
300
566
920
275
381
173
749
741
186
842
825
542
26
51
542
628
248
530
384
128
103
533
65
440
234
411
709
80
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