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.

475
465
279
318
120
315
412
335
533
122
499
718
436
78
314
194
807
302
372
443
881
18
288
797
107
433
155
506
784
981
768
389
246
925
715
705
354
526
749
350
749
688
513
790
935
92
692
753
666
147
137
677
648
393
611
932
759
68
338
761
596
752
374
333
639
687
561
303
164
321
633
156
650
564
259
596
821
282
912
818
349
790
441
112
323
508
511
151
574
208
94
289
313
24
714
166
562
966
882
712
BubbleSort - 0 steps
398
708
797
158
435
175
97
410
380
830
946
362
444
200
800
835
277
433
724
893
918
250
507
65
775
333
839
154
146
304
265
367
387
115
663
375
893
589
872
634
380
686
705
146
539
281
769
339
652
458
122
613
845
935
210
765
889
521
697
54
93
335
322
680
392
528
739
555
100
934
903
894
362
554
256
261
29
72
966
197
20
936
92
881
754
997
63
200
491
121
897
849
637
598
633
303
55
672
142
803
InsertionSort - 0 steps
509
869
661
105
677
272
564
195
437
150
407
285
958
640
346
297
402
900
760
355
969
267
30
769
330
558
727
610
635
517
950
722
258
462
950
677
332
22
284
366
830
76
677
513
68
962
695
474
862
71
472
661
334
184
608
183
742
192
105
222
481
864
111
804
883
576
222
397
697
186
427
327
164
234
866
570
941
61
521
269
698
980
315
907
497
363
499
846
797
335
124
174
645
983
569
29
277
553
322
47
ShellSort - 0 steps
370
356
50
43
290
507
680
794
816
15
367
614
44
438
259
42
644
768
751
407
262
335
648
761
481
247
965
608
200
470
100
202
121
893
764
644
304
634
582
990
245
225
663
974
940
27
432
479
306
358
415
954
54
630
781
18
564
239
65
377
121
194
591
572
820
409
477
732
749
338
810
720
735
496
615
272
419
287
921
818
520
914
879
607
455
521
496
312
25
903
140
127
799
385
454
832
129
653
269
778
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