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.

460
232
730
150
988
513
71
176
769
360
642
168
973
334
67
14
886
561
144
552
476
233
781
442
300
231
757
138
914
323
298
882
477
232
255
282
594
141
415
416
872
830
622
98
595
921
172
966
560
794
581
546
178
718
485
853
866
257
889
350
515
88
727
445
477
363
727
32
562
365
138
886
49
295
56
353
153
20
317
125
41
141
490
501
716
911
413
444
346
810
742
854
775
133
111
719
897
54
928
815
BubbleSort - 0 steps
19
491
387
524
902
140
718
365
251
372
109
604
420
608
180
876
680
77
134
819
382
206
877
293
377
296
729
766
425
526
334
738
980
584
904
444
756
282
591
175
322
98
731
405
863
745
718
804
246
434
345
940
449
591
315
716
934
389
72
538
606
307
523
917
999
200
572
58
177
811
228
578
787
287
688
227
508
367
869
578
180
933
679
48
166
715
909
910
458
620
552
370
44
166
117
106
704
346
597
451
InsertionSort - 0 steps
239
231
572
833
657
290
400
765
78
327
166
660
769
491
393
235
221
534
29
747
762
310
418
838
306
411
247
575
235
479
907
205
968
174
451
95
288
225
777
132
991
684
35
20
152
553
489
362
83
192
497
612
123
405
361
629
394
744
409
402
87
830
624
102
454
164
869
637
48
71
139
233
953
82
296
703
346
134
621
331
370
567
375
149
762
325
786
272
888
998
459
457
562
796
518
969
932
762
541
535
ShellSort - 0 steps
128
117
882
651
960
646
921
232
601
701
763
676
903
160
631
986
624
807
621
714
695
863
728
421
779
875
595
78
536
392
950
665
15
69
826
978
420
902
647
850
934
764
891
866
634
42
305
841
979
862
250
873
862
605
359
587
390
572
348
189
994
319
320
384
445
468
230
625
768
398
363
189
972
131
798
894
73
102
201
892
374
55
157
563
635
830
162
439
322
331
222
497
391
288
342
631
977
1
866
173
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