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.

831
678
108
63
93
551
863
464
943
749
632
211
211
154
408
117
505
432
955
25
64
645
186
468
991
835
262
588
288
14
390
286
394
156
937
301
319
578
911
135
12
538
282
233
165
579
904
978
951
954
373
569
740
479
607
859
68
120
470
387
140
610
868
227
335
418
993
684
871
234
875
262
775
769
944
585
195
98
449
364
965
585
414
933
322
306
865
693
397
760
951
668
653
323
173
111
267
784
800
652
BubbleSort - 0 steps
263
827
473
671
853
269
800
859
783
442
933
455
345
783
373
374
286
271
783
856
916
888
474
818
295
374
739
28
682
381
286
535
833
497
590
48
180
856
196
538
976
420
457
102
71
212
609
987
469
894
571
322
48
83
678
871
253
462
299
178
453
321
355
563
259
262
431
453
76
510
544
916
352
944
334
35
565
59
290
960
305
594
824
648
447
716
421
391
633
673
419
441
360
592
272
759
688
74
749
946
InsertionSort - 0 steps
183
87
337
508
31
900
492
907
445
862
730
631
999
243
337
47
558
349
971
71
574
454
290
872
451
401
440
990
479
10
41
948
8
154
482
562
108
852
509
688
316
384
626
69
10
661
57
541
694
905
639
628
508
716
376
502
835
325
777
39
647
445
536
634
154
824
359
932
486
640
984
380
610
796
379
100
263
913
930
951
785
15
384
294
350
708
481
240
51
899
681
978
384
488
19
236
622
782
674
949
ShellSort - 0 steps
762
886
689
527
664
640
831
124
234
449
886
195
656
101
834
168
895
542
532
968
67
370
648
504
217
379
912
875
797
735
867
264
723
814
519
57
147
429
591
954
522
467
526
841
601
370
607
847
585
334
822
703
719
197
44
828
924
4
289
764
582
410
888
161
475
686
595
18
250
362
140
964
678
207
517
661
192
868
570
106
934
548
531
89
289
628
863
325
510
252
431
826
580
440
605
489
568
36
112
320
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