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.

315
787
29
135
633
937
89
365
682
806
234
763
69
917
348
664
799
904
449
189
938
241
79
264
322
310
944
846
347
10
109
537
684
105
998
598
245
309
510
86
313
50
499
648
233
693
550
57
518
350
990
787
342
115
402
926
956
919
353
812
563
153
715
441
338
295
629
886
919
460
337
409
44
126
175
789
205
745
908
416
382
713
923
555
34
776
931
423
552
917
511
886
712
190
897
818
877
256
214
134
BubbleSort - 0 steps
90
830
351
571
778
727
30
723
288
941
866
631
65
818
32
58
897
816
506
664
787
271
400
841
677
307
546
380
257
725
652
656
664
404
281
871
540
949
774
10
518
861
915
542
627
878
285
458
463
749
960
194
757
322
157
910
47
708
370
936
858
906
209
616
736
26
214
904
103
528
923
450
85
89
374
722
804
100
958
94
741
228
861
289
617
92
727
527
825
197
576
609
12
923
228
24
562
36
122
145
InsertionSort - 0 steps
737
349
361
798
535
440
247
11
981
771
721
384
465
94
143
651
230
608
619
194
839
810
349
409
620
401
810
367
629
475
24
427
471
955
708
65
15
387
376
251
327
410
93
65
93
762
296
255
882
570
681
879
989
75
989
422
602
594
550
508
504
450
665
451
721
249
474
993
507
935
288
832
924
704
946
977
201
720
760
412
808
408
442
862
246
729
276
799
420
718
729
844
68
735
764
178
825
895
647
485
ShellSort - 0 steps
374
9
746
289
726
933
226
831
301
988
865
771
892
844
55
917
271
815
670
534
371
743
269
889
178
548
865
60
630
612
12
819
252
688
181
385
217
637
193
101
287
404
262
346
161
562
990
958
868
171
574
89
369
275
777
610
341
855
840
589
926
143
524
377
763
889
424
726
671
586
577
933
564
80
895
402
410
9
648
977
604
616
810
443
302
642
860
314
769
181
351
345
826
988
123
700
624
843
242
747
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