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.

940
776
360
553
962
897
3
108
549
138
916
327
656
918
355
964
333
243
965
496
5
781
574
664
910
92
84
463
705
619
707
684
212
692
511
398
281
620
777
63
599
951
823
215
34
533
462
877
915
27
471
77
131
558
468
426
423
697
589
789
11
473
956
635
139
175
724
972
136
332
941
754
235
389
316
214
394
934
305
85
770
486
818
684
513
761
999
148
802
756
442
686
450
288
175
676
586
316
891
487
BubbleSort - 0 steps
904
203
540
498
527
344
811
430
543
569
345
629
768
777
226
523
154
886
332
811
727
831
966
164
479
790
460
463
493
983
759
136
494
803
155
868
615
41
203
761
886
790
522
617
912
754
206
314
135
809
196
601
286
318
562
480
112
477
513
414
492
190
523
97
98
132
407
613
402
352
390
217
954
467
581
504
967
957
592
365
992
479
259
692
680
501
338
586
554
884
337
693
136
990
349
945
380
624
38
902
InsertionSort - 0 steps
431
748
99
290
620
759
120
861
110
321
125
906
701
754
766
239
397
177
419
235
650
835
7
113
659
549
379
381
832
269
92
815
191
435
894
400
607
476
437
536
741
877
234
930
509
414
619
436
129
182
658
701
137
173
130
378
716
666
985
169
735
274
564
884
588
941
64
27
298
139
768
655
164
993
369
158
109
328
442
345
255
213
201
353
739
78
568
913
500
816
331
290
924
83
786
915
295
686
182
852
ShellSort - 0 steps
630
416
859
240
51
979
401
335
119
257
385
675
453
422
513
689
834
934
626
480
367
495
161
814
386
335
2
841
5
245
579
381
885
615
248
200
966
493
596
821
413
837
719
803
383
694
337
762
21
782
322
534
244
99
849
597
401
785
550
244
472
367
204
477
873
647
400
734
787
718
942
378
984
337
355
221
215
381
753
988
549
49
455
560
176
465
878
371
70
800
878
684
213
560
822
550
356
562
718
629
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