AlgorithmsCodilityPatternsTutorials

Let’s sort some stuff out, because sorting things is a great way to develop analytical thinking, as well as expanding your coding skills. Most of the time, you will want to use the built in methods in your language of choice, providing your own predicate (a function that returns a boolean) if needed.

But understanding how each of these approaches work can be really rewarding in terms of devising your own algorithms based on them. As a neat visual followup to this post, open this website and take a look at the pseudocode implementations and animations .

Bubble sort

One of the first sorting techniques you will learn operates on a simple paradigm that if you compare two adjacent elements and switch their position based on their value, lower to the left, higher to the right, you will eventually, after enough iterations, end up with a sorted array. I know, this does not sound to efficient, but due to the shear simplicity of the algorithm it can still sometimes be a viable choice if you have nothing else available i guess.

Bubble sort operates in O(n2) time complexity, making it a bad choice for larger arrays. Still it has O(1) space complexity so if you find the case where the application of this algorithm is the best choice, please comment 🙂

Selection sort is similar, in terms of performance and being a stable sort. Selection sort is a child’s algorithm where you simply traverse the list until you find the largest element, and append it to the sorted list. The only application of these two algorithms is when for some reason, you can not use the default library’s sorting algorithm. Even then, insertion sort is a better choice, but is slightly more difficult to implement.

Merge sort

Merge sort has an interesting concept. By dividing the array into smaller arrays recursively, until there is only one item in the array left, we can merge the arrays while sorting them while we do it.

This approach is so popular that many js engines use it as the default sorting algorithm. Spidermonkey(Mozzilla) and JavascriptCore(Safari) use merge sort on their Array.prototype.sort() method. So let’s see how it works.

Worst case time complexity is O(n log n) and the space complexity is always O(n). So when you need a stable O(n log n) sorting algorithm, this is about your only option. The Downside is the auxiliary space needed, but that rarely poses a problem.

 

Insertion sort

Another interesting approach with two for loops. Not very “performant”, but pretty convenient in some cases. The best defense for this statement is the fact that V8 uses it to sort arrays smaller than 20 something items. A little digression here: Performant is a word that was made up by software developers to describe software that performs well, in whatever way you want to define performance. It is a word that is not in the dictionary yet, but I think it should be. –Stack overflow 🙂

The time complexity is O(n2) and space complexity is O(1) meaning the algorithm is not very performant on large lists due to many shifting that needs to be done to insert the item on the spot. The common use case is when you have a small list, as this algorithm has a very small constant and is a stable sort.

 

Next, take a look at some more complex algorithms.