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.

495
321
590
506
584
430
733
821
222
668
533
122
92
167
422
141
87
877
888
876
229
531
679
539
853
582
44
227
793
933
60
171
16
694
703
483
754
791
146
835
371
138
469
144
145
386
216
224
274
643
915
739
1
174
831
729
699
269
811
471
510
508
4
1000
888
142
556
452
305
857
10
660
349
655
571
364
607
18
839
601
581
81
912
77
964
873
13
906
294
447
71
275
494
646
122
126
499
73
581
776
BubbleSort - 0 steps
142
125
520
917
254
90
376
531
234
124
279
902
652
102
230
691
133
934
88
734
895
701
89
725
397
79
562
434
841
824
737
592
297
579
78
572
536
606
218
602
409
307
803
910
577
331
840
401
925
587
449
517
112
550
80
133
55
429
796
170
828
975
569
192
569
636
72
250
364
237
93
422
595
582
434
352
360
374
416
540
704
74
873
32
995
538
844
980
264
729
7
699
765
181
318
833
918
204
702
734
InsertionSort - 0 steps
910
269
413
421
58
726
202
943
419
642
313
817
875
86
816
77
115
967
263
663
228
129
731
220
429
458
97
586
946
986
706
983
671
672
60
187
172
749
393
879
589
950
681
866
760
103
424
457
55
867
884
118
495
411
924
765
34
589
867
471
645
616
519
535
694
904
102
118
576
134
850
987
331
432
329
151
395
40
377
541
440
325
495
422
289
309
202
373
450
949
542
192
36
737
647
80
570
474
253
354
ShellSort - 0 steps
773
109
453
829
862
568
220
772
691
543
201
123
861
74
394
790
574
26
929
849
665
494
848
497
771
222
466
455
558
892
59
17
580
950
736
253
78
219
537
572
728
638
658
16
820
909
777
65
366
398
422
355
377
44
966
589
164
161
753
462
512
965
33
795
615
466
357
594
2
405
103
476
848
911
171
986
245
754
39
844
690
120
777
125
9
973
420
65
704
168
853
117
892
7
572
670
419
245
506
985
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