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.

372
369
577
41
979
728
439
228
974
410
352
667
478
588
177
536
504
358
903
760
413
906
378
254
125
940
677
152
291
884
673
852
156
617
936
422
80
919
508
643
868
51
175
274
389
268
654
961
83
365
377
287
609
90
466
996
758
330
686
59
307
723
873
40
545
351
140
250
772
959
932
267
480
519
663
72
732
844
519
491
717
410
333
45
299
76
610
467
412
445
590
624
334
299
15
225
228
434
215
311
BubbleSort - 0 steps
585
272
486
17
890
602
544
123
499
555
721
526
774
496
168
195
104
760
534
393
317
346
151
819
204
259
353
879
616
208
781
261
742
498
127
730
996
778
104
541
651
561
659
232
210
631
442
913
462
32
994
225
970
477
828
546
266
100
292
565
142
763
985
656
50
759
391
528
338
261
655
256
734
623
811
563
702
410
127
262
662
682
237
502
366
55
889
407
148
519
545
805
643
658
188
827
193
865
644
116
InsertionSort - 0 steps
824
890
477
180
571
826
607
487
755
59
653
324
334
702
140
224
587
679
192
476
658
968
201
537
888
329
22
179
829
779
519
108
405
428
45
357
927
840
291
631
196
627
228
818
584
567
983
169
790
137
28
198
148
805
334
480
773
382
436
685
423
387
993
202
880
602
507
64
583
886
281
880
86
771
824
259
451
524
993
152
898
145
486
803
209
29
964
700
591
857
386
199
11
497
29
455
452
257
223
821
ShellSort - 0 steps
836
307
779
515
187
731
322
228
35
44
581
88
1
654
702
988
527
656
348
463
939
27
938
302
280
512
371
422
763
5
338
461
99
305
201
663
443
184
644
113
712
630
382
565
956
821
566
329
259
359
808
228
667
649
244
24
329
650
634
248
544
220
553
627
662
665
991
385
598
9
809
590
121
862
581
11
976
819
513
923
853
812
344
780
877
572
385
621
816
908
963
513
654
443
303
70
243
400
486
893
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