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.

620
746
454
20
298
45
999
631
351
755
594
603
893
736
440
266
92
326
463
905
486
446
960
788
141
85
180
289
642
248
715
688
111
157
453
762
683
769
540
978
899
745
521
220
893
31
602
760
860
406
396
662
426
918
269
485
946
331
282
676
382
93
870
728
981
285
271
60
128
729
423
197
491
513
266
598
600
976
485
316
198
281
812
200
465
406
566
470
196
318
665
925
68
174
97
145
4
155
646
825
BubbleSort - 0 steps
828
216
50
459
844
455
829
222
870
628
357
828
969
424
889
286
854
101
357
748
963
576
345
643
753
215
32
146
683
42
770
60
51
182
136
133
617
154
364
210
55
81
274
971
577
104
451
771
307
147
384
378
421
97
396
608
120
233
291
169
989
301
509
626
739
816
361
625
568
77
120
97
218
528
716
775
637
348
625
794
795
148
419
56
897
464
119
501
45
737
290
112
78
395
689
217
923
129
781
920
InsertionSort - 0 steps
398
534
909
981
683
718
15
756
909
170
408
219
311
339
483
558
913
778
375
425
608
211
898
513
604
456
963
41
432
940
45
925
133
42
395
561
416
477
507
116
689
683
468
881
174
987
995
773
398
979
244
464
770
1
678
282
459
775
677
965
983
969
242
49
432
150
576
79
780
412
972
127
394
464
287
960
699
800
230
995
273
195
404
61
355
559
712
420
929
245
374
310
182
251
175
836
113
106
136
646
ShellSort - 0 steps
471
30
32
66
590
898
912
304
502
433
311
195
827
745
883
498
902
718
245
441
555
948
673
438
568
326
196
784
756
288
945
367
569
529
710
958
99
58
843
879
463
773
480
905
145
885
155
149
473
437
181
822
703
993
188
197
734
488
819
601
55
30
549
921
876
486
67
81
695
73
109
368
958
314
954
761
307
757
266
732
509
837
972
962
630
33
558
679
286
695
852
352
786
977
358
475
108
77
974
948
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