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.

390
679
733
464
416
272
143
965
263
883
659
983
603
340
558
520
915
938
85
695
638
669
228
760
310
379
919
830
334
964
291
708
205
452
364
515
241
302
299
813
81
326
793
421
165
613
96
250
414
504
649
31
131
791
296
611
454
867
223
550
515
798
229
519
573
186
394
938
255
735
819
679
692
536
644
77
117
20
524
470
19
502
917
800
480
437
738
169
166
930
314
364
878
416
678
678
840
713
21
197
BubbleSort - 0 steps
31
595
580
569
923
73
46
419
309
462
628
784
142
255
254
23
181
507
991
468
58
859
77
944
69
965
229
371
235
911
957
397
306
972
190
510
566
48
530
717
636
573
486
917
814
273
417
948
435
165
328
610
643
108
224
434
910
979
848
466
579
621
205
3
416
260
686
437
392
369
386
19
872
4
595
189
631
190
951
516
383
265
165
338
645
843
26
729
127
362
117
131
459
868
483
907
202
63
993
248
InsertionSort - 0 steps
957
775
481
342
780
401
937
900
322
990
87
196
489
942
571
686
45
355
152
501
701
687
960
154
543
316
308
175
315
756
948
120
935
572
122
823
212
353
19
877
191
996
937
749
237
817
416
149
827
253
972
935
521
584
773
716
948
826
747
65
138
428
907
912
559
691
259
764
852
436
441
388
180
332
633
678
62
605
4
88
112
786
496
414
57
860
542
285
684
91
364
378
336
406
876
702
515
272
691
52
ShellSort - 0 steps
526
799
420
494
782
152
888
61
189
186
430
315
548
714
341
57
750
503
262
250
466
151
220
62
371
565
211
648
555
352
381
789
298
677
42
506
71
100
692
4
183
547
688
354
597
964
583
789
679
158
709
103
424
82
507
3
959
590
663
671
503
537
612
574
220
662
716
402
249
297
271
740
131
728
367
412
88
893
683
991
768
122
551
241
277
515
300
965
582
294
656
683
621
266
363
113
934
259
799
890
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