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.

630
548
253
389
100
529
511
515
193
862
702
790
219
645
431
66
563
364
71
395
178
92
357
593
872
831
648
151
197
102
821
717
49
887
383
627
858
814
297
509
647
328
987
485
656
822
853
509
372
570
251
617
553
649
211
743
914
855
509
173
977
393
628
638
696
157
846
507
594
672
801
407
609
396
470
433
339
468
776
484
324
135
222
243
439
972
746
386
585
79
758
561
664
788
271
684
143
449
928
275
BubbleSort - 0 steps
59
515
947
562
162
948
175
434
570
179
365
770
710
892
274
598
663
27
743
653
391
406
363
461
251
64
126
623
539
436
432
361
158
641
356
611
803
835
436
929
983
67
728
29
704
809
151
339
582
354
320
14
418
706
507
107
153
15
909
36
833
460
489
884
124
420
121
433
204
107
515
762
603
979
683
315
537
113
328
95
381
506
759
77
401
209
415
610
692
950
620
469
387
526
554
116
313
116
534
769
InsertionSort - 0 steps
656
327
935
337
975
604
528
211
733
503
29
896
288
722
450
410
464
264
735
831
474
144
558
422
414
450
54
664
839
208
150
525
40
10
972
297
489
607
711
287
295
813
145
619
527
835
586
415
899
437
873
134
973
920
666
103
354
787
664
280
804
717
510
78
621
903
239
814
974
67
34
898
7
763
491
49
237
714
782
810
431
427
761
952
467
669
757
49
495
502
790
342
373
126
317
338
913
12
699
833
ShellSort - 0 steps
922
979
569
282
230
36
495
815
45
25
995
40
58
356
35
536
507
600
20
316
986
96
866
734
779
208
661
648
230
20
645
684
686
20
732
574
462
207
381
15
242
241
111
915
34
604
943
735
286
290
861
665
229
281
617
427
667
314
9
546
17
98
861
704
633
337
451
608
25
777
390
300
920
840
238
695
922
2
346
40
513
658
176
908
137
109
984
473
213
330
182
156
506
301
62
607
406
559
574
191
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