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.

436
561
818
15
895
425
530
723
204
722
702
381
174
76
58
495
399
442
939
278
873
520
566
496
833
900
923
662
437
327
57
903
828
45
100
178
392
454
460
978
424
156
506
61
983
502
818
241
55
531
387
499
525
326
329
848
469
577
462
119
866
519
842
687
469
69
693
816
632
780
940
91
80
362
259
555
800
716
402
947
157
641
197
248
113
870
839
576
309
72
389
682
455
439
803
726
48
474
572
117
BubbleSort - 0 steps
282
138
278
985
109
926
113
548
27
126
536
144
724
517
41
264
531
508
214
8
274
991
898
15
671
327
147
194
691
386
34
196
54
425
313
368
544
667
20
975
450
383
192
825
788
240
173
837
804
49
609
63
463
207
17
638
728
921
567
561
958
239
977
300
514
164
416
49
50
553
997
177
323
633
623
336
942
686
789
95
233
754
460
435
339
231
7
550
575
45
479
905
514
68
320
834
631
793
830
589
InsertionSort - 0 steps
605
46
875
774
186
999
363
545
128
86
438
694
599
793
329
930
740
35
715
518
57
875
830
95
556
495
99
83
498
329
84
665
807
464
372
923
201
731
951
757
620
355
155
403
367
870
855
808
335
548
496
804
508
807
76
475
821
254
247
244
455
766
330
696
429
882
364
703
415
524
580
114
631
542
709
11
74
424
698
346
634
403
97
875
89
183
838
734
814
454
178
988
812
486
956
519
418
419
211
322
ShellSort - 0 steps
210
624
611
811
980
4
847
355
53
880
388
199
563
426
709
715
323
59
484
890
770
891
396
718
383
926
816
976
892
360
193
878
834
489
719
152
487
432
796
37
679
530
703
836
403
966
197
268
921
364
52
434
199
127
153
882
908
606
83
105
726
601
573
747
665
378
487
81
725
638
53
434
223
935
158
58
216
70
557
89
590
735
397
76
440
467
183
764
698
426
103
878
80
284
35
582
515
402
772
314
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