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.

348
602
945
778
562
955
379
586
331
50
289
980
438
214
860
82
764
394
666
992
236
475
20
551
692
628
442
505
196
985
769
255
594
240
307
459
906
736
373
590
746
204
773
800
103
604
321
190
154
266
56
836
575
777
946
65
10
298
281
171
142
708
594
546
914
298
385
522
492
459
434
1000
262
948
985
348
639
868
36
673
781
366
243
450
284
358
109
529
357
768
475
779
893
318
878
124
788
16
98
572
BubbleSort - 0 steps
897
908
138
172
443
863
511
219
248
238
913
784
461
710
806
423
420
53
333
433
663
934
428
664
606
246
236
928
404
885
516
55
263
383
215
386
767
71
935
882
353
809
196
823
317
76
574
794
629
817
178
233
73
536
81
803
746
77
54
807
327
52
511
105
207
945
568
426
471
923
675
534
256
277
975
660
330
385
685
761
228
458
234
818
44
818
641
317
232
435
45
274
502
891
944
570
266
812
532
453
InsertionSort - 0 steps
625
599
288
74
171
535
909
282
821
971
192
63
623
524
41
735
749
555
248
598
501
978
313
216
51
990
235
282
594
465
960
221
811
894
488
66
576
803
778
115
296
2
81
285
323
531
910
105
501
392
466
451
110
373
99
457
243
932
193
131
938
336
455
565
584
469
392
292
39
597
735
681
381
227
382
415
28
606
373
44
460
319
295
379
536
109
332
74
568
829
722
437
3
786
31
915
118
70
534
311
ShellSort - 0 steps
527
301
908
913
71
128
708
883
744
163
474
227
827
309
108
761
150
251
644
957
834
203
596
39
429
976
787
218
622
385
215
952
728
393
999
162
647
987
133
74
23
323
33
186
37
281
442
944
557
660
587
588
553
184
700
304
653
457
483
11
669
632
395
401
933
264
344
54
809
512
39
78
995
307
930
773
334
741
460
164
376
18
126
267
286
276
958
468
920
332
282
577
60
196
105
46
616
302
435
837
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