Sitemap
Jon Schulman

Jon Schulman

Experienced regulatory attorney, passionate advocate of good design, casual space enthusiast.

Sorting Algorithms

6 min readSep 13, 2018

--

Press enter or click to view image in full size

As far as Javascript is concerned, the Sort( ) function is somewhat ambiguous. Javascript itself does not care how sorting happens, typically leaving this ‘decision’ in the hands of the javascript engine. This means that Sort( ) can function differently on different browsers. After I discovered this I decided to take a deeper look at Sort( ), and found myself deep inside the soup of computer science and mathematics that is Sorting Algorithms. In this post I’ll be talking about three sorting algorithms: the radix sort, the bubble sort, and the quicksort.

Radix Sort

One of the earliest sorting algorithms dates back to the 1880s and is attributed to a man named Herman Hollerith. Hollerith should be recognized in his capacity as a civil servant working for the U.S. Census Bureau, where the equipment and technologies he invented helped allow the 1890 census to be processed in just 6 years. That may not seem like a big deal, but the 1880 census had taken 8 years to complete, and that was with nearly 13 million fewer citizens to count. It was on the success of his tabulating machines that Hollerith founded a company which eventually became the cornerstone of something called the International Business Machines Corporation. You may have heard of it.

That first sorting algorithm was the Radix sort. In a traditional radix sort, a set of numbers are progressively reordered beginning with the digits in the ones position. Once the data has been rearranged, the sort then moves to the Tens positions, then the Hundreds, etc.

Press enter or click to view image in full size

The word radix actually derives from the Latin word for “root,” and you can see that reflected in how this sort algorithm begins at the bottom (or root) of each value and works its way up. Note that when the processes is flipped, it is referred to as a most significant digit (MSD) radix sort.

The radix sort itself is closely tied to its original use in electromechanical tabulating machines, and looking at the drawing above you can almost see the rotors of Hollerith’s 19th century device whirring away as they sort through millions of pieces of data. That said, in 1954 Harold Seward devised an improved style of radix sort to be run on a digital computer.

Stable and Not Stable

Now is probably a good time to talk about stability in sorting methods. It is important to understand the difference between sorts that are “stable” and sorts that are “not stable,” where “stability” ensures that elements of identical values will maintain their relative position to one another throughout the execution of a sort. Probably the best way to visualize this is to think about sorting a deck of cards. Below we can see that the 5 of ❤s and 5 of ♠s maintain their position relative to each other (e.g., with ❤ on the left) in a stable sort, but may not in an unstable sort. When evaluating sorting methods, it is vital to consider whether a stable sort is needed for your application. A radix sort, for what it’s worth, is a stable sorting method.

Bubble Sort

Like the radix sort the Bubble sort is stable. Though very simple, the bubble sort (invented in 1956), is relatively inefficient because it requires repeated passes over every element in a set of data until the entire set is sorted.

This type of sort begins by evaluating the very first value against the second. If the first is greater than the second, the values are flipped. The algorithm then compares the second and third values. This pattern is repeated until the end of the dataset is reached, and the process begins again. Given its simplicity, the bubble sort’s strength is the minimal code/instructions needed in its execution.

Get Jon Schulman’s stories in your inbox

Join Medium for free to get updates from this writer.

One of the big problems with a bubble sort is that if the data is completely backwards, the number of steps needed to sort all of it to completion is roughly the the number of pieces of data squared. While this worst-case scenario is true of a lot of common sorting algorithms, a small bubble sort will take much longer than a small merge sort. But wait, what’s a merge sort…?

Merge Sort

John von Neumann is a fascinating historical figure of immeasurable intelligence. He was prolific in his written work, publishing 150 papers over his lifetime, and was instrumental in the development of the explosive lenses used to the create implosion-triggered fusion reactions that were at the heart of the first nuclear weapons.

Oh, and in 1945 he invented the Merge sort. In a merge sort, a set of numbers is first divided in half, then in half again, then in half again, and so forth until only pairs of numbers are left. Each pair is ordered, then ordered pairs are “merged” into each other.

Unlike bubble sorts, merge sorts become “faster” over time as sets of numbers become internally sorted. In the chart below, the blue line represents the theoretical number of steps (e.g., time/effort/operations) to complete a bubble sort for a given number of elements, while the red line reflects the number of steps to complete a merge sort of the same size. Note the these are only averages, as the degree to which the data is unsorted to begin with will greatly impact the efficacy of most sort methods.

Quicksort

So far we been talking about stable sorting methods, but I was curious to see what a not stable sorting method looks like. Enter the Quicksort, created in 1959 by Tony Hoare. (You can also thank him for the existence of null values, his “billion-dollar mistake” according to his own assessment of the matter).

Press enter or click to view image in full size

Like all sorting algorithms, quicksort has its pluses and minuses. Quicksort, like a bubble sort, can be very slow in the worst-case scenario. However, on average and in best-cases, quicksorts tend to be very fast in completing their work. A quicksort starts by selecting a single element called the “pivot.” There are different strategies to selecting a pivot depending on the goals of the sort, and I wont be getting into that here. Suffice to say, the pivot represents the first point of division for this sorting method.

Once a pivot is selected, the elements are recorded so that values less than the pivot are to the left of the pivot, and values greater than the pivot are to the right. The pivot is then considered locked in place, a new pivot is selected, and the process runs again until all elements have been sorted.

Conclusion

The math and science behind sorting algorithms can intense, and for smaller datasets worrying about how Javascript is going to handle a sort operation is probably not a great use of time. However, as datasets grow in size selecting an appropriate sorting method can substantial effects on the performance of your code, and having a basic understanding of how they work is sure to make you a better developer.

--

--

Jon Schulman
Jon Schulman

Experienced regulatory attorney, passionate advocate of good design, casual space enthusiast.