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.

665
807
810
419
258
954
267
815
726
23
374
402
159
720
797
562
424
30
285
569
287
727
108
708
924
97
148
325
776
499
737
691
565
848
987
589
608
989
606
473
178
90
575
233
345
724
641
404
385
31
227
717
381
671
974
810
96
514
934
517
87
559
303
482
112
10
140
899
588
990
451
595
729
84
95
150
823
921
458
138
806
296
8
169
930
448
246
587
801
440
568
859
522
89
251
400
841
562
268
115
BubbleSort - 0 steps
658
814
668
550
475
937
822
336
813
247
411
588
992
399
679
608
150
978
740
460
473
530
106
927
276
820
227
140
830
553
18
203
399
360
355
978
383
855
230
555
530
459
285
546
654
158
776
480
677
363
436
138
315
11
361
472
757
305
860
418
195
455
543
167
579
596
27
882
685
161
610
761
134
88
587
446
150
73
543
300
759
426
873
566
142
961
676
602
920
191
176
97
420
224
638
934
661
268
953
308
InsertionSort - 0 steps
976
53
826
601
111
444
515
464
406
258
789
527
127
473
564
342
626
785
767
368
323
181
910
26
621
870
571
768
573
242
987
768
543
782
594
71
698
209
501
44
400
950
361
26
941
407
238
906
811
490
614
9
859
481
453
224
976
596
815
754
623
541
652
704
922
844
967
358
529
646
367
761
735
328
702
397
848
20
861
438
419
750
468
183
758
354
791
624
452
221
839
493
16
502
744
757
564
684
394
740
ShellSort - 0 steps
790
201
961
903
242
653
570
872
893
696
155
487
680
923
238
324
729
459
537
144
572
199
734
36
640
960
954
893
300
166
67
13
594
356
202
249
767
82
347
685
418
299
191
565
938
276
948
543
338
798
312
86
856
728
583
802
820
553
819
92
880
351
822
934
985
485
18
453
747
405
970
946
353
264
3
442
210
770
889
805
499
184
320
794
231
199
273
6
594
949
106
245
104
785
805
69
387
286
972
810
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