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.

457
436
40
371
840
703
133
753
738
291
127
366
785
511
66
57
681
444
770
407
744
169
331
986
797
445
516
18
130
656
511
813
393
903
470
885
229
704
798
507
958
387
897
350
338
106
261
546
709
440
869
848
184
201
133
675
905
66
411
593
883
515
910
618
633
834
821
142
982
967
287
716
62
523
908
38
410
859
493
288
784
203
201
17
809
265
202
946
361
517
657
866
99
590
853
5
981
425
601
668
BubbleSort - 0 steps
697
599
58
953
847
615
276
22
651
211
218
196
737
224
320
554
306
881
180
207
87
915
153
368
955
176
204
202
990
821
981
638
714
627
735
766
334
122
820
712
937
893
74
978
573
611
572
364
168
384
103
42
344
206
289
402
138
831
629
334
542
544
916
860
477
892
688
143
763
272
154
972
876
183
467
917
415
877
6
224
641
695
857
390
794
648
596
796
639
288
823
591
922
850
56
676
803
144
385
214
InsertionSort - 0 steps
369
972
967
508
271
449
399
554
298
673
873
673
708
271
396
581
567
195
70
500
858
377
167
849
235
868
514
254
836
690
243
424
800
449
961
919
265
43
333
972
115
267
523
599
402
222
476
307
342
42
913
629
874
644
754
125
946
254
408
84
123
994
116
392
757
496
398
952
838
645
861
281
714
274
655
471
567
837
837
408
210
530
293
543
265
854
109
805
135
510
598
86
772
721
563
403
838
761
861
860
ShellSort - 0 steps
870
626
552
144
994
349
22
771
825
863
952
356
746
913
750
929
652
769
675
610
284
872
573
342
111
859
826
691
301
124
597
662
28
972
855
329
645
155
792
612
395
396
222
161
823
268
498
299
447
385
392
859
839
795
261
458
528
961
800
502
72
10
517
675
739
70
241
126
557
723
807
431
914
613
140
234
848
317
130
925
408
381
691
194
2
511
334
7
404
904
514
878
3
512
467
959
991
721
382
323
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