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.

226
280
429
567
253
318
600
384
895
512
31
940
214
603
753
588
772
90
133
689
586
941
972
813
792
786
937
745
420
112
784
611
68
978
412
554
632
985
981
677
565
342
558
788
173
885
582
981
405
90
989
644
483
157
913
658
929
336
448
37
919
744
675
989
853
391
29
650
756
826
27
949
710
474
201
878
129
239
217
862
770
966
586
549
26
245
1
872
712
180
579
844
75
128
672
598
833
180
710
961
BubbleSort - 0 steps
147
251
481
897
937
628
699
952
738
39
314
75
381
841
606
422
425
903
852
713
264
99
912
340
536
479
678
813
252
971
216
584
441
650
948
108
860
465
957
844
735
353
369
852
278
209
504
637
269
407
561
842
852
446
83
419
737
122
340
636
898
161
18
884
295
90
593
449
128
226
304
558
500
200
870
429
982
187
267
398
246
793
786
251
492
409
855
948
745
619
236
122
882
627
558
134
68
207
219
43
InsertionSort - 0 steps
228
621
885
735
242
304
506
209
825
859
4
850
383
527
849
876
510
987
889
819
192
121
394
81
350
283
700
414
38
595
55
140
595
271
559
698
971
626
252
725
554
194
573
57
796
885
7
219
228
36
266
598
733
383
561
82
72
401
799
861
719
911
913
584
296
821
693
783
781
667
905
920
221
179
773
418
437
911
30
53
90
959
599
891
458
329
622
734
932
77
991
142
105
516
531
958
52
806
641
552
ShellSort - 0 steps
338
481
159
74
538
958
912
96
650
527
297
414
905
171
300
385
642
77
945
242
723
928
690
566
284
952
554
477
467
745
833
221
850
22
297
169
694
205
448
730
403
874
167
193
423
74
357
154
93
419
35
975
698
110
16
655
60
17
405
168
667
501
764
787
298
877
343
146
507
13
451
356
584
243
737
791
21
753
184
287
485
142
415
305
951
595
895
674
209
369
717
779
300
98
236
547
939
210
800
368
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