Sitemap

Fun with sorting

3 min readSep 29, 2015

Sure you can sort with a single command like .sort(), but what’s the fun in that? Why not work through the confusion of writing multiple lines of code in order to gain a better understanding of sorting logic? There are many sorting algorithms that can take a while to wrap your head around, but working through them can only increase your knowledge of programming. Well, maybe it’ll throw you so far down into the rabbit hole that you’ll curse everything in your life, including that stupid computer sitting in front of you, but if you can manage to claw your way out of that hole, oh what a feeling.

Let’s start with bubble sort. This algorithm compares just two adjacent array elements at a time, starting at index 0 and 1. If index 0 is greater than index 1, the two are switched. But switching positions requires a few steps. First, the value at index 0 has to be temporarily saved to a variable. Next, the value at index 1 is reassigned to the index formerly occupied by the now saved value. Finally, the saved value is reassigned to its new index. And voila, the two values are switched. The focus then shifts to the values at index 1 and index 2, and these comparisons continue until the array is sorted. That wasn’t so bad.

Here’s another, called selection sort. This involves scanning an array and swapping elements based on their value and iteration number. So on the first pass, the lowest value in the array would be sought, and after the entire array was evaluated, the identified lowest element would be swapped with whatever element was held at index 0. The process would continue with the second lowest element being swapped with the element at index 1, and so on and so forth until the entire array was sorted. Booyah.

Get Michael Im’s stories in your inbox

Join Medium for free to get updates from this writer.

If you thought that was awesome, wait until you look at insertion sort. Here, you’re taking an array and splitting it into sorted and unsorted arrays. In the first run, you only have a single element in the sorted array, so sorting is simple…the element from the unsorted array is inserted either before or after the sorted array element. As the sorted array grows, the unsorted elements are inserted in between appropriate elements, the preceeding having a lower value and proceeding having a higher value. Pretty soon, you have a fully sorted array. Oh yeah.

Merge sort! This one continues to split an array into smaller and smaller subarrays until only 1 or 2 elements exist in the subarrays. Those containing a single element are already sorted! Freaking cooool. Subarrays with two elements are simply rearranged, if needed. Then comes merging. Subarrays are compared two at a time, and merging creates an array with sorted elements, so two subarrays each containing two elements would become a single array with four sorted elements. These steps continue until all of the subarrays have been merged to yield a final sorted array. If the array hasn’t been fully sorted after one cycle, the process starts anew until that final, smooth as gravy, sorted array is achieved. Gravy.

The last that I’ll mention is quick sort. This one uses recursion, and implements a pivot element to which all other elements are compared. Elements larger than the pivot are placed to one side of the pivot and those that are smaller are placed on the other. The left and right are then recursively sorted, resulting in a fully sorted array! Hoorah!

There are many visual animations that help to illustrate how each of these algorithms work, and they’re worth checking out. The only part left, for those who are interested, is to write the code to get each of these sorting methods to work.