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.

265
527
399
164
490
580
269
798
982
732
674
560
860
861
14
552
612
509
24
970
341
715
269
307
785
864
810
342
766
827
115
549
409
410
240
892
358
56
375
579
61
13
173
421
155
279
140
999
609
220
86
178
592
117
301
457
737
547
304
151
979
565
930
434
337
548
381
70
49
127
606
853
968
28
306
249
164
911
258
581
26
426
725
852
850
367
357
863
539
395
615
401
463
305
520
462
172
131
461
226
BubbleSort - 0 steps
10
76
840
997
348
99
469
559
886
109
272
680
354
899
657
289
892
436
446
321
838
27
640
418
618
96
506
754
432
261
279
146
52
427
150
278
42
979
287
353
25
141
989
987
221
568
206
545
131
6
413
476
970
57
9
265
417
416
424
691
503
765
715
265
6
90
725
430
485
663
907
530
75
321
690
142
354
425
937
873
762
46
174
379
623
361
489
653
631
194
851
203
640
341
943
785
846
383
377
716
InsertionSort - 0 steps
800
705
931
650
469
324
568
713
631
951
641
966
269
745
882
295
532
56
268
803
739
869
677
420
736
308
82
135
331
84
793
174
515
748
200
236
151
241
398
803
828
33
986
692
659
456
390
470
160
802
386
127
345
877
738
252
68
953
737
742
759
235
347
19
188
537
617
868
936
780
382
333
465
769
250
505
264
966
683
255
961
569
26
784
26
216
619
487
811
951
193
900
22
410
102
339
58
369
337
572
ShellSort - 0 steps
114
583
809
453
555
777
675
252
3
410
65
325
802
709
328
759
829
518
712
570
615
38
861
548
280
881
759
825
315
992
46
264
908
448
805
632
961
604
852
202
57
490
142
996
993
440
362
432
995
878
798
326
395
446
540
515
629
767
314
905
597
148
138
412
33
321
919
245
775
863
589
266
492
883
547
46
951
636
634
442
65
752
767
534
459
985
513
370
298
560
405
258
341
641
242
311
929
64
933
304
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