What is an Algorithm?

What is an Algorithm?

The term algorithm is a precise step-by-step method of solving a difficulty or making calculations, usually with the help of a computer.

The order of actions in an algorithm is very important because the result depends on them.

In computer science, algorithms enable computers to solve problems and execute tasks.

They are created as sequences of precise steps that lead to efficient execution.

Characteristics of Algorithm

A well-written algorithm should have the following characteristics:

  • Precise – An algorithm is a sequence of unambiguous steps leading to a result. Each step must be precisely defined, so there is no ambiguity about what has to be implemented.
  • Finite – An algorithm must terminate after a finite number of steps.
  • Effective – An algorithm must produce the result in a finite amount of time and space (memory). It must not go into an infinite loop or run out of computer memory while executing.
  • The algorithm should not depend on any programming language or machine architecture-specific features such as flow control statements (if, switch, goto), loops, etc.

Types of Algorithm

Algorithms are subcategorized in many different ways.

They can be classified according to the following criteria:

1. Recursive and Iterative Algorithms

Recursive algorithms solve problems by breaking them down into smaller versions of themselves.

Each recursive algorithm needs an ending condition that stops the algorithm from calling itself again and again, also known as an infinite loop.

For example, if you wanted to find the factorial of a number, you would stop multiplying numbers by each other once you hit 1.

Iterative algorithms solve problems in steps that repeat until the problem is solved, like a for a loop.

2. Parallel and Distributed Algorithms

Parallel algorithms run multiple calculations at once on one computer.

Distributed algorithms divide their work across multiple computers or networks, allowing greater efficiency and scalability for large workloads.

Examples include peer-to-peer networks like BitTorrent and high-performance computing like [email protected] (the search for extraterrestrial intelligence).

You may have noticed that these solutions use computer clusters to achieve their goals, meaning they’re built from a network of connected computers.

As you might imagine, random access memory (RAM) becomes important when dealing with so much information at once.

An Algorithm Can Tell You When To Do Something

This is where the “algorithm” part comes in.

An algorithm guides you on how to solve a problem. It tells you what to do and when to do it, which means the above algorithm could be written out like this:

  1. If the light is green, go straight.
  2. If there’s a car coming from your left, stop and let it pass.
  3. If there’s no car coming from your left, go straight!
  4. If the light is yellow, slow down but keep driving straight.
  5. Don’t stop unless you’re close to the intersection.
  6. If the light is red, stop and wait for the light to turn green again.

The Same Algorithm Can Be Applied To More Than One Problem

Let’s take a look at another example of an algorithm. You slow down when you’re driving on the highway and pass a slow-moving car.

When you notice that the road ahead is clear, you resume your previous speed. In this case, the algorithm is:

  • If the car in front of me is going slower than I am, I will slow down to match its speed.
  • If there are no cars in front of me (or they are all going as fast or faster than I am), I will drive as quickly as possible within the speed limit/my comfort level/traffic conditions/local laws allow.

You can apply this same algorithm to many other situations: riding a bike, walking on a crowded sidewalk, driving in heavy rain or snow, etc.

Algorithms are Powerful Tools For Solving Problems

Algorithms are great for solving problems, and you’re likely using one right now.

Here’s an example: Let’s say you have a box of socks and need to match them.

How might you go about this? You could start by reaching into the box and grabbing a sock. If it doesn’t have a mate, throw it aside and repeat the process until you find what you’re looking for.

This is an algorithm because it is a step-by-step process that solves a problem (matching the socks).

Your personal experiences can help illustrate the power of algorithms: how often have you used Google Maps to get directions when going on vacation?

That app uses algorithms to let users know which route to take on their trip (and how long it will take them).

If we’re looking at another example in terms of travel, think about airline prices jumping up or down depending on supply and demand; in this case, an algorithm helps determine the price based on available seats and popularity.

By powering apps like Google Maps and helping set airline ticket prices, algorithms make our lives easier every day!

Conclusion

And there you have it. We hope you feel more comfortable understanding and talking about algorithms now that you know how they work.

Join the conversation!

Further Reading