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.

821
223
209
423
381
33
564
10
132
807
642
317
644
192
413
207
365
415
188
522
622
522
760
258
432
698
103
131
291
132
512
212
671
609
218
437
910
547
804
195
148
927
248
825
777
684
862
715
430
721
950
357
309
375
470
175
676
161
215
169
320
384
468
950
938
495
104
897
47
666
440
145
358
966
127
117
659
159
701
186
234
110
794
638
294
645
355
717
724
127
454
80
821
263
380
974
540
904
49
263
BubbleSort - 0 steps
379
30
887
929
747
395
574
431
356
594
223
834
829
383
897
922
439
231
646
611
294
940
927
543
333
862
258
19
37
440
146
890
487
916
401
740
676
909
772
4
146
144
365
222
290
321
24
768
277
331
552
934
695
944
390
812
884
273
962
316
269
592
307
899
205
402
893
791
27
231
384
642
363
520
573
184
972
279
24
658
538
55
152
102
458
861
755
241
300
28
550
856
573
87
42
541
15
866
918
451
InsertionSort - 0 steps
719
385
199
150
457
165
420
737
803
938
624
627
618
229
363
516
510
118
59
701
220
458
323
62
821
760
241
126
325
48
163
283
286
409
148
504
633
254
526
243
300
661
753
12
884
701
861
58
452
1
684
245
357
101
632
425
889
267
604
990
933
279
304
310
523
289
444
247
56
770
114
869
802
318
154
647
123
115
241
307
222
623
623
602
430
836
316
221
783
682
735
530
618
998
243
352
881
181
448
687
ShellSort - 0 steps
647
826
83
710
586
849
507
275
908
888
787
496
101
333
729
452
410
148
147
777
246
356
541
624
225
235
50
374
307
238
539
869
637
689
429
762
201
811
581
694
679
753
861
547
164
725
414
918
33
794
768
368
544
959
235
178
722
821
720
976
654
585
528
140
718
338
887
622
175
985
442
482
118
14
356
551
806
360
228
724
982
788
947
361
581
268
924
739
287
639
662
462
340
600
559
597
583
582
29
781
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