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.

201
402
658
193
239
794
777
782
21
627
917
831
457
638
917
25
895
690
160
194
678
887
938
387
638
873
373
248
706
719
118
75
525
85
14
543
448
225
393
658
144
354
919
780
293
159
243
128
719
973
779
589
784
695
788
245
761
211
851
660
910
8
138
640
50
246
263
664
527
488
825
253
629
437
924
38
242
734
846
17
345
514
347
449
595
765
464
542
648
45
534
467
78
803
650
39
898
317
35
762
BubbleSort - 0 steps
863
950
787
433
807
862
155
683
753
136
56
836
142
155
166
890
338
30
441
140
232
487
747
587
152
193
571
183
426
735
284
948
340
704
599
922
821
852
111
324
931
184
623
797
502
402
626
841
112
804
913
992
489
281
741
327
771
56
250
628
28
19
527
760
979
841
699
567
23
118
898
353
879
716
328
847
722
735
516
665
378
508
396
956
462
718
443
489
201
101
255
711
822
366
828
982
389
676
552
382
InsertionSort - 0 steps
892
284
85
545
385
832
516
258
238
557
202
974
288
629
121
365
181
124
159
60
172
149
908
142
147
427
90
130
449
131
682
557
577
289
656
96
448
716
658
72
779
220
164
143
982
786
582
863
900
665
786
322
867
336
92
182
344
680
16
53
930
936
714
632
575
733
718
919
654
23
379
194
484
821
123
137
874
528
359
467
832
595
30
412
249
883
48
909
795
778
367
514
974
350
373
622
722
302
279
553
ShellSort - 0 steps
394
327
54
529
61
2
305
995
199
327
168
805
88
667
490
853
526
265
275
201
874
240
167
3
432
143
926
520
90
349
365
250
251
598
843
878
107
963
201
423
628
282
815
29
385
759
316
205
758
225
942
42
43
608
477
628
926
841
272
167
289
451
876
152
246
596
927
49
144
590
783
85
878
982
900
912
360
87
2
997
375
833
218
198
790
977
297
929
609
806
172
599
260
474
456
463
825
627
158
657
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