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.
583 | |
624 | |
335 | |
989 | |
627 | |
514 | |
551 | |
809 | |
175 | |
346 | |
799 | |
191 | |
242 | |
318 | |
757 | |
462 | |
134 | |
461 | |
259 | |
524 | |
583 | |
527 | |
613 | |
174 | |
59 | |
933 | |
5 | |
70 | |
236 | |
774 | |
73 | |
336 | |
74 | |
768 | |
727 | |
367 | |
544 | |
576 | |
796 | |
133 | |
848 | |
363 | |
330 | |
552 | |
281 | |
882 | |
421 | |
725 | |
77 | |
475 | |
328 | |
268 | |
349 | |
337 | |
861 | |
506 | |
932 | |
456 | |
818 | |
934 | |
84 | |
284 | |
519 | |
927 | |
267 | |
438 | |
4 | |
28 | |
257 | |
547 | |
94 | |
213 | |
887 | |
46 | |
448 | |
26 | |
53 | |
742 | |
613 | |
581 | |
664 | |
229 | |
559 | |
319 | |
508 | |
546 | |
541 | |
855 | |
515 | |
905 | |
146 | |
413 | |
320 | |
13 | |
949 | |
962 | |
362 | |
355 | |
419 | |
577 |
968 | |
651 | |
534 | |
612 | |
457 | |
258 | |
277 | |
323 | |
681 | |
306 | |
435 | |
42 | |
147 | |
661 | |
86 | |
658 | |
533 | |
427 | |
728 | |
81 | |
42 | |
54 | |
387 | |
185 | |
437 | |
352 | |
905 | |
693 | |
120 | |
422 | |
59 | |
907 | |
582 | |
761 | |
229 | |
730 | |
404 | |
106 | |
479 | |
65 | |
940 | |
185 | |
252 | |
118 | |
661 | |
955 | |
678 | |
846 | |
680 | |
793 | |
515 | |
600 | |
728 | |
827 | |
497 | |
626 | |
912 | |
276 | |
842 | |
343 | |
813 | |
505 | |
1000 | |
458 | |
542 | |
130 | |
639 | |
736 | |
539 | |
270 | |
56 | |
406 | |
816 | |
59 | |
946 | |
771 | |
570 | |
916 | |
159 | |
780 | |
774 | |
757 | |
930 | |
824 | |
113 | |
894 | |
375 | |
208 | |
325 | |
35 | |
967 | |
134 | |
468 | |
890 | |
42 | |
756 | |
186 | |
391 | |
905 | |
401 |
211 | |
317 | |
465 | |
104 | |
53 | |
570 | |
797 | |
744 | |
566 | |
452 | |
461 | |
319 | |
156 | |
121 | |
359 | |
836 | |
212 | |
941 | |
668 | |
812 | |
832 | |
864 | |
413 | |
325 | |
718 | |
81 | |
409 | |
862 | |
201 | |
846 | |
770 | |
110 | |
857 | |
169 | |
780 | |
177 | |
365 | |
162 | |
447 | |
743 | |
992 | |
950 | |
668 | |
152 | |
980 | |
143 | |
532 | |
659 | |
118 | |
375 | |
551 | |
614 | |
111 | |
251 | |
58 | |
736 | |
175 | |
371 | |
874 | |
938 | |
701 | |
326 | |
729 | |
464 | |
215 | |
240 | |
997 | |
239 | |
700 | |
216 | |
447 | |
713 | |
99 | |
856 | |
669 | |
342 | |
724 | |
890 | |
908 | |
873 | |
609 | |
872 | |
840 | |
253 | |
554 | |
57 | |
556 | |
223 | |
612 | |
835 | |
855 | |
839 | |
186 | |
433 | |
705 | |
440 | |
197 | |
626 | |
716 | |
793 |
293 | |
880 | |
279 | |
593 | |
572 | |
238 | |
842 | |
269 | |
251 | |
499 | |
601 | |
141 | |
360 | |
157 | |
201 | |
741 | |
930 | |
900 | |
224 | |
513 | |
979 | |
937 | |
230 | |
833 | |
515 | |
674 | |
205 | |
959 | |
167 | |
44 | |
484 | |
670 | |
215 | |
701 | |
509 | |
781 | |
579 | |
376 | |
645 | |
818 | |
467 | |
915 | |
698 | |
981 | |
245 | |
568 | |
704 | |
962 | |
77 | |
996 | |
508 | |
369 | |
461 | |
552 | |
431 | |
428 | |
228 | |
651 | |
401 | |
811 | |
875 | |
732 | |
116 | |
838 | |
290 | |
982 | |
171 | |
654 | |
772 | |
238 | |
460 | |
23 | |
64 | |
375 | |
33 | |
228 | |
273 | |
745 | |
414 | |
161 | |
379 | |
442 | |
219 | |
445 | |
298 | |
776 | |
479 | |
963 | |
388 | |
10 | |
885 | |
174 | |
828 | |
245 | |
61 | |
318 | |
804 | |
873 | |
875 | |
589 |
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.
Related Articles - Sorting Algorithms
- JavaScript DHTML Insertion Sort
- JavaScript DHTML Shell Sort
- JavaScript DHTML Quick Sort
- JavaScript DHTML Bubble Sort
- JavaScript Sorting Algorithm Comparison
- JavaScript DHTML Sorting Using OOP - Example 1
- JavaScript DHTML Sorting Algorithms
- PHP Sorting Arrays of Arrays
- PHP Sorting SimpleXMLElement Object arrays