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.

468
580
361
936
782
236
159
1000
84
240
65
327
835
362
832
601
573
196
125
903
345
613
656
896
780
830
262
971
787
344
847
249
826
894
139
752
514
245
554
513
732
708
472
505
135
925
422
822
511
337
862
549
153
778
986
308
767
548
255
775
659
263
274
697
415
975
944
764
549
222
96
645
651
100
270
625
334
869
776
785
679
235
254
741
449
242
88
352
760
507
423
286
727
670
429
773
237
658
880
640
BubbleSort - 0 steps
129
484
712
940
974
325
367
71
963
192
21
970
328
832
358
750
508
304
369
604
474
965
13
471
38
879
307
55
993
9
165
919
931
36
628
801
998
472
52
668
276
31
890
956
549
584
24
664
528
228
110
787
424
996
386
87
476
764
929
537
451
50
860
474
899
351
718
50
147
992
328
786
590
972
89
292
296
559
857
238
136
486
325
157
656
396
576
997
24
57
145
155
228
776
826
17
691
752
592
992
InsertionSort - 0 steps
61
30
724
446
664
926
10
565
853
940
9
342
539
379
172
452
274
830
785
781
93
924
103
769
158
508
122
129
864
497
209
485
569
129
346
975
579
528
143
754
382
77
357
747
645
83
692
958
1000
970
346
238
471
238
643
974
153
842
821
206
438
753
654
331
527
157
442
423
658
475
979
514
593
160
63
543
985
454
636
588
220
820
290
546
881
382
756
812
367
462
762
503
898
302
414
483
667
197
638
189
ShellSort - 0 steps
416
983
804
442
620
791
935
87
179
609
887
329
895
742
648
617
175
610
704
832
957
227
572
462
886
590
54
859
219
799
269
935
969
650
867
760
552
387
419
970
387
907
946
831
929
833
259
418
387
15
405
630
742
351
968
724
220
92
912
418
9
314
180
58
587
592
665
53
836
903
561
790
754
394
324
204
244
467
564
166
124
927
896
814
953
304
35
799
786
411
560
640
388
32
379
822
392
177
538
548
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