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.

277
328
251
483
539
589
408
699
551
473
391
639
306
471
527
807
849
630
393
758
293
679
282
155
308
906
758
182
723
90
844
608
401
723
101
192
328
133
957
193
38
957
317
378
277
294
334
455
810
510
786
341
487
346
232
55
99
298
640
817
619
292
922
706
865
329
223
887
342
776
795
863
498
252
189
947
49
819
24
650
670
146
31
838
879
857
519
795
235
232
162
29
709
282
822
320
880
970
128
610
BubbleSort - 0 steps
715
885
956
583
429
10
205
812
607
576
551
742
591
298
713
847
593
335
136
550
998
66
554
668
698
75
764
769
614
170
1000
91
472
312
609
490
458
49
361
991
789
269
731
368
548
825
373
884
200
446
307
118
267
297
761
169
800
171
672
190
280
349
438
475
824
659
353
127
578
91
845
816
348
294
239
935
105
506
641
459
612
269
694
436
251
473
472
969
997
151
811
950
275
598
755
626
77
205
493
388
InsertionSort - 0 steps
727
232
29
552
407
565
57
906
115
865
55
535
767
985
492
466
155
511
39
399
693
502
432
77
948
563
828
19
664
440
904
740
117
777
129
622
6
275
119
63
871
454
469
806
745
699
803
492
453
233
167
544
873
645
970
941
383
267
371
260
253
376
993
363
660
832
799
222
212
580
255
518
112
35
956
522
812
318
848
723
203
60
951
506
126
125
704
909
737
103
217
751
472
992
973
208
488
859
501
710
ShellSort - 0 steps
76
589
944
853
476
370
915
590
949
304
610
656
863
157
338
623
427
323
104
157
223
834
295
892
178
246
436
425
809
465
47
734
686
441
280
357
121
628
318
395
308
311
817
973
97
101
389
869
344
11
990
343
651
681
420
801
804
429
915
398
146
843
750
18
118
828
67
117
134
405
464
816
146
619
501
809
710
701
767
726
969
107
395
295
381
736
91
925
86
351
282
820
894
866
616
26
728
807
259
970
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