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.

879
231
366
125
437
312
599
580
254
245
395
827
737
966
596
562
545
865
962
335
920
471
197
832
69
465
134
812
937
190
241
81
768
875
7
590
255
924
824
963
795
385
499
80
810
21
205
264
186
811
320
598
260
920
559
599
285
373
147
538
350
421
258
591
864
915
219
780
78
131
194
597
443
883
38
263
236
263
132
19
565
267
501
332
345
86
30
191
984
52
615
905
202
676
107
698
455
777
309
254
BubbleSort - 0 steps
954
34
945
657
568
952
878
572
362
846
534
520
32
50
954
69
72
765
667
270
718
943
172
224
984
625
117
833
718
962
275
232
615
181
70
489
963
904
91
970
448
57
710
341
13
361
611
37
663
520
49
82
405
740
247
537
796
962
974
278
859
84
7
527
570
595
291
54
451
738
399
850
982
844
137
578
825
340
844
158
378
893
720
520
380
558
535
329
658
163
19
712
913
739
721
934
880
212
276
873
InsertionSort - 0 steps
173
743
198
254
30
396
744
718
972
547
538
418
69
680
635
287
356
499
459
856
167
505
33
221
610
545
598
174
607
772
191
912
769
319
841
710
928
502
579
284
638
923
230
474
973
923
986
670
248
794
136
380
775
569
700
619
710
5
292
351
791
408
114
330
876
887
189
334
590
934
497
889
785
862
373
982
856
494
383
657
389
532
858
509
354
687
384
330
813
22
226
852
538
804
434
102
719
51
146
950
ShellSort - 0 steps
533
809
475
151
528
707
537
199
826
171
339
923
87
175
294
177
523
381
614
701
525
597
335
257
632
996
314
339
786
764
724
802
331
296
946
394
631
815
961
652
448
471
850
292
844
452
127
510
599
770
472
499
340
237
134
480
225
86
230
724
409
847
917
339
436
222
886
396
92
330
676
805
424
74
456
515
516
637
659
871
509
276
606
994
560
211
617
143
255
719
802
719
48
487
123
713
490
66
888
897
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