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.

262
22
239
957
784
683
113
623
615
645
367
55
937
9
618
568
946
492
702
399
14
92
95
891
940
480
454
773
549
573
919
843
650
590
307
241
209
542
700
62
22
739
995
626
450
506
302
163
726
597
699
122
123
738
549
104
101
327
359
442
80
885
215
457
193
138
454
800
90
749
592
594
665
811
728
764
248
76
628
971
403
866
829
64
511
754
289
906
425
229
760
644
744
678
280
418
679
766
577
527
BubbleSort - 0 steps
242
724
676
780
777
820
67
609
360
156
321
196
161
64
667
74
824
356
558
436
616
17
735
172
894
961
613
779
445
899
919
674
185
14
681
114
593
578
601
432
189
186
448
213
99
969
907
139
772
950
928
978
779
118
470
625
288
549
139
860
819
26
40
199
632
930
75
661
541
170
62
313
136
819
220
67
892
308
394
643
675
939
668
98
990
486
344
551
293
580
742
243
727
828
526
28
129
347
149
536
InsertionSort - 0 steps
190
187
158
893
294
221
436
436
977
608
929
860
522
300
645
394
176
962
665
759
450
523
848
921
637
329
602
303
640
158
651
471
264
461
602
556
702
534
393
339
945
141
900
135
333
928
541
25
633
928
871
406
512
389
447
169
30
313
290
162
389
747
973
960
47
332
852
538
57
830
427
130
654
936
715
731
909
664
577
277
105
646
263
861
893
116
314
555
1
214
383
8
218
874
435
195
83
376
381
369
ShellSort - 0 steps
383
233
131
725
36
411
227
428
397
260
137
226
806
806
382
741
126
344
829
346
43
532
599
808
808
483
808
761
744
243
766
952
138
369
761
636
539
409
200
37
493
544
911
767
290
900
695
963
994
727
716
489
220
469
809
500
371
15
417
2
375
91
464
314
810
994
984
412
453
985
796
681
566
824
925
120
792
297
528
181
714
327
55
392
329
507
434
894
406
197
635
168
833
880
291
506
359
463
209
654
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