What is an algorithm?
An algorithm is a procedure used for solving a problem or performing a computation. Algorithms act as an exact list of instructions that conduct specified actions step by step in either hardware- or software-based routines.
Algorithms are widely used throughout all areas of IT. In mathematics and computer science, an algorithm usually refers to a small procedure that solves a recurrent problem. Algorithms are also used as specifications for performing data processing and play a major role in automated systems.
An algorithm could be used for sorting sets of numbers or for more complicated tasks, like recommending user content on social media. Algorithms typically start with initial input and instructions that describe a specific computation. When the computation is executed, the process produces an output.
How do algorithms work?
An algorithm is a coded formula written into software that, when triggered, prompts the tech to take relevant action to solve a problem. Computer algorithms work via input and output. When data is entered, the system analyses the information given and executes the correct commands to produce the desired result. For example, a search algorithm responds to our search query by working to retrieve the relevant information stored within the data structure.
Algorithms can be expressed as natural languages, programming languages, pseudocode, flowcharts and control tables. Natural language expressions are rare, as they are more ambiguous. Programming languages are normally used for expressing algorithms executed by a computer.
There are three constructs to an algorithm.
- Linear sequence: The algorithm progresses through tasks or statements, one after the other.
- Conditional: The algorithm makes a decision between two courses of action, based on the conditions set, i.e. if X is equal to 10 then do Y.
- Loop: The algorithm is made up of a sequence of statements that are repeated a number of times.
![](https://cds.santechz.com/userfiles/media/uploaded/ljzyexj0.png)
There are several types of algorithms, all designed to accomplish different tasks. For example, algorithms perform the following:
- Search engine algorithm. This algorithm takes search stringsof keywords and operators as input, searches its associated database for relevant webpages and returns results.
- Encryption algorithm. This computing algorithm transforms data according to specified actions to protect it. A symmetric key algorithm, such as the Data Encryption Standard, for example, uses the same keyto encrypt and decrypt data. As long as the algorithm is sufficiently sophisticated, no one lacking the key can decrypt the data.
- Greedy algorithm. This algorithm solves optimization problems by finding the locally optimal solution, hoping it is the optimal solution at the global level. However, it does not guarantee the most optimal solution.
- Recursive algorithm. This algorithm calls itself repeatedly until it solves a problem. Recursive algorithms call themselves with a smaller value every time a recursive function is invoked.
- Backtracking algorithm. This algorithm finds a solution to a given problem in incremental approaches and solves it one piece at a time.
- Divide-and-conquer algorithm. This common algorithm is divided into two parts. One part divides a problem into smaller subproblems. The second part solves these problems and then combines them together to produce a solution.
- Dynamic programming algorithm. This algorithm solves problems by dividing them into subproblems. The results are then stored to be applied for future corresponding problems.
- Brute-force algorithm. This algorithm iterates all possible solutions to a problem blindly, searching for one or more solutions to a function.
- Sorting algorithm. Sorting algorithms are used to rearrange data structure based on a comparison operator, which is used to decide a new order for data.
- Hashing algorithm. This algorithm takes data and converts it into a uniform message with a hashing
- Randomized algorithm. This algorithm reduces running times and time-based complexities. It uses random elements as part of its logic.
Properties of a good algorithm
A good algorithm must be correct, efficient, finite, and easy to implement.
Clearly specified input: We need to know all relevant details, such as input data type, input size, data structure, input distribution, etc. Input can be a single value or a set of values.
Clearly specified output: We need to know all relevant details related to the output. Output can be a single value or a set of values.
Correctness: For every set of inputs, our algorithm must halt with the correct output. In other words, our algorithm must be correct. An incorrect algorithm might halt with incorrect answers or not halt at the correct output for some input values.
Efficiency: Running time and memory are bounded resources, and our algorithms must use them wisely. In simple words, our algorithm should be efficient in terms of time and memory!
Finiteness: A good algorithm must terminate after a finite set of steps. We should avoid infinite recursion or infinite loop conditions.
Simplicity and elegance: An algorithm should be easy to understand and implement.
Representing an algorithm
There are two main ways that algorithms can be represented – pseudocode and flowcharts.
Pseudocode
Most programs are developed using programming languages. These languages have specific syntax that must be used so that the program will run properly. Pseudocode is not a programming language, it is a simple method of writing up a set of instructions for a computer program using plain English. This is a good way of planning a program before coding.
Writing in pseudocode is similar to writing in a programming language. Each step of the algorithm is written on a line of its own in sequence. Usually, instructions are written in uppercase, variables in lowercase and messages in sentence case.
In pseudocode, INPUT asks a question. OUTPUT prints a message on screen.
A simple program could be created to ask someone their name and age, and to make a comment based on these. This program represented in pseudocode would look like this:
OUTPUT 'What is your name?'
INPUT user inputs their name
STORE the user's input in the name variable
OUTPUT 'Hello' + name
OUTPUT 'How old are you?'
INPUT user inputs their age
STORE the user's input in the age variable
IF age >= 70 THEN
OUTPUT 'You are aged to perfection!'
ELSE
OUTPUT 'You are a spring chicken!'
In programming, > means ‘greater than’, < means ‘less than’, ≥ means ‘greater than or equal to’ and ≤ means ‘less than or equal to’.
You can also represent an pseudocode in the form of steps.
Flowcharts
A flowchart is a diagram that represents a set of instructions. Flowcharts normally use standard symbols to represent the different instructions. There are few real rules about the level of detail needed in a flowchart. Sometimes flowcharts are broken down into many steps to provide a lot of detail about exactly what is happening. Sometimes they are simplified so that a number of steps occur in just one step.
Flowchart symbols
A simple program could be created to ask someone their name and age, and to make a comment based on these. This program represented as a flowchart would look like this:
Examples of Algorithms
Pseudocode (Algorithm):
Step 1: Initialize X as 0,
Step 2: Increment X by 1,
Step 3: Print X,
Step 4: If X is less than 20 then go back to step 2.
Flowchart:
Pseudocode (Algorithm):
Step 1: Read temperature in Fahrenheit,
Step 2: Calculate temperature with formula C=5/9*(F-32),
Step 3: Print C.
Flowchart:
Algorithm:
Step 1: Input grades of 4 courses M1, M2, M3 and M4,
Step 2: Calculate the average grade with formula "Grade=(M1+M2+M3+M4)/4"
Step 3: If the average grade is less than 60, print "FAIL", else print "PASS".
Flowchart:
Some algorithms for practice
- Write an algorithm which controls a pump which fills water into a tank.
- Write an algorithm for a count down timer.
- Write an algorithm which picks a bus route with the shortest travel time.
- Write an algorithm which allows you automate the process of making a cake.
- Write an algorithm which allows you to find the number of occurences of a word in a paragraph.
- Write an algorithm which allows you to monitor the fuel consumption of a vehicle.