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.

867
84
409
476
203
177
366
299
478
967
714
987
652
309
75
754
896
147
797
95
418
261
556
294
727
116
341
923
38
544
750
261
288
61
639
801
259
48
270
150
143
332
790
749
287
754
561
262
528
817
123
892
911
242
441
954
403
796
427
878
483
699
727
391
950
910
37
198
307
722
553
646
419
844
802
725
694
418
374
675
628
592
866
811
36
11
137
919
255
943
336
713
915
293
242
879
601
778
831
32
BubbleSort - 0 steps
602
877
430
427
466
467
450
5
407
536
724
24
359
461
871
940
400
646
364
847
202
910
268
390
415
362
377
654
416
366
102
628
285
289
243
783
830
521
185
364
486
88
33
931
921
899
147
550
703
203
877
826
302
710
984
784
481
841
965
756
746
165
398
895
689
758
441
550
284
949
634
985
102
494
369
787
393
85
471
954
961
4
206
494
37
883
881
542
740
87
83
787
125
858
149
240
510
650
986
700
InsertionSort - 0 steps
243
276
573
775
316
608
556
345
381
204
469
58
391
985
631
147
157
398
850
456
865
644
528
258
362
517
937
68
523
433
582
252
463
149
432
940
156
754
462
197
126
589
170
275
318
172
507
898
734
850
951
229
750
287
555
638
118
206
272
265
827
680
349
940
31
286
86
967
349
352
498
593
874
412
302
772
932
961
278
888
452
217
330
733
173
461
439
102
167
312
467
401
245
107
150
251
67
265
399
637
ShellSort - 0 steps
188
904
999
465
629
471
94
206
620
473
463
846
385
702
217
980
976
845
46
322
26
134
885
35
677
65
529
181
364
386
325
388
114
937
847
401
157
540
28
284
310
632
432
981
228
998
782
607
1000
456
209
169
293
461
457
325
322
346
80
255
205
832
105
936
659
704
187
686
869
334
637
814
200
578
682
739
214
248
770
333
285
589
41
249
926
695
997
878
235
834
709
128
566
750
250
358
984
240
489
806
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