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.

928
680
227
480
983
521
351
682
741
780
521
628
849
613
342
839
848
5
754
326
941
499
649
305
709
235
239
583
72
555
588
377
289
343
358
454
575
515
43
602
8
115
71
224
476
405
936
693
645
465
225
683
660
268
204
220
279
169
720
542
219
399
537
976
473
613
198
550
983
403
887
668
580
390
684
237
617
422
828
77
550
763
409
228
118
162
195
134
703
706
520
378
977
187
157
560
910
280
119
396
BubbleSort - 0 steps
812
531
663
899
980
810
961
378
504
271
105
145
825
380
197
361
52
951
530
554
854
548
264
972
237
510
295
241
106
143
882
104
753
472
193
875
428
784
421
939
285
376
467
9
173
646
791
540
637
432
691
929
640
920
305
142
235
498
761
394
293
582
121
426
662
793
392
321
802
35
222
122
410
787
394
627
920
174
822
181
11
275
212
843
729
595
141
583
827
320
12
977
231
71
883
710
715
813
537
634
InsertionSort - 0 steps
161
196
852
638
742
714
1000
363
486
806
15
740
154
488
568
433
895
765
399
873
686
919
99
683
396
963
976
488
892
910
835
313
111
556
640
397
735
366
538
477
177
905
366
327
379
593
463
799
673
601
561
773
959
163
785
45
67
810
122
849
692
372
393
468
432
917
143
557
317
805
527
31
905
891
426
597
330
876
704
485
562
806
923
173
613
154
769
298
237
485
535
354
249
29
497
451
83
296
329
1000
ShellSort - 0 steps
258
57
392
64
686
867
845
615
381
864
731
940
303
326
160
491
170
875
531
743
844
505
433
204
319
942
828
831
601
156
930
909
541
928
358
491
92
338
620
846
628
73
219
796
63
314
906
454
676
414
726
945
963
623
316
991
822
417
845
640
817
540
934
10
728
945
912
150
308
742
363
87
675
268
710
784
172
785
400
63
347
931
5
619
116
921
42
382
112
627
67
680
39
50
605
819
50
87
204
59
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