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.

87
460
579
7
6
302
368
38
513
986
780
693
44
483
275
528
917
514
199
601
529
704
574
141
340
989
586
64
697
635
216
586
557
690
425
752
974
979
71
846
701
364
133
261
878
570
944
527
875
758
281
759
250
423
759
500
936
688
827
875
80
331
588
9
199
708
393
23
728
199
483
842
624
673
468
141
279
167
63
444
73
847
597
931
651
899
697
700
867
175
815
478
727
65
423
213
353
30
565
770
BubbleSort - 0 steps
698
749
911
873
236
516
848
541
489
164
685
672
639
214
872
924
501
344
719
258
68
566
896
715
950
664
760
954
147
713
796
502
497
323
244
700
194
53
320
98
497
793
338
605
747
304
450
827
432
421
910
44
111
557
319
347
438
825
917
738
225
932
246
946
983
329
553
513
909
772
9
695
265
974
992
536
308
675
404
742
612
384
212
463
209
540
801
659
300
255
23
101
585
321
928
484
263
659
418
118
InsertionSort - 0 steps
684
566
167
579
812
699
907
52
412
863
425
567
821
783
216
391
500
334
665
225
520
249
561
258
519
347
141
828
590
483
239
892
211
840
828
424
895
308
530
700
994
108
784
268
211
915
230
391
263
488
404
514
247
476
356
436
183
351
441
520
147
821
682
879
147
131
359
606
941
933
205
175
10
111
669
42
692
382
229
934
448
377
856
901
164
823
371
597
353
189
600
62
479
474
98
673
362
393
861
539
ShellSort - 0 steps
668
475
798
808
313
260
914
367
530
719
320
736
346
204
742
480
21
482
797
40
365
152
651
221
858
279
491
54
156
441
495
524
11
208
678
396
593
850
187
776
956
460
257
347
402
680
893
921
154
728
663
36
88
203
999
53
636
884
394
797
397
817
338
152
337
571
427
594
557
301
584
917
534
899
286
393
331
57
82
655
114
953
159
288
335
320
717
44
892
950
960
675
206
205
289
810
728
55
673
21
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