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.

312
463
300
801
14
785
886
200
424
664
3
384
595
627
539
230
569
473
818
144
828
987
388
912
344
369
353
876
558
972
650
479
600
263
394
414
549
796
192
362
227
950
952
915
358
408
545
512
10
679
856
440
25
853
311
750
670
195
701
285
811
279
733
169
498
689
897
576
604
458
490
832
250
432
83
255
702
86
147
181
844
61
932
189
750
12
741
677
872
906
373
809
592
133
832
728
388
29
8
775
BubbleSort - 0 steps
464
433
884
998
330
213
4
270
607
938
395
907
584
951
820
329
636
287
615
138
11
870
51
820
875
249
611
902
953
667
911
313
462
502
177
960
989
780
619
554
590
519
237
42
146
345
186
813
608
735
145
752
737
228
92
282
937
98
119
419
47
170
635
310
758
841
715
954
789
69
724
3
565
381
184
410
881
218
185
547
767
513
629
988
685
994
694
254
768
708
540
90
92
602
745
581
190
434
231
64
InsertionSort - 0 steps
510
179
676
92
391
604
770
104
495
445
157
317
553
217
142
270
102
321
675
417
282
441
83
950
202
110
385
520
912
555
119
754
747
589
816
51
868
642
811
546
474
360
382
661
185
783
864
115
504
154
836
500
280
820
292
95
7
203
514
906
154
243
970
377
428
683
181
382
512
114
766
396
690
433
481
125
330
456
59
121
166
947
511
699
407
350
278
845
789
224
528
545
815
964
686
156
952
436
225
607
ShellSort - 0 steps
264
823
316
197
83
326
245
63
607
259
27
29
316
244
569
386
951
848
671
234
55
534
785
592
445
747
798
410
947
983
222
437
537
532
7
812
373
119
879
738
240
319
254
18
57
31
552
616
38
14
395
570
522
49
911
782
261
462
106
238
987
467
540
647
941
845
875
134
733
869
481
198
885
793
598
542
454
888
820
482
607
772
370
334
200
729
271
971
603
772
850
175
753
249
307
973
979
935
584
344
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