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.

37
699
231
287
155
209
952
22
840
673
884
572
109
638
542
723
196
794
687
860
775
885
846
832
229
336
230
115
909
222
957
414
412
774
273
195
316
276
632
90
108
872
804
463
200
619
384
267
441
266
160
628
223
465
631
576
433
80
314
166
619
779
529
233
370
738
718
662
469
337
912
583
670
556
112
562
600
936
172
12
96
291
14
844
176
112
294
742
889
249
531
620
31
528
1
718
541
510
18
904
BubbleSort - 0 steps
568
981
993
441
88
892
984
742
392
829
444
914
683
556
663
425
344
895
369
449
470
876
224
254
661
451
750
468
659
90
764
448
582
448
240
462
475
253
115
609
989
756
330
413
685
110
861
963
846
481
182
388
213
913
99
343
376
273
439
187
618
548
797
160
575
708
949
899
266
447
127
135
604
491
368
766
193
367
686
625
63
741
933
142
563
511
939
255
323
621
385
749
209
958
769
81
537
329
458
322
InsertionSort - 0 steps
637
746
219
138
255
867
462
993
830
210
740
676
509
2
214
520
715
539
352
364
965
656
408
802
758
899
440
859
292
762
22
270
335
341
495
264
221
132
139
208
868
786
768
633
81
793
549
554
421
932
149
156
732
713
98
448
101
156
242
418
110
121
207
431
997
208
649
691
835
415
501
463
527
105
82
478
566
848
277
601
435
85
829
192
441
35
136
105
928
217
965
884
254
66
754
160
365
912
67
658
ShellSort - 0 steps
143
72
831
681
674
899
345
118
854
257
702
700
436
651
32
537
754
266
741
367
649
942
395
580
571
401
402
639
666
703
66
937
21
677
688
328
69
190
300
242
49
121
218
950
207
312
680
94
219
801
426
700
906
550
354
417
701
596
790
581
188
18
89
437
581
949
572
20
554
787
926
186
50
70
123
728
691
396
339
483
498
31
124
879
271
846
825
388
362
202
19
786
809
33
293
130
526
160
170
474
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