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.

190
279
258
569
124
174
30
269
743
83
345
334
217
13
467
631
397
534
259
671
14
807
756
95
932
936
171
229
671
567
370
777
708
837
850
556
705
199
755
837
612
410
950
702
881
547
699
471
315
860
630
241
318
74
955
565
547
294
653
804
224
189
688
318
297
799
726
231
821
143
239
865
402
97
66
589
683
500
640
635
369
450
307
32
505
548
304
843
765
538
753
661
204
180
952
694
884
151
899
667
BubbleSort - 0 steps
349
356
170
144
347
201
739
399
990
245
390
507
778
831
116
260
609
833
382
524
994
478
517
670
690
304
863
211
64
278
706
726
562
293
925
414
202
189
351
795
8
711
536
112
123
211
73
91
600
969
333
747
776
206
812
953
357
646
444
794
440
366
254
5
481
904
39
607
475
657
409
21
838
11
868
436
995
448
423
570
110
890
424
766
231
672
612
537
898
198
690
929
115
877
343
765
71
347
368
282
InsertionSort - 0 steps
174
811
304
657
926
813
226
29
583
188
736
450
120
365
542
632
296
725
392
611
208
172
977
44
774
334
263
403
973
709
163
702
466
744
567
63
466
445
252
736
204
606
708
59
99
707
165
621
511
270
412
264
987
328
454
387
741
512
206
217
680
946
208
62
23
215
846
305
924
582
381
135
660
594
298
567
558
299
822
816
656
512
818
811
17
980
96
45
993
823
921
187
732
589
959
267
673
144
251
371
ShellSort - 0 steps
169
652
814
945
253
716
928
655
302
503
700
804
248
565
442
510
898
252
450
245
795
694
110
314
903
619
794
742
218
350
655
28
5
367
399
176
716
891
752
427
815
554
887
138
809
359
958
42
151
137
860
158
596
474
959
403
877
70
4
689
297
482
22
644
412
339
853
460
759
898
799
838
128
518
211
398
373
161
214
606
557
574
818
677
412
52
600
593
684
48
340
423
294
225
429
641
376
634
334
686
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