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.

292
943
480
72
734
933
668
651
204
906
356
7
865
929
599
841
808
739
266
989
140
315
348
422
471
263
919
857
914
433
694
187
513
189
102
662
386
755
452
258
307
850
677
521
965
671
775
699
734
607
839
371
221
404
202
915
993
998
344
785
506
928
548
576
287
994
943
15
894
249
87
535
543
192
635
696
381
450
541
811
21
126
103
880
903
770
267
873
849
233
94
408
84
376
100
563
98
541
412
617
BubbleSort - 0 steps
188
657
747
470
926
797
620
962
345
678
126
941
381
894
750
704
322
525
468
392
71
265
524
900
513
575
806
872
819
963
951
716
102
949
320
621
853
638
46
442
646
493
316
619
790
826
929
519
510
870
396
411
87
165
50
871
654
896
601
59
800
11
172
567
498
433
738
255
708
528
467
470
477
932
822
635
297
405
918
403
222
390
502
181
764
594
726
781
344
549
475
900
998
981
170
441
126
531
395
202
InsertionSort - 0 steps
516
97
44
507
959
312
848
707
625
246
937
795
273
227
20
771
301
205
97
909
528
837
949
370
545
7
372
217
362
811
343
356
451
805
746
274
955
255
309
990
569
260
12
933
127
764
150
13
885
978
786
586
440
763
186
590
955
692
43
753
420
773
427
364
416
363
905
926
987
145
761
126
415
109
333
299
489
563
868
755
286
344
553
344
594
67
7
501
476
434
499
384
448
432
672
789
759
648
214
419
ShellSort - 0 steps
380
800
647
258
492
261
948
123
964
852
983
672
669
577
626
291
744
585
16
512
165
491
503
75
29
408
487
594
498
658
652
450
149
740
522
619
830
904
117
818
51
289
175
375
467
327
979
476
613
491
101
524
739
935
758
339
996
838
73
978
658
450
192
361
869
363
828
602
363
881
82
542
917
634
789
965
241
213
30
727
771
660
956
729
729
11
105
6
540
125
613
361
920
32
904
681
245
32
413
635
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