# Algorithm

An algorithm refers to a set of instructions used in problem-solving. It can be a simple procedure for finding the sum of two numbers, or it can be a complex operation for compressing video files.

In computer programming, an algorithm tells computers what to do. It is written in a programming language that computers can understand and follow. Often, these algorithms are written as functions, which large programs use for reference. For instance, a photo editing program contains various instructions for processing image data. These algorithms enable the program to resize, blur, or sharpen photos.

Algorithms aren’t new. After all, they are just mathematical instructions. In 1952, Alan Turing, who is celebrated as the father of modern computing, published algorithms to discover patterns in nature.

## Why Do We Need Algorithms?

Algorithms are everywhere, enhancing various areas of human lives. These instructions power your gadgets, enabling devices to recognize your face, respond to voice commands, or sort files. They also power apps and social media networks. Take Facebook, for example—it uses a complex algorithm to determine the content you would see in your News Feed every day.

However, it’s not just Facebook that uses algorithms. Other tech giants, like Google, Netflix, TikTok, and Amazon, also use these to personalize your experience. Algorithms can make everyday tasks faster and simpler to perform. They can also make it easier for you to sort through large data sets to find crucial information quickly.

Here are a few real-world applications of algorithms:

• Self-driving cars
• Search engines
• Automation software
• Social media
• Video compression

## How Do Computer Algorithms Work?

Not all step-by-step procedures can be called algorithms.

Algorithms function using input and output values. They gather input, then apply these values at each step to generate a specific output. A search engine, for instance, may use search queries as input values. They scour their database for relevant information. Then, it delivers output values as search results.

A good algorithm must contain the following characteristics:

• Precise

For a computer to understand the algorithm, it must be highly detailed. This is why programming can be very time-consuming and costly.

In traditional programming, algorithms must have specific and unambiguous instructions. The programmer must specify the steps for turning inputs into outputs. Every step must lead to a definite result.

However, there are a few exceptions. When it comes to machine learning, which uses some of the most complex algorithms, the computer discovers its own algorithms. The programmer initially feeds it with raw data. Then, it’s up to the program to discover how to move from point A to point B.

• Input

A sound algorithm must contain 0 or more well-defined input values.

• Output

Every algorithm produces at least one well-defined output, which matches the desired results.

• Finite

An algorithm contains a finite number of instructions. After all the steps are executed, it will stop. It may stop when an expected output has been achieved or when no other solution is possible.

• Feasible

A good algorithm must be able to perform tasks using available resources without errors. It should eliminate unnecessary or redundant steps that could make the process fail.

• Efficient

An algorithm shouldn’t take up too much space or time. Most of the time, however, there is a tradeoff between space and time.

• Independent

The step-by-step instructions of algorithms aren’t dependent on any programming code. That means you should be able to understand the algorithms even without programming knowledge. It also means that you can run the algorithms on any programming language.

## Types of Algorithms

Algorithms can have different types of control structures. You can classify them into three categories based on these structures.

• Sequence

As its name suggests, this type of algorithm uses a sequence of steps that lead to a desired output. Each step is designed to be performed one after the other.

• Branching

The “if-then” problems represent the branch control structure. If, say, an algorithm is true, it will lead to output A. If it is false, its output will be B. This structure is also dubbed the selection type.

• Loop

The loop structure allows repetition under specific conditions. It entails terms like “for” and “while.” Even though the process can be repeated, it will eventually end after a number of loops. This structure is often called the repetition type.