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.

480
571
332
317
791
216
820
8
969
17
169
17
969
625
284
787
828
606
647
645
88
374
260
701
85
425
734
369
809
747
969
153
497
235
52
169
25
872
212
710
645
503
672
894
316
776
809
93
337
239
950
295
308
891
987
871
634
531
230
811
175
174
548
601
706
509
977
892
945
761
609
918
528
121
710
956
802
407
161
124
113
946
54
713
558
887
108
511
312
854
89
850
655
975
409
551
973
418
6
962
BubbleSort - 0 steps
490
312
924
26
206
480
244
132
193
600
81
159
292
67
889
915
813
627
632
743
347
14
955
12
375
147
232
686
848
701
422
154
888
238
67
437
34
562
577
820
167
404
167
705
888
63
190
762
221
373
646
504
350
49
864
701
415
605
209
467
863
612
5
934
520
273
273
864
282
483
204
358
377
887
704
946
383
161
317
717
369
486
807
582
603
279
292
246
474
984
121
125
347
146
491
507
426
525
5
453
InsertionSort - 0 steps
308
550
552
635
675
910
863
797
262
889
738
836
664
24
502
685
266
292
871
48
678
440
941
449
63
284
428
226
70
693
667
199
844
47
230
101
80
723
365
284
168
87
884
883
136
97
11
128
210
966
659
913
398
701
721
289
38
631
35
654
71
743
21
614
592
170
593
703
648
54
671
569
91
174
959
712
609
940
138
573
536
761
173
814
651
900
575
475
352
320
219
589
500
93
600
865
539
441
932
708
ShellSort - 0 steps
41
753
947
962
874
778
60
971
6
773
931
476
305
467
309
662
718
771
927
319
64
575
141
889
907
372
851
177
24
889
425
952
30
504
217
462
633
414
440
209
885
44
188
233
270
862
305
229
459
138
10
815
807
752
599
680
744
985
42
915
150
263
615
174
83
200
9
778
737
760
318
938
888
225
476
201
603
902
975
426
41
378
934
671
451
801
969
395
851
598
52
901
777
125
69
579
649
737
940
150
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