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.

632
878
116
795
74
62
442
62
364
638
630
973
222
737
179
316
907
984
234
844
886
684
786
805
583
821
89
178
368
267
822
659
826
196
172
414
839
106
122
598
430
881
848
350
30
442
656
183
405
338
641
378
312
214
765
145
116
80
804
706
79
196
319
132
789
943
729
553
836
970
718
901
633
505
118
511
263
518
932
679
202
569
870
467
1
455
303
340
709
185
456
904
541
314
444
541
136
154
784
311
BubbleSort - 0 steps
58
438
513
484
428
356
378
937
970
314
173
946
846
987
67
219
469
179
966
230
36
503
829
269
397
219
731
384
740
155
65
789
536
212
405
746
857
387
601
711
751
88
380
319
179
550
503
611
233
376
380
683
951
539
735
132
108
75
822
999
488
830
491
211
767
216
599
986
756
858
262
452
792
704
592
972
393
902
369
55
730
818
347
117
711
105
227
559
480
661
207
175
384
762
154
311
566
152
341
693
InsertionSort - 0 steps
439
490
21
914
965
198
315
817
825
515
870
733
782
237
515
256
168
677
152
122
255
689
922
384
123
334
712
339
865
361
580
35
277
437
532
423
311
927
246
565
148
396
931
939
853
933
281
387
961
443
82
540
439
919
655
865
666
985
53
605
978
758
625
876
802
891
249
385
452
613
48
625
658
960
438
847
219
875
339
333
626
702
111
339
709
717
146
226
985
247
53
872
328
613
427
349
550
780
180
728
ShellSort - 0 steps
832
228
940
701
802
94
214
566
439
83
519
476
80
255
124
118
639
991
765
11
226
683
5
294
689
485
440
996
642
812
423
532
884
233
342
755
929
870
395
617
359
559
903
576
984
349
980
102
770
396
865
757
82
991
776
834
227
329
586
399
903
928
307
429
265
117
322
832
900
652
640
63
576
476
194
201
328
555
892
846
181
994
746
670
252
161
149
154
30
741
73
564
679
911
371
370
46
388
745
162
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