When going through the coding interview process, the topic of sorting algorithms arise regularly. Which begs the question of: why are sorting algorithms important to understand as a developer?
I remember back when I started taking computer science classes at Texas Tech. Now you have to know that I taught myself programming. So I essentially took a backwards approach to learning computer sciences. Which means that I learned how to build real world applications first, and THEN I got my bachelor’s in computer science.
My approach to learning caused me to have some issues with sorting algorithms. My main point of contention was that they didn’t seem very practical. The professor spent weeks going over various sorting algorithms and all I could think was that I could accomplish the same result by simply calling the
sort() method. Which would only require a single line of code.
Practical vs Theoretical
At first I thought the issue was in the realm of the age old debate of practical vs theoretical learning. This is something you come across quite regularly in discussions around traditional vs skill based education.
So my initial reaction was that I only needed to learn enough to pass the class and move on with my development life. However, during the course of the semester my entire perspective changed and I realized that my first reaction was naive.
The Real Reason
So why are sorting algorithms important? And why are they the most consistently asked questions during coding interviews? The real reason is subtle and you may need to discover it for yourself. Your reaction to this guide may be the same that mine was when I attended my first algorithm class.
I may have gone my entire life with the same negative mindset towards the study of sorting algorithms if it wouldn’t have been for one of my professors. After suffering through an 1.5 hour lecture on sorting algorithm analysis I met with the professor. I asked her if there was any practical reason for studying sorting algorithms.
She knew my background and that I had been developing applications for a number of years. So she challenged me with a homework assignment that I’ll never forget.
The class was mainly theoretical, so all of the homework up to that time had revolved around writing pseudocode and not actual programs. She told me to spend the rest of the week building functional programs that implemented the algorithms that we had been studying.
Honestly I thought the assignment would be a breeze. I’d been building production applications for years, so how hard could it be to build a few basic sorting algorithms? It turns out, shockingly hard!
I spent the full remainder of the week building out a handful of sorting algorithms in Ruby And I struggled through each one!
A Change of Heart
What I discovered in my algorithm assignment was that creating sorting modules forced me to work through a wide variety of language components. Most of which I had rarely used in regular applications.
For example, to implement the
Quicksort algorithm I had to write code that included:
- How to partition an array.
- Swapping array values.
- Performing recursion, which means that the method calls itself.
- Implementing a randomize method to select a pivot value.
- And a number of other tasks.
I had used each of those types of features, in some form or another, in previous projects. However this was the first time I had to combine the full set of components into a single method and have them work together seamlessly. And that was simply for a single algorithm!
By the end of the assignment I discovered that I had learned more about the language that I was working with than I had in years.
Why are Sorting Algorithms Important?
That is the top reason why it’s important to understand sorting algorithms. There’s not really a reason for implementing your own sorting algorithm from scratch for a production application. However in the course of learning algorithms you will discover advanced facets of a language along with programming design patterns that will give you an edge as a developer.
In addition to combining multiple language components, sorting algorithms also help you understand program accuracy and speed.
Just like in any type of program, sorting algorithms are not valid unless they are accurate. Imagine if you had an array such as this:
And your sorting algorithm almost worked, like this:
This type of behavior would make the entire algorithm useless. Sorting algorithms give an abstract way of studying program accuracy. You don’t have to worry about other development tasks, such as system configuration or working with dependencies. Instead you are able to simply focus on your data input and outputs.
Lastly, you can’t have a discussion about sorting algorithms without mentioning performance. This component is vital to understanding why it’s important to understand sorting algorithms, and how the study will help improve your skill as a developer.
Let’s think back to the early days of Twitter. Twitter was originally a Ruby on Rails application. And the basic functionality of sending a Tweet and viewing other user’s tweets is the same today as it was when they launched. However if you remember the early days of the application you’ll be know that the site went down constantly.
There were a number of reasons for the poor performance of Twitter early on. However the main issue was that it was not built with scalability in mind. This meant that it worked perfectly fine for a small number of users, however with tens of millions of users and Tweets, the system would crash dozens of times a day.
Building Scalability with Algorithms
The story of Twitter reminded me of the difference between algorithms.
Are all sorting algorithms created equal? Not even close. For example, if you use the
Bubble Sort algorithm on a collection of 300k integers it will take over 9 minutes. However if you use the
Quicksort algorithm it will sort the same collection of 300k integers in less than a second. This type of performance variance can only be understood when you get under the hood and truly understand how sorting algorithms work.
I hope that this has been a helpful guide for answering the question: why are sorting algorithms important to understand as a developer? In the show notes I’ve included links to some tutorials on sorting algorithms if you want to extend your knowledge on the subject.