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.

699
646
479
74
237
646
689
742
320
982
756
394
97
100
763
566
152
3
177
209
726
338
890
314
322
936
374
568
12
442
504
157
907
296
280
12
801
839
205
568
114
294
569
735
448
769
476
249
700
113
444
435
431
230
240
262
287
436
111
538
453
135
466
580
317
832
591
859
73
734
979
390
195
929
931
30
810
770
2
116
860
929
904
283
104
885
310
878
776
389
777
837
26
92
995
58
213
344
212
529
BubbleSort - 0 steps
323
103
413
537
116
914
293
841
853
825
986
801
702
202
875
669
508
202
607
433
690
851
797
410
298
571
924
983
926
972
452
226
517
593
283
444
562
385
569
5
17
245
407
244
830
239
651
955
102
373
837
800
912
680
882
87
382
489
507
163
941
942
527
275
934
583
443
646
797
291
71
110
129
834
65
649
28
535
789
110
739
47
701
510
244
653
3
394
43
601
546
790
363
923
779
345
118
486
355
622
InsertionSort - 0 steps
300
690
873
144
186
979
750
200
392
319
237
418
947
186
924
28
624
710
630
753
219
571
855
305
790
540
368
391
931
193
210
275
832
121
169
311
994
288
55
352
864
720
883
52
409
833
740
820
221
508
785
299
631
138
158
652
719
864
805
699
733
133
214
548
899
5
739
425
267
980
858
402
164
337
43
375
1000
997
289
465
794
247
260
564
789
371
890
475
193
967
481
809
229
344
787
667
46
467
565
362
ShellSort - 0 steps
357
407
434
878
305
226
727
498
928
284
642
482
60
796
373
127
262
777
309
201
649
213
153
900
388
597
670
112
448
197
199
548
237
834
552
521
654
276
713
71
719
401
764
591
419
633
694
819
327
158
874
832
805
124
340
299
749
957
70
135
605
863
381
327
343
236
631
440
803
771
133
10
482
729
850
494
36
978
128
312
595
430
276
350
582
376
733
913
811
31
97
736
299
83
588
800
945
836
78
559
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