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.

643
645
646
332
455
557
886
186
885
882
966
776
493
661
272
624
689
593
988
16
486
178
64
848
616
752
188
947
275
404
638
252
447
522
372
688
43
999
132
736
148
282
642
109
425
773
511
61
515
98
89
954
273
61
652
355
888
818
216
561
385
755
595
616
368
781
955
664
860
881
850
481
812
223
463
868
50
961
451
830
484
14
458
814
438
673
655
244
810
773
869
737
99
377
247
356
336
17
475
332
BubbleSort - 0 steps
725
717
632
45
420
114
97
420
238
810
998
307
488
621
924
615
419
965
348
393
87
970
279
978
400
169
906
621
399
908
342
331
678
189
523
623
604
94
267
309
607
379
652
675
447
991
782
768
850
629
371
757
376
845
249
827
541
657
137
443
313
442
544
169
871
378
538
126
172
80
566
920
320
70
228
453
214
220
644
949
185
161
446
104
697
207
400
266
649
593
661
135
788
297
97
4
800
973
612
318
InsertionSort - 0 steps
49
832
712
395
69
1000
150
629
729
840
434
367
962
703
606
190
713
442
654
221
54
479
422
775
519
870
235
707
457
361
356
630
750
542
562
131
702
370
88
596
112
351
566
112
577
147
101
806
996
997
952
978
899
258
948
237
975
794
888
617
583
430
199
815
634
972
80
390
969
17
733
662
141
433
94
200
789
418
749
986
571
500
730
773
601
908
314
463
195
945
547
973
140
990
926
134
185
410
919
193
ShellSort - 0 steps
785
86
366
262
586
581
345
124
197
326
690
480
739
960
826
558
211
432
360
269
318
664
435
397
99
97
638
325
705
185
16
49
760
788
97
39
487
662
865
9
112
617
640
354
362
293
907
467
914
933
833
470
231
397
889
320
125
130
930
882
369
924
51
713
402
357
590
72
697
381
503
489
184
124
236
618
134
568
234
39
209
793
414
323
299
88
514
202
269
945
663
332
24
346
970
961
775
281
302
134
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