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.

999
422
367
780
998
87
460
211
367
686
919
319
711
1
414
311
179
348
383
164
615
611
831
954
996
177
871
557
980
362
809
478
435
397
811
199
290
263
386
717
167
196
551
146
802
249
703
900
76
434
402
316
177
452
696
94
756
954
221
24
448
339
812
560
449
532
159
321
605
346
695
22
522
643
408
353
90
468
806
111
140
283
584
633
432
710
279
382
739
885
260
822
948
598
322
941
406
38
689
751
BubbleSort - 0 steps
81
608
485
194
153
189
306
506
854
857
305
86
663
784
510
652
264
298
689
821
450
83
667
560
438
520
522
32
847
969
435
420
844
475
956
374
214
442
264
586
996
597
302
512
585
373
964
343
827
628
7
213
984
901
595
174
707
427
858
956
306
462
501
741
207
104
710
208
398
684
322
245
258
900
816
991
726
687
880
6
466
497
217
669
922
911
993
144
901
469
448
172
785
430
272
329
232
365
772
173
InsertionSort - 0 steps
636
493
471
501
942
734
417
688
282
986
146
263
696
113
540
865
437
906
132
701
688
125
606
787
469
997
146
138
704
504
468
908
719
111
270
176
733
180
967
725
609
516
464
44
130
471
206
825
590
423
527
411
718
962
965
124
155
534
553
471
229
890
259
698
901
337
245
468
914
534
488
257
258
644
278
839
859
813
978
761
261
526
476
195
130
777
619
966
890
56
327
146
674
400
397
262
508
347
530
279
ShellSort - 0 steps
280
623
97
749
617
918
679
471
388
963
342
739
54
47
456
409
624
687
524
833
560
368
266
803
669
655
10
10
98
905
643
254
374
298
529
423
636
85
471
541
635
406
8
432
405
999
518
389
14
889
346
144
70
72
437
994
228
272
690
359
305
424
248
338
27
415
584
610
57
109
665
922
117
733
961
418
250
211
828
780
365
24
300
10
250
459
971
73
103
593
54
72
642
991
205
246
192
472
753
552
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