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.

774
204
814
218
784
527
486
386
34
393
590
498
322
969
946
242
5
219
274
665
852
885
364
824
712
129
127
476
728
38
575
789
22
286
366
818
229
502
930
308
352
855
190
780
547
785
402
890
971
278
561
836
754
115
557
290
150
574
246
662
550
628
110
817
267
818
460
218
54
8
275
763
249
450
873
629
283
183
448
138
830
510
190
695
173
915
433
924
675
122
789
390
702
31
457
746
94
191
544
14
BubbleSort - 0 steps
175
867
516
209
290
350
555
427
689
586
554
632
958
920
167
283
642
468
782
74
53
640
17
369
839
26
266
591
740
25
73
685
855
183
827
348
765
601
308
432
289
956
509
675
893
701
838
228
847
684
446
529
176
576
515
401
308
795
221
192
535
490
615
729
457
393
636
412
902
53
766
440
817
891
69
217
627
268
363
263
51
219
426
946
688
164
282
246
726
539
887
388
695
287
938
527
474
573
484
360
InsertionSort - 0 steps
734
239
92
514
458
871
708
358
476
294
517
439
806
118
410
726
57
13
884
470
576
980
506
426
540
27
930
937
797
473
513
708
860
90
395
518
657
780
108
73
611
38
207
445
500
500
336
762
741
1
826
589
284
155
300
218
605
24
477
605
176
291
883
884
281
378
806
882
428
584
163
722
771
668
200
969
810
93
148
375
85
276
136
967
667
269
249
177
507
800
451
626
721
998
270
695
591
462
535
232
ShellSort - 0 steps
536
333
581
486
495
227
909
985
152
888
876
870
286
248
94
939
483
890
665
832
514
486
151
41
839
328
949
905
895
440
119
426
608
311
407
476
319
438
438
796
309
220
536
223
664
654
850
401
424
606
392
6
39
150
643
376
162
63
136
160
851
611
217
736
199
993
918
455
655
605
307
830
774
293
653
943
932
356
442
518
383
446
184
650
416
615
999
441
169
1000
605
324
708
569
407
419
693
390
827
99
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