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.

320
214
775
926
486
77
319
61
667
521
881
237
368
528
626
693
704
404
139
546
900
639
924
340
126
134
369
921
701
499
954
271
9
324
999
842
308
957
636
296
894
5
37
234
160
728
647
696
40
154
514
385
930
204
237
166
178
735
973
550
528
263
832
600
43
31
964
116
177
648
819
571
425
388
659
875
593
751
293
532
650
578
67
397
333
70
290
876
427
715
647
146
419
176
950
318
236
23
28
383
BubbleSort - 0 steps
3
921
571
67
852
583
121
426
47
352
72
806
490
902
150
993
639
870
407
754
245
831
365
176
504
318
885
259
613
376
297
148
434
613
40
396
184
998
397
9
606
454
170
656
472
159
150
141
767
881
777
890
970
709
384
843
515
563
403
469
856
986
349
887
842
125
741
406
161
360
5
738
171
595
677
889
799
440
529
599
304
333
921
517
347
178
187
865
820
306
318
171
572
284
914
100
286
878
545
823
InsertionSort - 0 steps
453
538
224
65
365
498
827
679
207
160
459
763
754
54
926
462
46
188
285
292
29
317
281
383
313
471
282
360
373
241
214
151
174
382
735
329
727
207
257
328
45
104
167
655
857
153
258
408
173
168
788
373
166
515
122
864
404
651
18
838
748
25
246
973
260
130
327
633
491
776
22
664
462
185
831
879
113
504
449
654
322
934
567
111
875
883
823
370
669
319
948
501
762
577
314
439
247
801
304
299
ShellSort - 0 steps
357
817
157
753
985
642
577
287
200
970
621
588
979
362
385
696
282
240
542
745
163
672
35
553
447
927
518
613
250
855
430
484
190
78
587
393
850
879
81
26
616
30
818
638
577
550
375
606
58
58
918
838
110
522
211
669
717
328
654
601
134
635
659
730
315
514
386
21
729
334
470
1000
472
926
251
863
476
1
525
83
905
632
322
452
498
320
768
518
200
328
801
463
150
750
573
856
649
133
989
829
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