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.

954
584
434
372
804
243
193
122
74
59
208
927
807
167
521
26
924
663
292
563
448
849
22
964
521
48
189
919
146
512
308
900
673
382
928
80
946
383
498
542
192
840
838
484
707
861
327
600
878
241
913
345
258
97
394
619
499
473
47
605
291
991
970
422
395
745
427
211
94
332
790
504
754
183
128
292
288
966
50
418
638
288
703
700
112
307
615
206
15
590
349
591
488
138
424
787
732
133
549
254
BubbleSort - 0 steps
453
351
853
323
890
924
33
709
282
339
580
980
944
757
787
565
486
777
475
55
624
62
124
275
884
918
57
299
370
922
582
965
231
417
25
138
568
977
806
454
772
381
287
579
429
228
329
65
663
556
564
834
292
994
822
798
93
104
422
278
587
170
561
297
744
575
232
977
390
499
866
901
852
591
315
126
375
440
444
236
510
57
896
621
397
44
247
981
766
845
841
418
981
245
824
865
723
915
578
321
InsertionSort - 0 steps
442
94
113
991
623
468
893
76
963
683
406
649
541
80
175
130
429
489
118
553
24
15
355
137
770
313
28
615
264
181
987
932
511
848
487
873
971
703
681
56
396
477
705
650
779
901
162
588
31
643
330
351
421
375
867
989
942
281
33
862
479
67
234
702
842
603
504
659
116
375
236
623
263
26
195
83
975
130
676
739
887
434
892
630
615
334
801
43
822
597
431
900
494
464
123
345
94
146
355
969
ShellSort - 0 steps
397
781
264
233
105
622
293
814
51
659
952
625
280
785
285
990
446
301
746
350
793
739
101
473
986
59
829
199
619
325
322
722
543
433
666
365
285
358
580
105
861
905
30
611
611
880
865
617
953
464
503
126
717
787
64
296
973
485
45
319
220
117
474
153
979
459
568
370
277
402
24
538
613
895
378
602
557
329
344
142
540
175
259
626
254
932
477
875
79
547
693
357
331
179
608
111
341
743
114
103
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