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.

410
825
31
423
250
687
634
403
801
660
251
118
836
807
337
214
915
432
925
143
739
999
280
556
399
847
238
654
774
266
15
297
408
163
768
17
370
616
726
363
859
921
491
450
387
404
943
970
783
591
444
248
113
196
136
163
932
436
481
476
305
437
269
377
745
304
139
981
721
407
949
964
687
259
358
766
443
519
197
274
188
524
857
764
191
14
746
616
602
915
351
618
59
981
628
154
518
924
356
245
BubbleSort - 0 steps
328
349
953
615
110
960
898
196
951
502
9
46
8
498
25
520
337
541
631
704
927
428
809
333
982
831
551
839
422
569
624
380
914
897
721
773
665
644
66
951
296
919
200
379
82
379
871
590
758
232
868
920
108
708
547
258
469
147
807
878
973
576
496
777
134
123
104
199
922
803
226
910
69
817
932
777
562
114
93
686
329
204
711
76
640
718
485
548
213
986
989
318
367
480
305
934
806
248
666
278
InsertionSort - 0 steps
267
947
959
979
452
109
143
389
721
605
996
21
276
63
860
363
625
362
487
453
934
385
142
301
839
617
548
47
172
380
387
737
954
66
555
708
378
337
428
555
472
512
162
164
868
760
915
251
476
858
620
860
764
740
316
39
65
403
484
661
770
977
284
477
640
766
751
338
118
285
518
521
235
273
549
364
103
79
499
320
321
971
969
817
146
727
384
144
858
379
880
721
14
861
809
121
160
5
606
1000
ShellSort - 0 steps
784
827
831
597
665
325
533
91
732
374
906
485
628
607
54
932
70
403
811
775
609
680
246
333
197
535
59
410
315
566
796
635
832
108
611
103
621
396
261
79
479
606
779
43
553
755
510
132
14
831
10
179
286
983
253
314
822
704
350
339
325
599
358
791
286
154
294
380
372
624
167
364
574
709
715
486
533
411
599
181
566
896
760
26
381
999
480
568
876
316
71
926
690
56
873
504
818
811
301
976
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