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.

803
312
180
938
195
489
932
949
106
699
246
14
593
908
1000
613
914
35
27
62
521
915
101
49
167
107
974
342
671
505
163
729
956
672
826
652
173
295
987
122
971
297
799
929
221
757
972
131
816
138
213
883
860
878
162
523
241
210
830
901
238
295
133
268
308
265
522
658
685
966
872
703
738
481
26
237
43
247
233
42
176
440
49
918
663
901
44
493
305
669
82
718
208
763
120
534
940
717
966
90
BubbleSort - 0 steps
427
183
733
879
367
403
938
651
558
144
864
873
283
34
141
568
660
462
378
470
275
600
837
844
19
679
87
22
106
191
300
170
644
789
931
602
337
479
143
332
246
205
609
176
996
515
387
193
222
514
864
356
281
808
577
947
900
335
174
932
795
175
900
463
847
361
366
630
342
422
198
201
435
717
322
887
675
932
272
844
331
104
743
986
827
867
823
177
522
435
70
161
937
344
678
230
515
267
520
681
InsertionSort - 0 steps
592
391
959
587
44
633
279
242
577
884
532
448
336
885
588
982
803
463
761
13
954
346
542
184
870
323
847
652
872
867
92
453
628
92
837
256
915
524
432
998
408
741
862
433
688
419
220
426
155
129
336
26
415
26
789
583
193
216
33
222
33
881
569
827
531
997
304
765
214
212
382
731
625
35
963
508
153
167
525
846
1
672
951
171
949
119
567
603
236
296
829
116
265
19
300
274
942
376
546
728
ShellSort - 0 steps
484
587
350
381
902
157
462
649
127
294
252
122
313
427
618
936
569
76
644
502
882
18
645
425
725
683
804
116
123
94
609
710
491
879
263
270
627
694
63
50
641
904
75
334
882
80
463
824
320
506
815
606
965
246
227
772
720
981
365
317
112
306
836
80
814
682
774
449
171
168
242
285
850
423
38
788
138
335
814
138
783
225
411
450
830
490
291
808
947
586
483
430
888
354
479
581
3
917
351
794
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