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.

752
144
361
764
692
93
867
469
343
444
194
554
180
878
582
575
844
422
675
254
104
418
583
72
449
800
732
185
109
654
294
211
205
307
379
527
919
597
420
142
647
635
194
920
431
618
3
371
753
361
57
542
525
537
733
12
550
588
105
57
732
23
433
541
288
35
503
740
652
635
60
760
597
559
639
476
494
730
792
22
845
602
904
486
418
274
657
605
563
353
27
103
322
794
210
802
909
692
579
233
BubbleSort - 0 steps
227
785
942
770
87
804
621
665
909
334
828
99
470
90
886
553
479
605
528
483
266
93
146
777
749
31
733
881
873
988
616
79
808
110
367
855
819
137
994
884
270
722
196
671
64
328
325
594
180
421
123
888
365
486
419
593
834
669
741
10
241
810
56
837
49
28
335
922
961
116
315
717
680
55
286
792
616
842
64
628
599
199
773
266
254
359
479
894
808
611
872
699
472
213
536
192
168
876
572
514
InsertionSort - 0 steps
731
561
375
344
265
616
562
939
859
270
321
835
471
698
577
77
753
408
975
827
934
273
992
797
523
517
777
148
232
492
310
685
466
762
151
32
1000
950
464
224
985
150
378
681
324
399
338
758
721
130
48
13
610
538
601
285
293
911
570
250
834
148
341
399
577
605
85
983
541
890
964
620
364
668
791
174
360
917
742
510
978
564
637
892
376
329
512
180
449
547
572
649
863
507
103
360
398
336
759
727
ShellSort - 0 steps
524
955
46
408
650
558
610
785
983
424
244
749
713
159
556
670
827
122
762
530
986
340
469
155
802
165
27
651
428
609
857
3
907
459
826
177
255
724
244
183
180
38
400
204
620
479
3
303
150
283
241
217
239
706
109
679
319
215
788
391
300
819
894
877
960
452
409
463
125
31
460
417
169
409
360
59
810
913
26
320
508
228
971
77
667
739
3
94
501
971
466
185
157
570
223
931
79
102
357
721
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