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.

917
113
289
290
930
128
4
337
252
345
92
942
583
750
955
851
623
545
857
374
835
651
590
888
682
484
822
861
459
942
508
179
370
364
941
253
471
571
267
587
347
69
866
144
152
666
212
988
86
930
29
159
195
594
793
608
729
404
663
261
253
283
403
454
25
917
446
4
595
674
318
6
70
752
371
90
373
906
146
485
422
918
172
521
355
628
711
744
217
582
496
725
861
739
91
668
612
926
764
421
BubbleSort - 0 steps
262
777
970
465
978
672
848
1
778
240
651
292
623
442
990
275
212
287
214
694
85
713
189
520
835
568
44
847
567
845
314
284
196
205
246
379
69
971
397
611
977
926
309
664
752
74
520
87
545
322
816
718
297
616
318
945
155
41
432
990
954
885
595
315
409
717
533
312
156
160
146
802
992
371
489
704
442
113
470
387
533
339
342
370
176
215
937
209
638
823
758
491
416
196
236
477
384
798
192
12
InsertionSort - 0 steps
256
837
537
146
728
8
24
983
806
699
965
301
780
418
860
394
819
633
779
330
771
317
261
855
346
17
883
311
834
226
186
159
304
866
53
955
325
238
887
582
129
992
325
333
253
425
994
801
805
787
448
756
576
122
293
519
575
386
69
673
528
952
293
362
316
501
703
345
929
431
986
262
589
957
977
393
782
253
103
12
355
505
398
126
405
327
476
406
536
207
877
846
532
820
769
861
146
673
74
2
ShellSort - 0 steps
489
997
762
507
71
513
108
157
960
373
347
872
419
816
189
920
719
293
352
878
366
123
704
15
76
544
337
733
114
260
207
135
286
209
138
54
210
283
90
351
996
501
552
976
569
853
306
602
169
899
118
393
423
732
672
48
235
495
113
262
532
186
878
908
253
324
369
756
978
792
390
1000
213
155
646
889
971
589
100
707
975
943
757
456
152
690
587
646
203
252
155
374
114
327
889
600
488
425
148
296
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