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.

258
285
14
750
401
581
218
7
765
400
182
808
905
576
262
157
75
893
127
540
716
993
823
76
742
322
214
908
193
710
178
518
781
890
771
139
309
818
453
173
243
199
59
910
727
505
495
207
364
507
808
746
691
96
817
526
698
678
592
898
746
266
875
812
55
828
100
455
460
938
87
353
807
451
7
826
179
648
839
444
730
17
62
783
603
214
866
200
923
395
278
842
124
627
450
629
774
214
637
783
BubbleSort - 0 steps
420
21
253
284
614
703
486
638
314
358
75
659
469
456
647
468
777
229
555
33
464
148
236
937
980
320
166
534
197
681
565
162
702
647
460
29
799
260
855
945
461
507
365
586
62
480
399
860
956
602
205
5
18
283
468
671
886
535
951
274
293
856
392
959
805
408
582
62
665
900
993
490
742
592
936
765
952
275
106
164
477
592
420
488
422
72
311
234
793
70
183
926
989
2
713
217
690
488
328
967
InsertionSort - 0 steps
291
833
397
458
798
752
27
286
819
691
668
694
587
925
277
378
646
625
699
404
987
66
315
690
491
71
42
166
805
264
350
587
487
495
249
323
352
378
790
675
46
877
515
565
37
563
783
608
460
409
902
375
225
173
291
816
805
265
862
740
436
401
797
533
942
186
356
682
272
33
842
408
351
280
382
969
259
878
785
562
570
204
217
315
413
699
515
500
987
960
165
645
299
898
127
615
513
681
271
811
ShellSort - 0 steps
266
944
100
867
185
63
432
461
760
35
143
483
111
544
51
143
214
422
106
988
166
609
281
12
767
838
400
80
905
361
414
860
310
374
912
262
32
440
324
198
26
508
145
73
335
15
616
21
187
126
648
865
448
264
272
20
964
859
693
632
101
400
585
200
287
546
994
258
856
480
783
145
231
268
983
301
670
722
509
286
233
472
717
420
851
917
840
717
963
709
924
420
790
891
646
298
201
411
774
578
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