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.

253
145
16
757
234
519
656
388
421
740
790
340
452
490
166
362
152
726
149
338
544
396
712
791
70
545
954
294
649
475
304
823
674
822
364
921
968
118
894
476
341
591
170
868
237
316
618
1000
858
2
988
516
178
328
724
895
933
304
354
890
692
238
359
868
543
178
778
390
916
529
63
50
969
468
466
127
300
312
92
411
612
81
610
185
192
557
882
612
968
564
531
857
282
799
882
292
887
134
527
594
BubbleSort - 0 steps
189
988
193
293
21
629
774
295
926
829
755
522
109
745
488
752
162
849
287
381
394
22
133
90
965
594
699
780
36
987
34
945
631
644
278
460
711
623
37
765
560
762
45
167
315
890
444
996
147
741
854
831
323
754
557
75
471
98
888
966
318
77
327
380
328
255
333
433
884
196
1000
500
784
547
988
795
484
287
726
654
904
479
345
440
207
809
548
722
989
887
866
218
793
796
332
840
69
720
893
421
InsertionSort - 0 steps
165
86
950
279
986
940
966
322
819
229
449
496
214
980
602
272
949
813
197
982
356
496
38
677
670
935
884
629
831
574
96
209
370
51
591
985
468
59
934
689
899
767
769
451
284
669
701
469
393
442
161
32
103
307
15
8
708
124
927
718
468
431
197
172
168
765
696
617
409
163
304
94
977
792
720
101
430
331
672
982
385
106
261
170
225
966
373
665
49
65
952
441
178
519
399
78
659
61
360
100
ShellSort - 0 steps
569
444
2
834
272
318
713
441
289
774
493
307
349
446
382
643
150
369
908
775
539
496
165
184
252
471
83
300
568
445
972
924
396
315
412
951
362
117
537
612
813
130
917
829
239
649
685
565
676
244
280
804
569
352
185
727
191
933
282
956
145
339
676
429
38
235
332
846
917
522
484
930
352
90
828
962
756
266
905
195
662
795
836
490
883
107
192
793
515
782
572
983
608
132
345
216
77
502
628
475
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