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.

105
356
715
149
247
987
124
864
87
892
772
871
834
140
800
879
92
266
411
386
684
258
256
457
540
293
546
239
687
359
360
471
24
171
448
477
560
679
22
476
563
827
807
565
196
612
58
694
426
308
982
481
453
636
379
416
3
293
459
446
72
646
718
222
478
632
257
705
357
749
632
395
323
907
433
292
476
841
951
630
170
714
860
726
795
984
656
115
944
476
231
955
684
658
856
12
192
401
912
692
BubbleSort - 0 steps
554
792
708
765
951
355
773
76
598
985
943
327
268
972
308
297
930
961
780
155
952
160
438
311
398
777
118
322
149
445
541
354
255
684
602
550
15
92
225
151
613
505
504
748
120
180
82
52
725
380
586
628
212
701
951
996
210
971
983
890
820
331
773
283
128
176
531
766
554
893
56
440
719
521
366
193
464
151
624
385
407
837
919
938
905
915
60
641
548
853
206
815
41
597
867
751
618
368
975
837
InsertionSort - 0 steps
565
981
110
840
81
904
492
571
136
83
389
123
470
20
870
742
587
741
129
290
19
78
482
914
257
943
593
455
755
474
590
173
455
52
900
62
397
598
588
389
869
740
398
317
638
783
107
501
601
573
152
221
742
270
87
680
790
566
60
942
501
792
685
540
993
147
242
348
571
926
295
280
558
602
940
319
706
620
98
273
913
234
189
845
197
958
410
175
483
211
439
842
855
513
817
736
927
910
841
825
ShellSort - 0 steps
722
609
39
460
509
437
220
769
416
831
317
92
773
391
317
947
344
405
866
939
85
603
700
176
280
400
364
790
608
5
790
491
764
265
277
698
763
464
63
634
11
741
324
17
751
328
173
17
129
971
602
911
222
189
843
791
747
292
897
579
232
494
544
577
216
292
81
223
109
163
868
433
123
474
739
253
238
112
76
868
411
840
685
708
537
882
501
588
240
670
853
289
215
395
728
171
883
567
12
121
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