Recently, we’ve been running a skill amnesty. The idea is that people can nominate a few skills they feel they’re lacking in (or not as good at they might hope) and then be matched with the relevant teacher. Classes have included: git, JavaScript, CSS, Python, Comparing Algorithms and more.
I volunteered to teach the Comparing Algorithms class and got asked a challenging first question. The idea of the class is give people a crash course into how to figure out the complexity of a given algorithm. In other words, Big O notation and all that fun stuff.
We were looking at search algorithms – simple ones like linear search and binary search, O(n) vs O(log n) – and someone asked me “how often do you think about these things when you are programming?”. My answer was: “Rarely, if ever”. How often does anybody in their day-to-day programming tasks actually think about these things? I can’t say I’ve gone through a list of numbers and thought about the complexity of my list traversal. I know it’s O(n) but I’m not letting that hold me back on a million integer list.
Almost every reasonable programming language provides fast implementations of every algorithm you can name. If a programming language doesn’t, it’s unlikely anyone will use it. It can take a bit of digging, but you can discover that Python uses a Timsort, which is just a hybrid of insertion sort and merge sort. More digging and we see that JavaScript uses quicksort, but might not be the case. The point is that you shouldn’t really need to worry about what type of sorting algorithm your language of choice uses. Some clever people have already done that for us.
So why bother to learn these things at all? I think the simple answer is in some strange, almost inexplicable way, it helps to know how an algorithm that you probably use every day works under the hood. It helps to know what a linked list, a binary tree, and a heap are, and what such structures might look like in memory. Once you know this information it can be easier to build abstractions out of the programs you’re writing. Nice abstractions lead to nicer code and nicer structure. Programming is as much about moving and transforming data as much as anything else and it’s these algorithms and data structures that do it.
For me, algorithms give you a different way of thinking about computers. They’re elegant, high-level and abstract. You don’t have to worry about semantic and syntactical details. Mastering them, or at least understanding them, can lead to a great understanding about the nature of computers. While you might not endlessly compare sorting algorithms in your head, you’ll certainly appreciate them when you reach for the standard library’s implementation.