Skip to main content

Command Palette

Search for a command to run...

Algorithm Complexity

The idea behind making your program more efficient.

Updated
โ€ข4 min readโ€ขView as Markdown
Algorithm Complexity

When you run a program, do you ever wonder why it's taking so long to run? Or perhaps you see your current program takes up half of the RAM on your computer? The problem might have to deal with Algorithm Complexity.

What is an Algorithm?

To start, an algorithm is a step-by-step approach of precise instruction to solve a specific problem. It is important to note that it is independent of any programming language. You can represent algorithms by using pseudocode, flow chart, structured chart, etc.

Let's start with an example. Let's say that you want to create an algorithm that helps adds up the sum of two numbers. I will be using a flow chart and pseudocode to demonstrate.

flow chart:

flow chart example.png

pseudocode:

pusedocode.png

Being able to solve the problem before jumping into programming helps with logical errors. A logical error is when you don't get the desired result from your program. An example of this is if I were to add 2 + 2 and expect 4, but instead, I got 6. When you write your algorithm, it will be easier to find where it went wrong. It's not a common skill most self-learned programmer have. But it's a skill that I recommend programmers learn.

What is Algorithm Complexity?

Algorithm Complexity is the measure of time and space requirement of an execution of code.Let's us look at an example of three different algorithm that solves the same problem. The problem being to display the sum of positive integers up to n.

The First Algorithm

https://cdn.hashnode.com/res/hashnode/image/upload/v1627167062683/awkhz0jY4.png

Explanation

We see here it is the simplest solution to get the end result that we want. The algorithms is using a for loop to iterate from 1 to n. The idea of the algorithm is:

1 + 2 + 3 + 4 + 5 + ... + n

The Second Algorithm

https://cdn.hashnode.com/res/hashnode/image/upload/v1627252845693/zAARTqCaF.png

Explanation

It is the same solution just like the first algorithm but instead there are two for loops in the algorithm. The idea of the algorithm is the summation of n

summation.png

The Third Algorithm

algorithmC.png

Explanation

It's like the other two and instead uses the summation to calculate the sum of n. Algorithm 3 is the fastest of the three algorithm

So, how would you choose which of the three algorithms to solve the problem of displaying the sum of positive integers up to n?

That depends on what you are looking for in a given algorithm. Which of the three criteria is important to you? Is it the speed, simplicity, or efficiency algorithm? When talking about the efficiency of an algorithm, it's important to choose the most efficient one.

Measuring Algorithm Complexity

Measuring the efficiency of an algorithm can be broken down into two categories: Time and Space complexity.

  • Time complexity focuses on the time needed to execute the program from the given algorithm.

Example: When you are waiting at a restaurant for your food and let's say the food comes out in an hour because of a poor design in management in the kitchen, you might be furious. Compared to when you get it in ten minutes because of good management in the kitchen.

  • Space complexity focuses on how much memory the algorithm uses up when the program runs.

Example: Let's say you are putting away clothes, and you shove all of them into the closet without organizing them. Yes, it might take less time now to put away your clothes. However, you are going to spend more time looking for particular clothes and have less closet space. Compared to when you take the time to organize your clothes. The process might take longer. But in the end, you have more space in your closet.

In the end, you will usually choose time or space complexity to help make a good algorithm. In the next article in the series, I will be taking a deeper dive into how time complexity works in something called the Big O notation.

If you want more information on the resources I used when I took data structure in university here is the link .

I hope this helps give you a basic idea of how algorithm complexity works! See you on the next article.

L
Layla5y ago

This was INSANELY explanatory -- you rock, Vy :D

1
T

Thank you Layla <3! I try to be better than our computer science professors XD

P

Hi Thuy, for me it was the topic sentence in your article that stood out the most, it was enticing enough to pique my interests. Welldone!

1
T

Thank you so much! I'm glad my topic sentence helped you with your interest in DS! : )

1
P

Thuy Vy Tran Sure thing, keep going!

H

I found this very helpful and I loved the closet analogy - looking forward to the next article in the series! ๐Ÿ˜„

1
T

Thank you Hunter! I used the closet analogy because it's easier to give an illustration on how space complexity works!

1
Y

Your content is appreciated... Wait for more ๐Ÿ˜

1
T

Thank you so much! I'm glad the article is useful.

Data Structure and Algorithms

Part 1 of 1

There are many different algorithm and data structure in the world of Computer Science. Get ready to learn a new algorithm for your next tech interview or programming project.

More from this blog