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.

410
24
526
691
874
826
938
633
541
712
926
274
574
646
69
370
140
749
113
890
265
446
295
324
663
575
991
564
39
281
280
317
24
808
865
336
847
957
842
196
627
565
188
678
463
130
907
410
922
745
394
957
544
963
652
846
884
757
675
65
278
523
480
576
324
978
603
976
556
122
671
59
538
912
203
724
172
310
129
889
359
43
765
656
295
96
602
259
76
729
615
127
506
94
42
851
586
366
290
950
BubbleSort - 0 steps
615
840
610
433
150
59
695
240
535
582
102
746
322
929
827
97
397
698
894
219
853
406
26
495
883
315
550
579
262
745
300
898
550
28
359
718
927
140
297
560
180
64
188
502
33
409
378
50
156
659
228
476
478
647
552
415
137
500
399
920
929
335
598
286
227
739
368
851
564
842
707
471
781
856
69
837
213
394
755
809
636
717
582
103
63
832
83
492
350
776
342
949
270
930
637
135
296
183
324
105
InsertionSort - 0 steps
782
520
886
464
227
316
823
877
565
676
828
30
166
789
880
813
954
276
357
925
750
563
65
267
591
461
473
432
354
585
826
166
359
836
594
358
709
587
535
880
592
655
588
852
19
554
356
929
871
968
422
370
98
859
952
397
648
484
958
741
929
616
598
384
591
375
758
625
848
486
101
508
496
350
711
881
683
361
397
640
37
963
112
345
573
222
820
997
190
804
590
131
971
314
69
223
10
617
559
962
ShellSort - 0 steps
786
570
627
816
111
36
118
283
15
955
31
274
337
809
873
234
739
127
343
122
368
263
323
380
738
14
224
593
835
781
808
788
346
64
565
542
366
608
567
57
271
275
458
914
245
766
539
40
727
636
287
498
181
347
238
130
466
850
8
859
862
85
742
445
295
853
911
474
575
656
596
141
664
10
246
228
226
259
568
383
80
81
641
139
175
115
820
806
915
695
110
135
529
880
91
71
700
890
628
324
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