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.

965
178
604
763
461
200
393
381
386
366
444
94
827
148
42
47
343
957
576
685
846
204
748
343
352
583
366
742
630
280
20
862
809
2
375
206
783
535
11
780
170
141
575
124
141
363
82
380
694
526
529
849
591
270
961
915
383
573
694
529
983
156
428
356
470
157
835
394
38
511
562
797
107
413
409
749
973
160
610
327
928
903
439
867
328
574
878
152
43
86
660
466
665
924
240
463
466
415
612
338
BubbleSort - 0 steps
500
870
644
68
724
128
58
653
400
350
841
737
675
625
536
369
786
681
171
195
742
171
651
706
197
897
771
8
511
417
711
841
973
50
750
641
26
946
368
636
268
250
452
318
12
736
700
598
345
628
321
899
684
431
933
667
638
456
120
625
462
537
145
255
255
555
235
425
481
838
124
938
950
572
545
444
680
690
54
38
247
285
134
234
544
581
56
356
841
201
301
331
542
894
308
506
253
261
643
80
InsertionSort - 0 steps
64
365
911
327
845
660
898
524
325
626
990
56
205
385
128
243
451
561
704
147
131
575
37
625
507
551
408
969
355
251
689
828
615
988
323
283
907
918
689
832
338
143
352
264
654
442
190
583
43
466
849
114
879
264
607
458
727
484
850
241
488
930
104
448
767
298
809
459
32
198
865
608
51
150
880
510
282
434
75
812
889
482
398
964
258
322
205
583
359
547
239
186
294
68
715
764
291
541
32
235
ShellSort - 0 steps
841
886
393
258
440
361
953
37
948
166
318
383
117
880
247
910
187
944
149
300
101
478
159
236
843
292
878
333
950
950
130
341
656
429
829
773
881
120
226
22
230
889
495
318
474
741
358
388
642
684
346
696
807
690
137
219
699
419
606
129
785
350
359
346
71
691
797
873
874
905
186
903
40
970
986
120
261
397
568
205
982
747
987
933
432
493
225
594
159
448
218
614
307
122
353
750
708
462
963
137
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