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.

626
997
51
319
917
751
779
273
212
396
434
474
827
25
463
427
31
948
264
978
950
367
371
418
387
217
144
253
575
440
320
746
754
671
286
447
824
976
202
913
225
276
454
56
374
972
856
102
837
234
298
938
680
525
107
385
492
26
750
46
397
640
117
292
50
180
544
891
889
337
375
869
427
24
384
834
576
418
470
936
853
673
952
963
749
740
909
365
43
725
121
555
322
852
683
143
881
92
635
790
BubbleSort - 0 steps
606
398
228
393
155
620
117
352
48
126
733
329
3
611
102
149
123
121
673
166
299
149
474
459
675
147
202
25
348
650
67
860
725
160
640
508
793
241
449
276
862
634
56
414
40
957
30
47
875
5
924
367
87
759
498
658
555
593
916
528
247
972
252
661
906
991
734
258
792
520
372
268
630
42
273
629
85
324
959
890
150
3
650
190
9
216
710
325
559
629
991
579
600
381
893
880
861
612
390
135
InsertionSort - 0 steps
205
582
13
86
884
247
40
500
655
866
858
562
976
501
171
928
499
976
571
447
292
746
534
732
425
292
968
425
319
890
29
112
503
582
9
418
792
64
754
485
919
843
370
757
394
272
523
806
573
896
259
725
644
285
636
892
263
874
837
909
101
558
136
530
380
440
109
233
962
224
357
457
211
502
31
271
295
155
802
556
917
765
96
353
812
251
819
365
813
889
183
231
616
423
210
530
155
919
890
437
ShellSort - 0 steps
126
792
119
784
951
898
845
131
780
529
320
777
83
626
571
678
561
833
904
357
154
740
657
256
16
236
643
493
184
868
932
342
15
780
318
364
594
700
725
708
265
108
215
768
399
217
999
776
780
696
332
830
555
742
904
648
96
16
983
271
238
70
794
518
788
39
518
991
51
466
698
984
828
204
59
765
920
500
729
822
627
389
678
259
411
589
601
116
359
152
863
566
368
935
582
708
348
456
762
191
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