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.

654
276
97
553
356
656
608
526
266
775
302
454
180
869
714
388
719
795
286
569
513
786
180
199
697
233
213
150
428
438
708
515
367
624
509
231
784
456
17
498
750
998
651
515
665
958
995
883
52
494
7
739
34
126
655
654
59
262
230
236
478
56
348
880
188
681
995
413
365
699
352
477
554
633
614
733
48
461
180
308
525
391
677
910
269
103
561
24
441
356
372
734
537
396
899
736
17
881
592
937
BubbleSort - 0 steps
900
499
136
624
48
931
325
82
122
843
939
423
919
283
878
442
471
931
401
327
105
570
801
820
745
624
294
571
545
192
330
864
196
303
276
292
997
576
574
642
131
588
198
583
52
800
809
692
283
48
664
600
334
301
289
965
655
756
353
474
467
669
9
20
584
258
348
829
589
281
64
34
901
944
524
473
659
235
563
866
745
985
138
869
392
369
824
686
188
911
994
538
139
465
651
923
401
764
988
506
InsertionSort - 0 steps
892
318
937
16
937
492
585
183
174
154
690
371
750
732
938
773
40
998
889
583
640
997
794
819
298
98
867
583
859
310
449
546
525
272
39
290
600
312
804
458
918
989
483
685
282
849
616
264
232
200
554
309
268
678
754
197
236
300
835
307
447
576
318
126
780
367
11
876
821
166
340
873
219
18
443
841
949
89
160
586
280
688
654
25
791
913
113
438
727
740
237
570
732
711
367
95
594
947
390
193
ShellSort - 0 steps
511
694
104
174
389
781
131
23
2
250
699
685
450
60
276
160
742
70
763
67
988
623
863
610
571
178
255
95
83
329
275
435
334
865
15
930
143
369
750
722
192
590
108
179
388
523
305
205
285
785
566
733
432
564
176
879
122
330
787
597
359
206
6
756
637
297
96
846
477
998
799
655
190
828
650
391
485
902
883
806
848
906
72
337
533
855
636
491
529
527
738
573
888
178
252
567
42
388
353
379
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