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.

17
144
867
272
621
184
85
597
904
117
475
113
278
737
59
198
853
746
675
124
727
406
846
84
272
997
221
24
309
801
402
237
451
768
829
503
282
68
941
330
775
453
868
909
710
909
268
759
359
350
663
494
410
420
785
838
555
180
507
355
586
932
85
714
363
680
180
303
730
422
966
945
622
31
724
554
659
230
308
779
323
754
380
197
863
868
920
18
56
396
385
473
476
317
265
511
912
617
554
431
BubbleSort - 0 steps
82
252
332
316
549
976
475
189
786
625
21
358
733
151
462
141
274
428
631
349
245
392
69
893
305
70
171
993
401
427
127
478
660
760
131
825
600
282
171
376
720
394
151
706
843
630
139
499
859
255
408
680
290
541
743
544
411
842
555
630
304
944
110
724
622
298
318
995
528
987
429
319
360
344
394
251
431
944
696
858
192
211
385
378
915
994
504
238
694
43
916
374
393
870
985
478
222
980
53
376
InsertionSort - 0 steps
283
244
701
639
116
504
486
283
375
97
957
500
313
435
508
140
8
273
666
812
369
920
591
697
189
257
396
104
114
944
459
205
90
364
879
577
716
416
334
23
631
649
371
99
979
245
529
829
2
300
377
263
732
851
851
564
974
196
471
373
981
606
52
533
13
34
126
557
507
818
130
179
427
141
189
774
804
103
70
240
674
443
554
887
769
854
927
613
250
264
971
439
129
873
672
851
587
238
1000
454
ShellSort - 0 steps
11
378
672
984
647
131
415
772
759
327
122
37
879
163
245
970
885
190
280
617
706
35
897
349
100
448
909
766
120
618
846
345
24
654
903
634
64
608
582
815
800
531
574
507
323
675
417
677
153
35
489
399
284
677
235
235
455
951
632
525
960
833
941
402
13
240
114
856
489
46
252
983
291
117
945
721
434
533
433
427
797
950
900
136
757
760
532
394
476
702
712
71
469
785
914
541
357
412
278
95
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