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.

955
1000
905
129
319
475
615
261
111
552
409
387
613
217
410
800
348
395
275
840
36
732
85
204
940
425
241
123
233
792
396
773
816
627
769
618
395
918
114
786
180
333
498
785
259
687
205
281
659
723
396
804
109
781
732
228
372
735
494
130
368
445
100
5
489
236
324
134
110
70
821
852
527
730
413
26
82
178
685
602
334
638
338
419
489
890
140
162
466
386
634
676
201
390
656
460
350
131
332
145
BubbleSort - 0 steps
791
521
945
319
777
567
782
696
381
937
108
440
13
64
547
950
667
73
973
38
385
290
13
564
97
585
536
520
686
404
290
893
874
494
101
364
902
980
658
87
857
481
788
402
659
397
837
458
159
461
572
519
989
832
867
668
159
913
403
516
200
604
195
538
135
197
281
867
134
438
650
99
913
63
474
600
59
919
977
417
450
594
404
11
89
37
952
512
372
510
331
244
374
946
368
232
102
931
823
17
InsertionSort - 0 steps
841
269
489
386
359
55
19
587
508
780
576
31
297
633
483
841
347
642
969
455
976
124
679
229
135
946
711
707
960
738
579
326
1000
73
703
591
732
941
45
624
781
180
863
135
607
106
276
404
931
957
599
385
553
337
128
650
871
759
758
556
747
824
153
533
462
193
63
590
71
96
351
155
909
429
571
42
835
940
314
435
161
825
207
435
182
235
825
844
545
438
300
176
632
777
744
869
505
891
358
371
ShellSort - 0 steps
862
591
192
587
54
127
409
993
482
448
809
631
355
90
540
242
556
252
761
866
929
261
616
712
167
535
226
877
239
985
379
70
708
778
759
218
711
146
811
903
884
492
722
601
368
628
175
405
862
858
691
216
837
567
887
600
265
346
928
397
742
247
256
161
445
290
747
703
313
685
939
705
231
440
132
15
945
286
639
373
719
938
812
506
943
495
389
198
718
647
755
943
791
678
615
392
65
14
111
945
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