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.

344
306
416
907
845
546
140
467
135
747
465
990
439
359
383
298
796
429
17
748
813
568
708
109
497
724
149
381
228
654
372
285
579
916
753
47
14
670
848
321
354
569
863
332
745
367
51
987
138
251
709
610
814
785
788
371
675
98
418
7
280
186
990
104
606
737
511
868
348
622
741
422
691
375
965
854
455
688
589
110
853
26
553
386
436
621
732
299
814
428
511
508
252
326
868
69
536
320
507
729
BubbleSort - 0 steps
863
743
185
505
621
102
281
430
70
764
727
81
187
293
56
779
504
666
322
390
392
500
130
925
845
450
613
718
903
435
170
454
936
617
252
27
753
100
846
261
35
14
531
492
733
349
313
434
265
614
170
79
364
971
743
104
749
703
724
842
987
561
815
378
543
783
218
284
123
457
564
886
796
855
804
429
65
137
769
462
573
458
517
476
793
206
717
840
561
843
189
788
703
732
204
534
698
881
160
399
InsertionSort - 0 steps
179
459
632
416
456
397
532
944
531
270
711
585
353
929
84
959
119
858
580
56
563
220
250
903
249
519
787
389
787
826
435
205
472
188
605
340
961
956
265
443
657
109
705
342
644
89
674
156
409
195
523
342
7
117
356
452
544
448
226
564
521
825
40
324
847
588
813
587
344
800
668
928
90
255
143
871
566
927
650
518
562
22
685
743
275
648
279
224
990
105
851
833
715
953
859
187
157
154
679
767
ShellSort - 0 steps
646
273
832
762
927
646
558
587
268
37
898
245
752
579
691
464
924
675
484
640
359
868
949
695
67
64
859
56
812
776
90
311
86
763
687
678
329
700
912
508
340
824
106
950
581
650
646
629
322
40
412
355
749
554
636
939
239
661
434
796
724
388
150
273
435
848
626
290
729
304
841
471
744
597
692
208
514
151
673
963
368
597
435
530
408
33
638
77
319
344
635
280
440
775
899
325
509
207
46
897
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