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.

955
881
566
75
245
984
37
348
997
626
673
336
951
820
352
132
433
708
911
659
85
224
805
117
844
713
922
323
447
585
374
994
764
698
514
135
943
998
587
478
848
312
683
718
315
282
160
892
166
548
31
250
816
844
996
941
903
424
678
723
930
135
28
916
907
805
121
968
879
720
328
161
316
215
207
381
845
996
764
599
164
204
56
891
219
544
288
634
185
652
687
451
655
872
445
771
822
962
449
352
BubbleSort - 0 steps
880
308
383
208
641
191
804
351
528
697
585
507
607
338
44
899
880
2
650
573
331
384
957
690
425
663
120
742
747
436
334
64
552
167
269
620
467
512
618
58
315
595
85
41
782
384
481
822
509
973
146
74
463
719
494
236
818
232
362
350
871
79
605
974
26
814
853
489
181
462
720
367
273
299
352
50
918
956
666
779
543
399
198
927
486
546
965
248
909
238
347
930
673
296
299
140
108
51
261
643
InsertionSort - 0 steps
619
991
110
302
205
687
622
478
900
294
634
899
278
403
102
698
370
364
102
235
21
420
274
841
350
467
642
285
182
143
765
531
449
47
122
290
554
852
512
457
373
164
69
222
716
791
525
394
366
974
9
858
434
663
978
130
604
20
463
746
449
993
854
278
146
786
806
15
325
908
617
225
492
715
33
782
466
156
401
163
369
88
459
943
914
117
297
148
365
337
207
499
132
579
395
952
701
495
669
768
ShellSort - 0 steps
818
584
538
195
547
346
998
728
131
414
317
914
698
412
576
585
652
935
912
489
880
887
400
55
246
714
393
836
56
116
691
750
283
76
167
906
261
396
112
216
736
146
614
247
786
849
242
551
371
172
54
164
47
261
209
319
570
252
986
112
448
20
825
970
971
142
704
589
725
478
531
559
901
764
937
935
923
343
750
844
940
691
218
957
690
309
363
17
426
869
935
974
321
304
193
576
277
442
914
443
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