Understanding algorithm efficiency and why it’s important

Humzah Choudry
2 min readDec 5, 2017

--

When you first begin programming, algorithms can sound intimidating. You think of algorithms requiring a whiteboard, some PhD students, several hours and an impossible problem. You soon find out that’s not the case. Algorithms are solved by programmers every day.

An important part of solving algorithms is efficiency. As your dataset grows so will the time it takes to run functions on it. Understanding the efficiency of an algorithm is important for growth. As programmers we code with the future in mind and to do that, efficiency is key.

Creating efficient algorithms is about reducing the amount of iterations needed to complete your task in relation to the size of the dataset. BigO notation is used to describe time complexity. Here is a chart that can help visualize bigO runtime.

Note here that there can be a huge descrepincy in the amount of “Operations” (or iterations) as the elements increase, hence why it is important to create efficient algorithms.

A really great example is hashes vs arrays. We are all familiar with both data structures. The time complexity for each respectively is O(1) vs O(n). Let’s talk about why.

First lets look at an array. If we are searching for an element in an array, what is the maximum amount of elements we would have to look through in order to find our value? The answer is the length of the array, or n. If we have an array of [1, 4, 6, 10, 5] and we are looking for 6, how many elements do we search through to find the value? Whats the worst case scenario?

Why do you think hashes have a search time complexity of O(1)? A hash uses keys to lookup the value in which you are searching for. Lets say it’s a function “find_value(key)”. Everytime you look for a value, it will only have to run one time to find the answer. So the time complexity is O(1).

Compare these two time complexities on the chart. As you can see the hash will perform better as the dataset grows.

Understanding bigO runtime and being able to explain complexity is important for engineers.

Some general tips: Try to stay away from embedded loops, meaning loops inside of loops. Remember that this is meant to be within reason. There are plenty of cases where an embedded loop is a reasonable solution. Overusing variables can also weigh down runtime, if you don’t need it don’t use it. Sandwich coding is an example of this. Hashes over arrays. You can often use smaller datatypes to save memory bytes before integers, integers before floats. All of these tips will help increase the efficiency of your programs!

--

--