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.

568
411
916
758
733
462
381
193
782
593
43
58
542
586
833
895
150
477
900
619
869
661
790
4
127
695
67
178
626
153
246
884
22
466
456
408
619
969
159
292
904
941
25
914
338
88
654
966
720
403
622
488
154
669
291
117
794
422
434
239
533
447
529
369
324
425
152
829
566
911
259
29
494
143
536
340
740
741
868
267
921
697
284
711
355
313
522
294
550
341
537
914
687
307
988
12
866
522
522
136
BubbleSort - 0 steps
74
826
317
302
619
562
797
791
821
859
861
895
755
195
248
958
330
461
197
348
502
155
361
426
395
496
630
67
918
799
546
731
638
589
882
996
539
362
161
676
581
16
849
618
157
400
535
569
507
859
271
519
875
635
40
117
848
486
856
64
514
105
397
706
964
408
374
54
723
404
580
835
702
900
240
369
865
274
803
435
567
240
20
964
603
590
323
859
995
537
783
942
731
190
910
160
483
385
658
971
InsertionSort - 0 steps
190
842
256
113
299
214
346
995
492
979
758
3
464
997
746
269
90
285
321
706
715
336
860
568
882
915
848
781
199
251
597
24
841
481
90
386
573
25
450
41
199
888
747
617
831
523
187
481
578
602
266
105
845
603
549
541
527
308
562
771
261
90
278
8
366
953
845
287
178
996
562
68
727
449
478
74
293
175
433
924
858
176
62
774
267
959
576
689
45
95
448
756
614
574
927
468
953
806
250
787
ShellSort - 0 steps
618
600
832
462
85
485
764
414
321
567
450
324
574
31
215
898
648
526
896
259
622
110
238
289
164
376
114
68
70
335
519
456
578
132
451
712
434
40
325
437
447
684
495
944
850
429
857
699
304
875
681
340
137
464
611
881
411
122
152
609
559
353
648
27
929
896
875
619
508
976
912
37
37
878
577
894
197
297
692
969
412
110
852
952
628
887
416
436
765
89
224
740
586
419
601
222
760
804
230
667
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