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.

629
754
449
84
587
989
308
340
231
117
73
371
519
999
327
874
488
628
255
687
464
825
200
649
174
994
957
651
825
245
631
602
566
144
176
759
374
96
210
556
611
700
356
646
726
341
432
882
373
556
502
652
853
428
207
428
690
152
304
11
69
223
462
632
28
145
1
246
425
278
486
271
140
368
541
428
202
979
153
99
162
71
892
338
539
594
322
206
21
852
498
911
408
334
506
246
335
683
359
366
BubbleSort - 0 steps
856
61
235
295
330
903
145
225
75
595
261
339
117
463
881
851
377
175
509
588
379
592
580
543
712
663
706
867
887
281
649
661
860
122
964
856
299
999
624
312
498
858
601
140
42
676
143
682
95
569
908
968
940
755
659
891
245
82
151
910
425
186
530
218
325
192
531
983
450
655
301
855
423
234
406
111
308
229
247
957
572
796
567
624
920
48
462
800
451
305
829
56
62
543
432
841
46
769
831
183
InsertionSort - 0 steps
669
119
360
425
298
526
512
515
279
757
966
515
971
941
92
722
932
666
551
883
359
843
711
260
257
882
355
926
150
814
570
794
509
557
109
448
32
135
693
269
935
916
486
469
753
77
244
294
517
369
112
253
865
35
917
512
90
327
639
138
56
833
850
966
656
274
167
331
736
197
424
890
2
340
873
152
285
976
814
265
259
434
571
971
592
371
46
565
534
44
87
218
690
23
867
127
196
657
325
640
ShellSort - 0 steps
262
621
11
986
163
23
398
675
734
330
762
233
775
410
776
565
238
382
605
878
101
261
354
750
64
407
941
364
188
751
572
352
330
622
984
681
166
406
280
857
744
847
662
411
176
703
211
1
858
439
416
339
159
736
905
280
449
492
961
316
984
178
138
238
619
543
764
839
744
294
302
482
942
580
190
697
909
850
310
555
671
253
455
805
596
388
597
946
30
755
962
184
295
968
913
222
777
75
712
926
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