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.

347
467
118
763
341
253
817
367
224
219
850
583
519
544
128
560
976
129
238
996
304
31
841
900
162
549
318
344
779
406
819
400
770
958
955
543
404
130
215
479
56
812
515
803
157
27
488
42
381
395
76
813
905
760
169
642
452
917
373
106
210
676
463
87
585
193
162
990
862
206
369
530
97
670
272
829
113
14
706
827
485
311
139
199
198
487
369
180
930
990
649
81
764
215
171
296
679
932
41
991
BubbleSort - 0 steps
247
749
948
707
434
670
759
558
966
575
645
735
221
866
957
761
593
969
502
640
73
953
236
257
435
968
988
153
902
561
448
233
4
190
753
968
264
501
672
976
627
29
129
679
71
718
470
637
379
776
15
926
988
317
208
183
564
474
728
326
456
349
510
551
951
643
309
249
678
48
150
986
316
781
456
833
413
477
581
678
635
206
142
816
420
100
4
641
278
335
974
544
193
403
802
727
324
862
815
993
InsertionSort - 0 steps
368
610
469
38
575
452
943
461
203
520
637
565
58
387
169
35
251
82
599
397
186
237
157
475
469
450
323
42
628
176
183
199
197
949
176
578
340
409
183
234
926
558
560
922
137
313
517
921
531
184
284
849
212
109
746
520
440
901
538
740
545
927
307
867
930
396
297
377
179
96
546
955
428
45
46
12
522
200
143
547
500
83
999
870
724
716
525
370
15
855
335
969
560
383
553
39
289
856
52
606
ShellSort - 0 steps
593
666
550
282
622
49
582
674
306
871
541
806
112
351
844
327
888
42
364
312
307
108
462
123
866
417
911
602
523
503
769
94
552
732
741
224
969
390
142
689
776
376
980
564
817
530
527
909
100
244
497
437
633
62
900
766
711
125
369
452
955
658
890
197
20
965
540
489
373
226
716
755
940
603
744
88
624
75
371
210
79
510
502
391
520
183
47
231
488
898
339
694
879
426
748
724
178
371
763
719
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