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.

743
999
237
479
817
921
807
988
268
282
464
148
945
503
878
725
279
860
352
389
417
955
58
907
343
173
556
864
668
750
287
967
597
164
196
548
70
529
890
584
201
3
280
7
843
703
831
84
271
419
994
300
492
421
566
593
17
98
664
611
301
255
630
402
626
542
338
238
270
607
267
872
243
536
545
317
219
682
128
116
168
470
143
726
293
46
388
769
821
781
205
517
172
872
658
235
176
446
299
480
BubbleSort - 0 steps
249
825
833
422
307
484
517
764
1
996
288
502
52
408
263
534
66
668
720
503
322
200
535
904
251
853
790
940
694
709
621
81
458
967
743
569
29
920
338
629
115
547
725
417
533
116
665
294
943
249
753
904
281
215
781
853
978
65
681
265
23
430
167
932
889
261
103
760
699
244
232
319
847
850
3
687
498
643
14
359
86
1000
708
61
395
561
228
337
631
225
523
863
561
960
202
408
860
614
887
45
InsertionSort - 0 steps
702
199
532
950
7
351
640
390
511
655
735
648
200
624
383
536
806
191
518
22
736
947
873
971
226
465
714
306
48
40
194
95
768
379
666
264
118
275
765
974
606
332
988
500
484
889
416
590
361
282
999
297
997
876
299
322
591
691
680
994
423
216
868
316
447
48
536
690
175
312
206
425
979
102
339
425
229
299
513
780
851
58
935
927
651
197
273
191
686
703
411
58
651
147
702
278
366
824
415
725
ShellSort - 0 steps
432
302
451
294
971
816
980
536
981
883
467
409
311
669
406
553
412
572
549
180
51
531
789
295
525
348
214
875
8
250
80
471
262
845
836
15
692
671
377
195
231
326
522
369
319
915
469
735
623
696
933
585
670
215
950
204
237
900
59
271
537
621
393
713
255
594
360
961
990
410
248
59
213
549
69
138
79
722
714
621
897
185
213
948
363
730
535
342
310
542
824
760
257
935
12
3
250
345
155
364
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