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.

277
751
48
376
245
709
456
889
309
951
30
491
633
311
624
633
359
220
342
226
936
55
400
549
310
195
99
616
978
879
157
52
367
233
567
286
957
830
690
715
761
16
360
699
427
128
500
389
15
252
949
663
270
138
293
543
718
190
748
463
328
749
526
681
646
805
372
138
408
520
466
21
544
10
602
282
348
998
108
943
671
862
314
488
989
143
219
669
747
694
54
726
529
967
622
180
328
420
307
755
BubbleSort - 0 steps
231
133
998
792
236
159
679
665
26
698
542
839
979
201
439
433
482
833
786
839
903
888
766
740
609
79
457
999
912
836
855
928
156
873
467
414
635
97
636
700
287
200
931
728
721
376
159
317
783
331
269
789
45
384
110
38
718
167
447
189
948
268
498
869
201
551
219
778
251
807
652
134
644
568
389
215
410
670
891
830
340
137
751
73
727
893
888
643
679
898
179
640
547
56
437
906
929
841
167
612
InsertionSort - 0 steps
445
551
630
22
853
989
579
727
400
846
480
405
535
477
511
125
242
198
693
805
166
660
302
161
511
213
463
513
249
447
354
632
294
477
374
43
80
948
570
451
764
867
229
330
960
338
840
898
482
448
342
299
624
453
653
116
457
28
416
763
592
992
291
703
677
50
843
404
494
168
851
75
376
431
714
764
272
951
670
490
166
447
970
103
525
611
398
899
466
14
88
430
355
862
237
245
493
15
78
207
ShellSort - 0 steps
598
184
220
879
612
310
650
677
551
712
413
381
996
438
379
601
913
236
82
70
529
511
111
490
469
407
161
699
300
34
681
118
349
25
573
44
823
55
318
372
341
923
880
543
127
210
124
715
195
914
973
40
199
63
327
837
822
66
412
32
224
546
717
592
500
398
232
257
744
970
45
182
124
768
513
179
985
842
326
557
680
27
819
773
649
531
513
327
215
272
175
341
898
753
578
152
21
587
395
951
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