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.

157
443
326
773
763
22
190
58
734
420
707
739
557
599
127
592
716
850
709
980
912
254
321
214
1000
487
830
322
461
550
230
714
214
871
717
23
290
829
91
906
808
193
92
490
976
78
785
150
356
817
9
42
852
515
83
965
931
974
478
588
574
145
801
399
884
354
530
779
202
288
794
158
527
26
87
211
677
141
313
252
86
440
586
616
316
651
518
278
543
692
682
812
397
83
262
527
216
326
10
187
BubbleSort - 0 steps
613
926
800
277
92
318
144
416
631
561
994
11
175
36
152
885
115
527
316
304
447
199
396
877
263
542
718
908
566
117
60
259
885
987
104
198
537
437
334
898
201
361
449
576
37
868
123
456
319
190
352
200
787
851
400
25
22
502
437
153
101
188
136
241
502
576
985
929
450
144
633
420
194
183
597
498
528
236
185
201
690
731
917
968
201
905
565
720
131
543
965
606
367
890
964
859
78
389
141
376
InsertionSort - 0 steps
507
198
476
7
107
307
318
257
426
37
303
821
707
754
413
111
99
959
374
366
580
159
885
461
611
135
15
828
760
834
27
70
90
911
834
191
684
844
160
524
226
87
40
903
863
537
115
853
305
736
368
419
416
591
312
558
515
804
125
362
715
760
706
361
641
875
166
692
297
971
26
477
929
649
259
483
707
98
916
8
123
562
468
503
469
143
517
341
889
521
900
318
517
443
733
531
270
230
158
696
ShellSort - 0 steps
645
335
953
179
727
721
165
49
715
139
638
388
609
944
728
615
644
48
110
779
28
43
889
631
178
25
385
781
753
910
432
365
206
592
503
808
164
294
918
385
143
205
919
725
919
632
911
511
25
324
782
765
407
69
773
364
415
921
866
855
555
204
554
12
795
825
969
687
447
741
497
342
81
707
279
232
839
404
398
761
840
938
414
926
233
231
775
477
77
531
173
714
914
238
682
696
876
527
214
609
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