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.

359
738
974
279
23
167
35
583
50
145
501
369
324
30
200
288
218
479
166
755
699
298
905
716
290
550
371
653
568
149
465
259
56
429
310
346
817
789
912
814
331
163
401
876
36
332
836
67
738
465
516
662
411
654
490
797
35
242
895
696
235
642
441
667
608
233
542
316
247
202
695
29
819
974
581
670
123
794
42
622
214
502
937
73
771
157
259
563
553
52
1000
698
603
180
717
367
218
773
650
814
BubbleSort - 0 steps
937
556
100
43
266
236
269
925
160
570
292
21
486
352
152
730
451
519
978
516
480
843
228
781
575
77
910
19
17
55
948
403
968
102
986
649
305
242
877
513
221
434
892
850
500
425
330
919
230
756
830
657
245
250
999
123
461
871
882
793
658
644
88
870
524
2
893
105
427
704
156
953
914
837
943
201
790
78
623
596
205
445
799
543
335
380
923
463
226
821
431
472
322
798
542
71
708
176
162
514
InsertionSort - 0 steps
173
276
483
795
900
558
525
991
349
463
289
347
473
901
134
226
188
582
180
141
481
725
224
5
665
193
657
809
253
752
788
240
422
759
277
205
205
840
317
971
535
594
38
653
1000
372
413
174
24
184
863
74
883
388
116
22
247
327
484
753
677
688
239
711
451
289
520
581
873
1000
37
285
467
526
237
824
514
975
737
55
613
220
824
934
399
646
542
351
642
823
167
692
387
880
891
529
382
630
852
935
ShellSort - 0 steps
549
275
914
765
235
150
956
311
180
151
896
702
460
975
958
998
517
135
719
628
345
921
30
950
464
327
831
110
371
87
535
787
492
176
954
316
370
10
262
86
207
23
375
20
447
477
402
415
554
988
597
246
428
437
488
687
351
800
968
412
415
898
320
660
278
712
708
205
36
571
898
897
453
892
988
293
438
938
859
611
220
571
714
986
524
667
837
781
693
796
960
143
758
46
947
958
591
95
132
864
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