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.

584
172
185
947
915
233
74
64
514
932
491
983
38
415
668
55
411
439
836
191
239
708
115
883
5
248
579
201
436
395
921
245
845
126
562
614
476
43
758
241
694
394
754
494
117
201
693
918
94
851
667
81
728
101
625
845
130
461
95
887
106
51
575
236
361
220
402
643
186
372
492
609
387
511
686
31
627
532
370
735
854
297
41
574
857
113
957
102
458
490
112
153
5
388
133
514
705
347
323
359
BubbleSort - 0 steps
734
170
203
646
183
762
77
378
258
270
349
339
513
573
478
308
494
875
396
262
920
152
530
780
150
56
431
753
242
87
143
666
953
504
918
650
446
85
35
988
335
788
238
937
732
450
237
212
189
382
272
364
516
240
836
432
594
40
48
98
902
352
535
121
257
177
12
648
566
384
738
126
323
350
992
231
84
434
991
746
701
272
116
637
161
964
595
827
898
972
416
323
567
274
760
83
403
772
679
101
InsertionSort - 0 steps
550
844
912
437
674
266
25
342
42
903
474
489
933
593
328
942
257
447
133
451
378
23
768
527
191
980
751
468
176
844
826
236
434
85
590
843
861
439
365
333
997
200
715
880
493
97
130
725
215
74
684
521
942
622
565
277
209
682
319
701
792
370
838
883
192
309
403
455
491
868
969
77
513
872
921
142
419
950
449
476
473
2
294
749
73
857
862
175
997
307
77
693
658
191
669
613
268
197
880
671
ShellSort - 0 steps
54
295
887
250
758
452
776
978
373
107
510
236
95
229
674
931
936
74
458
654
617
474
609
293
591
116
387
472
648
400
548
98
221
587
555
698
400
760
312
46
228
499
529
589
349
189
931
997
35
285
29
676
250
953
981
791
139
48
320
771
455
223
799
670
118
15
105
83
835
762
352
581
778
921
253
985
926
351
944
404
909
901
937
477
174
673
959
450
335
681
658
448
302
248
932
316
908
697
750
89
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