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.

295
334
250
359
98
183
643
43
393
827
206
492
629
772
565
153
359
872
634
572
198
84
889
848
580
793
951
912
906
109
379
774
399
757
324
292
714
28
706
24
145
432
390
360
835
720
27
665
175
790
822
999
548
159
640
280
126
424
861
673
51
54
150
105
733
184
785
171
543
43
820
759
639
43
400
851
162
792
970
362
889
819
67
367
375
815
969
278
386
258
883
578
43
565
538
726
53
826
303
879
BubbleSort - 0 steps
162
532
315
432
638
138
226
749
45
476
513
629
329
393
630
387
920
135
776
959
776
605
181
515
845
677
445
106
314
480
196
616
656
243
798
207
877
834
307
977
761
222
873
917
627
551
804
1
924
471
918
914
337
999
608
680
661
901
531
711
381
937
736
997
860
630
261
882
159
818
447
637
667
610
978
127
538
714
461
53
140
749
289
821
594
904
181
559
157
554
93
585
277
558
910
799
88
515
108
285
InsertionSort - 0 steps
617
153
431
512
495
427
94
246
342
965
658
968
840
100
723
321
645
211
559
543
341
452
922
841
334
701
630
138
294
410
850
55
700
869
797
536
585
626
12
762
784
837
250
868
453
672
393
578
215
270
358
349
436
546
610
910
728
640
238
467
73
854
803
656
18
207
797
750
685
816
596
806
39
578
491
599
808
797
837
589
11
746
969
270
4
898
255
995
262
764
85
806
424
482
735
943
83
115
724
43
ShellSort - 0 steps
539
82
306
179
896
553
180
625
645
305
557
899
217
468
885
341
495
798
974
866
41
441
666
938
21
386
286
488
423
705
610
931
757
324
316
939
180
689
28
418
328
48
37
242
669
695
135
719
90
890
759
654
802
527
473
285
432
313
97
335
33
777
74
177
697
915
393
767
788
377
2
355
226
826
719
86
940
477
405
825
684
188
824
253
305
709
634
213
389
669
209
390
645
683
145
84
19
101
58
14
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