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.

507
831
741
554
75
778
793
385
899
455
383
554
364
664
194
484
166
110
538
968
174
316
574
563
476
381
969
943
157
132
139
20
833
45
983
46
489
82
149
376
156
49
962
971
292
673
207
470
560
363
677
172
1
124
351
500
604
661
519
264
229
670
978
280
981
326
993
454
463
26
81
770
96
764
301
502
551
533
428
763
427
683
693
13
732
383
850
544
869
563
495
681
844
500
799
383
164
721
504
219
BubbleSort - 0 steps
591
749
871
378
94
112
832
897
452
304
111
662
824
603
562
675
949
191
616
31
376
355
966
819
958
138
677
750
817
237
110
113
272
212
962
473
775
846
67
354
278
926
425
385
923
746
670
640
860
208
450
680
389
675
220
634
869
36
215
795
488
735
593
63
988
925
790
160
218
463
933
287
239
61
750
49
565
430
59
890
238
513
605
408
975
205
15
548
980
564
917
225
152
187
335
800
222
629
304
668
InsertionSort - 0 steps
874
641
253
258
491
725
27
166
565
313
46
267
98
554
283
983
175
936
133
495
649
747
39
501
581
984
809
256
804
700
716
275
884
999
291
804
926
819
477
752
507
433
323
946
680
365
514
866
854
687
837
117
44
798
579
404
228
294
554
262
581
220
994
880
126
990
132
434
757
200
90
57
773
184
142
194
544
506
627
855
331
654
614
456
526
634
881
295
832
379
188
776
654
971
673
231
483
20
368
987
ShellSort - 0 steps
137
343
7
880
110
409
730
504
835
614
782
438
560
175
627
394
56
425
736
403
841
541
375
531
166
531
101
960
570
930
625
372
918
283
537
360
568
588
700
112
181
37
348
252
892
200
285
750
581
678
57
107
541
461
232
621
962
945
307
785
464
753
277
249
753
120
900
369
234
809
695
553
348
59
166
688
576
395
558
1000
64
986
938
784
370
523
972
458
855
167
808
874
813
284
83
857
788
637
206
550
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