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.

49
240
491
364
442
539
396
772
805
542
337
810
148
129
19
653
743
917
510
96
930
686
60
132
508
414
894
861
269
539
856
329
404
676
588
340
313
390
272
801
885
482
601
574
268
677
573
642
130
976
903
287
441
916
30
119
384
624
706
55
451
990
526
717
477
207
993
438
604
483
239
834
104
286
3
905
83
967
221
255
913
306
110
74
399
606
798
412
557
265
669
106
559
816
766
750
181
645
822
528
BubbleSort - 0 steps
999
929
608
403
383
129
607
197
264
576
111
456
989
963
544
644
289
793
6
924
291
46
380
463
724
804
954
490
181
174
403
692
439
670
211
107
184
571
278
211
194
963
815
973
968
364
623
95
828
364
397
660
726
211
353
791
412
639
13
718
84
282
338
998
841
344
79
428
834
210
650
394
122
467
725
465
401
245
754
560
697
899
555
92
461
729
446
299
256
190
630
728
984
997
715
190
856
814
722
750
InsertionSort - 0 steps
322
600
482
125
927
886
602
88
579
235
591
534
373
922
563
202
787
184
625
279
805
620
690
95
239
744
154
554
183
318
668
456
771
868
823
663
258
121
586
18
660
999
639
99
581
728
507
12
877
618
299
507
561
771
874
297
566
371
572
145
560
292
109
615
124
770
629
455
359
665
992
538
644
653
142
185
993
719
537
834
517
441
657
934
919
55
504
74
235
736
475
155
182
358
635
747
792
935
163
733
ShellSort - 0 steps
579
717
488
967
76
182
901
493
293
955
247
241
414
929
363
664
211
969
898
478
580
911
645
26
803
791
43
114
617
574
256
549
30
773
730
609
931
288
880
738
880
909
956
659
930
129
462
911
369
803
409
947
562
608
295
917
193
435
988
547
447
613
755
309
488
347
46
142
435
695
280
493
299
630
337
448
131
800
571
799
212
238
752
5
156
373
658
823
380
808
209
244
407
690
688
397
587
517
978
961
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