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.

797
876
308
177
238
721
976
504
189
864
982
989
782
582
662
335
743
973
408
7
585
953
664
819
180
8
678
978
350
105
847
130
265
184
235
316
869
203
461
359
667
687
378
3
461
783
924
724
932
457
794
643
100
460
387
856
589
605
723
120
107
452
936
583
727
496
29
557
658
980
305
718
389
453
805
128
219
33
568
125
730
136
352
162
851
462
672
20
193
945
827
657
355
691
746
19
285
811
618
747
BubbleSort - 0 steps
444
748
211
853
536
197
493
962
31
39
236
36
308
943
985
309
269
465
17
631
989
996
187
429
84
101
213
19
245
312
107
179
541
151
312
810
608
781
396
844
465
923
543
634
630
783
719
637
372
123
991
287
173
390
634
152
642
919
112
401
858
225
12
850
732
296
629
604
450
481
20
459
844
842
97
220
121
633
629
234
444
555
345
952
815
467
878
962
130
586
595
627
194
504
675
684
42
617
333
478
InsertionSort - 0 steps
394
867
501
644
549
641
538
723
992
548
892
416
944
129
420
250
911
753
152
269
769
178
942
259
260
659
483
216
477
717
514
316
82
554
158
339
34
908
389
727
199
234
628
434
44
103
115
258
107
478
142
775
303
525
273
36
654
863
426
342
149
554
302
103
515
484
744
813
256
263
581
188
743
400
399
961
297
50
382
973
471
582
570
810
532
905
319
466
989
289
847
209
936
747
724
302
572
363
713
362
ShellSort - 0 steps
149
666
245
116
457
411
972
152
853
665
922
162
902
259
710
567
232
554
567
264
236
961
220
610
179
842
449
998
800
561
289
782
579
930
434
794
287
809
922
993
382
872
312
217
531
580
43
728
257
951
350
196
824
289
629
486
588
446
263
937
637
245
486
430
912
445
277
435
509
680
26
928
424
501
613
395
756
24
548
360
509
294
105
987
7
121
845
159
959
991
471
563
536
716
299
873
598
681
204
621
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