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.

412
377
196
297
795
608
883
581
341
329
387
99
116
205
598
512
600
444
375
586
110
328
196
890
16
184
74
412
58
649
266
211
176
4
851
67
234
311
219
353
6
57
216
948
161
667
273
43
372
115
722
314
51
386
722
578
365
454
124
129
847
147
54
7
104
819
866
64
512
460
987
598
832
508
452
509
998
647
781
963
379
366
871
299
185
929
395
211
816
203
366
129
987
159
458
131
450
544
97
708
BubbleSort - 0 steps
809
607
865
30
653
668
801
693
464
290
427
551
310
436
601
737
638
203
829
286
922
453
71
25
542
533
41
366
911
211
635
341
741
257
1000
688
59
865
358
916
440
467
184
833
507
873
331
261
798
20
614
449
787
761
582
780
234
257
372
49
421
453
693
117
500
175
676
779
451
557
481
799
332
580
9
541
845
13
443
261
19
753
707
920
428
227
884
960
944
199
145
32
301
110
828
96
874
64
914
531
InsertionSort - 0 steps
8
376
621
170
801
462
36
405
730
23
512
631
582
446
570
72
917
183
501
789
282
892
961
949
859
334
589
628
999
193
813
400
165
581
867
834
421
305
715
640
29
789
995
339
32
657
237
34
231
401
855
681
556
979
978
498
563
833
559
702
534
153
530
975
203
868
175
488
912
437
120
532
521
803
596
963
495
866
768
473
900
649
626
194
970
738
725
523
53
484
446
823
483
659
562
696
487
359
454
103
ShellSort - 0 steps
211
122
399
130
357
762
580
689
775
842
695
385
474
449
708
559
295
75
325
27
142
396
179
144
148
146
817
522
617
40
354
399
889
9
910
675
558
779
697
558
19
412
706
814
659
754
136
700
466
757
47
713
35
227
615
210
147
837
584
396
874
532
75
645
339
694
325
370
569
34
542
238
606
395
411
65
220
839
124
180
421
640
50
640
665
272
798
991
364
151
311
386
730
141
730
573
512
974
756
991
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