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.

407
44
453
701
452
897
2
505
71
807
217
678
934
680
748
52
218
184
370
617
874
455
209
100
408
114
961
777
611
201
399
868
326
618
325
719
234
432
480
961
675
96
33
314
147
142
178
984
256
32
170
326
986
496
943
189
924
659
854
612
918
464
158
47
579
691
705
869
301
188
11
6
102
699
945
63
574
770
429
314
665
500
77
607
160
36
939
727
217
418
539
16
352
613
686
701
448
273
466
418
BubbleSort - 0 steps
714
630
895
498
146
305
769
296
656
160
910
265
471
50
966
940
700
523
254
633
300
93
686
876
430
756
385
478
661
162
534
92
961
773
909
566
615
646
306
604
179
414
794
341
510
986
558
155
165
309
62
925
533
698
273
73
489
928
62
649
330
711
899
519
975
612
284
627
948
731
194
956
715
584
860
138
563
324
805
668
503
463
927
22
166
224
873
93
59
186
991
398
745
599
675
598
418
501
76
405
InsertionSort - 0 steps
545
374
603
355
289
615
243
64
254
48
250
26
432
59
707
113
667
50
835
893
599
119
224
580
570
539
848
836
69
542
25
338
914
601
377
874
548
679
92
993
916
755
338
957
758
511
472
545
740
25
966
639
113
121
924
310
540
137
936
614
716
256
678
515
94
157
793
512
614
825
109
226
73
896
879
141
578
791
855
34
910
778
509
342
516
530
934
542
708
934
334
567
73
393
630
621
164
129
860
461
ShellSort - 0 steps
497
677
169
635
859
987
878
6
214
559
843
196
378
695
336
207
880
488
952
38
710
263
336
190
592
795
173
850
381
197
750
456
531
401
588
890
931
416
802
329
979
606
906
861
565
923
497
287
246
222
732
566
533
479
236
174
33
571
164
138
624
768
409
323
504
254
91
683
152
772
369
375
212
657
459
277
583
665
649
857
592
562
200
848
217
366
711
957
626
829
111
894
554
474
218
151
493
917
742
293
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