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.

404
743
341
888
135
152
611
19
449
682
900
207
331
4
151
380
500
743
761
649
342
427
411
555
4
655
322
175
526
280
374
171
889
36
161
931
31
161
282
502
435
860
136
996
574
929
274
918
829
752
369
501
231
204
225
554
824
813
339
514
145
11
100
4
148
374
821
542
636
565
280
994
383
309
647
207
969
907
566
87
344
853
583
342
977
14
306
618
494
219
113
896
3
219
353
692
299
141
846
646
BubbleSort - 0 steps
697
984
570
207
181
614
352
80
30
454
198
847
944
741
350
980
677
213
695
138
761
879
753
506
729
643
151
126
89
908
524
552
746
180
147
97
278
72
395
484
327
697
957
612
99
874
539
323
417
803
547
749
762
10
580
463
672
895
130
813
153
20
892
577
314
307
865
570
826
821
581
255
983
881
460
833
336
246
133
719
90
824
957
92
562
647
232
807
900
519
371
965
497
910
708
292
236
386
749
565
InsertionSort - 0 steps
250
116
602
954
358
214
579
168
505
32
490
934
119
426
753
608
395
479
732
343
670
470
237
584
811
524
728
650
582
28
516
752
759
497
735
440
557
321
435
397
862
968
47
279
453
245
772
294
614
848
284
809
869
354
388
465
942
855
299
89
860
54
965
959
980
963
94
775
460
376
54
651
665
529
408
629
545
969
497
830
494
487
806
235
841
12
867
500
82
215
374
746
27
830
289
171
795
323
325
493
ShellSort - 0 steps
542
775
210
306
460
131
798
480
789
945
415
676
750
772
794
81
647
636
952
106
25
680
809
701
103
804
706
56
896
366
324
877
288
911
940
392
487
287
663
291
836
404
817
666
268
229
677
99
62
13
385
611
565
55
728
836
119
201
457
409
726
606
553
917
11
731
344
83
724
463
37
459
722
432
140
135
645
638
82
718
253
350
327
847
480
495
902
911
491
504
906
913
782
482
379
57
473
790
262
470
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