Algorithms revisited

As a developer exposed in software development for 3 years now, I got curious in revisiting theoretical topics when I was in college. And one of them is about algorithms. Since I didn’t pay attention before, I’m going to make it my life’s mission to get a hold of basic concepts and fundamentals of algorithms properly to apply it in my dev life.

Algorithms play a big role in software development and how we code every business logic flow. It’s like one of the keys or centers of programming wherein it determines everything, including maintainability, scalability, performance. Without proper knowledge of how an algorithm works, you’re going to make bigger mistakes in your career and do it in a wrong way without knowing.

Now, if you ask yourself, what’s the use of learning algorithms right from the very beginning if most of the core concepts were already embedded or existing right in front of every programming language we use? The answer lies in performance and maintainability.

Code Maintainability

How do you make the code maintainable? How do you write elegant code with fewer errors? Surely you might have wondered that while you’re starting your career and elevating your skills in software development. Perhaps by simply studying design patterns I can write maintainable code? Both yes and no. Think about it. How can you implement proper design patterns if you lack the knowledge of algorithms? If anything, you’ll be confused whether to implement it or not or is it really maintainable.

Algorithms are about procedures. Remember, it’s defined as a step-by-step procedure. Whenever you study design patterns, the concept of the algorithm is still there. But if you have a deeper knowledge of algorithms perhaps you can improve the pattern and improve performance as well.

Performance

This is the main forte of algorithms. I heard most programmers say that performance depends on the programming language they’re using. Actually, they’re right. But sad to say it’s just half of the story.

Let’s say you used C++ in your application and the other dev used C# in his app. In runtime, C++ wins by default since it’s closer to machine language than C# which uses C++ as its GC and compiler.

But let’s just say you implemented poor algorithms, algorithms which run O(n) or worse, runs O(n²) all the time. Chances are, C++ are not going to help you achieve the best performance you desire since you implemented algorithms poorly.

If you’re not aware of structures such as hashtables and sorting algorithms. Or how to determine the speed of each algorithm, chances are you’re going to make your application slower and will end up writing slower algorithms. You may not know you’re running.

It’s not language-specific

One positive point of each algorithm implementation are this. It’s not language-specific. When you know how to write algorithms the proper way, you can do it like a breeze. But most of the time it’s already implemented. But it helps to know which algorithm performs fast or slow.

I have here a sample of quicksort algorithm written in Python, Haskell and JavaScript:

Quicksort algorithms in different programming languages

Noticed that no matter what language do we use, the important thing is, we were able to achieve the procedures of quicksort algorithm and express it no matter what language it is. Now that’s something. Not only were you able to grasp the basic concept of algorithms, you were also able to transfer it from one language to another effortlessly. But that’s not only the point of learning algorithms, it also improves your thinking of improving the code flow and maintainability in my own opinion.

But it’s already implemented

I agree. For the most part, you’re not going to write sorting algorithms or hash algorithms all over again for yourself. In fact, you may need to reuse them. But without proper knowledge of algorithms, how do you expect to know the ups and downs of each function already implemented by the language?

I used to have a mistake that every implementation performs the same. I thought using List and Dictionary will yield same performance. Now I realized that’s not the case. In fact, I don’t really have an idea what Dictionary does anyway in C# since what I know is it stores data. That’s all. I just realized how dumb that is.

Now I know generic List<T> performs poorly than using Dictionary<TKey, TValue> in terms of searching in C#. When you search in a List, it usually has an average time of O(n) or linear time since the search has to iterate in each collection, Dictionary outperforms the performance by whooping O(1) time or constant time when searching since we just need to search it by its key and we’ll be given a value instantaneously. Notice the big difference in the performance. How was I able to determine that? It’s because I have basic knowledge of algorithms.

Conclusion

Algorithms are one of the most basic things we needed to learn if we wanted to improve ourselves as a software developer. It enables us to think more critically of the existing algorithms we use in day-to-day work and write efficient code.

In the long run, our knowledge in algorithms is transferrable. Not only we were able to apply this in different programming languages, but we can also use this in other areas and fields of our lives. Like, how can we be more productive in our work, how to finish a repetitive task in just a short span of time. Things like that. It tackles real-life situation and challenges as well. With algorithms, we were able to manage our actions more effectively and think efficiently to save time. And this also affects our decision-making as well.

Everyone didn’t know that our daily lives were operating in algorithms. Everything is interconnected. We just don’t realize it.