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.

877
538
389
654
426
144
573
939
285
928
646
493
264
576
143
305
627
582
676
429
346
204
880
12
982
807
414
800
152
272
982
752
53
642
214
109
672
683
954
553
906
215
166
717
629
625
182
90
452
572
484
970
764
318
13
190
883
171
845
878
136
363
396
868
440
106
199
768
130
645
115
492
774
652
127
271
401
843
602
34
672
440
958
156
21
290
327
406
738
311
871
359
15
646
132
799
335
879
969
699
BubbleSort - 0 steps
368
866
570
943
407
9
498
597
348
299
117
768
469
597
623
180
616
153
561
426
468
51
341
918
916
772
672
638
240
470
690
606
585
75
262
470
122
103
548
775
268
547
215
640
759
202
473
886
580
359
303
944
234
285
648
287
91
374
635
251
823
170
934
744
358
472
965
165
876
185
82
597
335
401
27
944
81
792
635
147
398
623
105
800
14
415
593
4
373
916
217
599
529
916
371
635
59
494
780
494
InsertionSort - 0 steps
489
294
923
910
424
414
78
448
747
29
271
22
442
464
881
20
915
123
703
826
879
415
514
678
271
894
907
246
751
682
690
296
439
846
683
570
215
448
943
621
655
886
728
558
561
964
630
677
457
39
742
42
154
736
796
467
24
362
555
112
103
745
944
85
856
271
289
593
284
21
945
245
404
173
297
980
622
333
220
974
789
916
427
997
792
769
93
966
324
916
308
361
401
563
41
190
586
309
15
466
ShellSort - 0 steps
109
584
406
62
381
705
675
800
943
388
433
840
305
617
527
167
647
929
306
37
87
858
567
563
287
29
63
901
350
717
688
141
763
534
527
955
598
955
21
533
161
238
416
587
494
111
799
418
36
207
641
946
928
832
788
44
913
200
984
57
781
849
768
790
845
116
754
385
657
952
587
927
232
612
358
451
801
873
407
670
211
971
507
860
360
749
992
737
343
954
439
283
388
358
783
656
567
276
187
347
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