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.

805
177
909
518
85
817
941
735
447
619
324
28
121
309
287
237
148
91
813
659
814
578
349
735
999
806
759
36
787
704
371
793
457
521
542
77
759
204
452
567
345
962
454
210
550
401
764
804
199
443
111
331
777
125
838
801
928
691
513
716
739
350
910
817
360
784
136
946
534
30
680
2
975
434
17
833
582
821
93
847
589
588
284
288
109
806
841
355
691
270
750
683
330
743
155
81
574
794
572
254
BubbleSort - 0 steps
477
136
112
662
416
817
931
417
15
219
551
25
670
534
182
873
189
780
544
781
615
533
570
462
266
479
332
633
647
713
956
387
449
354
221
175
648
416
898
161
583
100
272
399
56
141
962
350
409
115
556
8
145
962
949
330
179
692
393
996
510
580
828
770
170
286
759
839
7
799
419
264
712
662
905
950
716
196
439
217
219
901
499
687
235
408
404
796
376
824
82
116
730
680
402
845
253
569
412
237
InsertionSort - 0 steps
404
452
585
415
581
697
905
950
649
296
713
712
416
46
431
528
148
628
407
945
872
792
835
884
301
913
433
605
503
170
359
354
471
56
485
190
496
780
977
278
825
342
39
401
290
17
89
165
673
377
994
388
622
556
303
132
549
161
20
525
692
589
989
866
268
683
999
558
370
6
867
264
69
805
984
633
724
104
149
724
129
837
162
789
899
883
733
692
806
508
904
77
809
572
309
783
843
613
474
1
ShellSort - 0 steps
669
984
453
684
897
996
518
635
335
54
377
262
710
702
733
16
740
514
267
975
800
7
527
836
930
411
661
142
799
404
994
311
348
330
917
218
355
176
397
125
150
905
848
558
182
624
51
376
401
489
380
654
308
798
252
958
13
299
413
455
577
775
209
707
623
694
412
550
200
831
965
2
373
263
373
773
341
218
424
159
286
71
111
254
548
794
91
613
464
432
566
418
178
612
197
136
351
131
588
882
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