Every developer has asked the question, “What is an Algorithm?” at one time in his career. We use algorithms to implement the various operations in a program, ranging from data processing to automated decision-making. Continue reading to learn the definition and examples of algorithms.
You may have heard this joke before: an algorithm is a word used by developers when they don’t want to explain what they did. Funny, isn’t it?
However, there’s a truth behind the joke, and this lies in the answer to the question: what is an algorithm?
Simply put, an algorithm is a detailed step by step explanation of how to get something done. For example, a recipe is like an algorithm to prepare a certain meal, and if you fail to follow the instructions or steps outlined in a recipe, the outcome will be a culinary disaster.
You may be thinking that algorithms only exist in programming or computer science, this is far from the truth.
As we see from the recipe example, algorithms are not peculiar to only mathematics or programming, even though they are commonly used by developers or techies.
Another example is a music sheet. Music sheets contain detailed steps (ordered), specific notes (instructions), and a finite number of notes to reproduce a musical piece. I think it’s obvious what will happen if one musician in the band misses some steps on the sheet.
What is an Algorithm in Computer Science?
In computer science, an algorithm is a finite sequence of computer implementable instructions or logic that a computer programmer writes to guide the computer in processing a given input to produce output.
A set of computer algorithms is called a Program, or as commonly known Applications.
Computer programs are written in programming languages.
Each application has a specific use, algorithms are used to implement the various operations in the program, ranging from data processing to automated decision making.
Other applications of algorithms include robotics and electronics.
A robot operates and makes decisions based on an algorithm.
An electronic calculator also uses an algorithm to perform the right calculations. We use Algorithms everywhere in computing.
Characteristics of an Algorithm
- Specific Input
The computer has to receive data that will be processed during computation to produce the desired output.
This means that an algorithm has to specify the type and nature of the input, the form and sometimes quantity of input required to kickstart the process to achieve the task for which it is written.
- Specific Output
At the end of every computation process, the output is given by the computer.
Every algorithm should specify the type and nature of the output, the form the output should be, and sometimes the quantity of output expected or even if there will be the output depending on the input provided.
- Definite Sequence
If a recipe for preparing a meal does not specify the sequence of steps to follow, that recipe cannot determine the outcome of the cooking process.
Likewise, an algorithm without a specified sequence of operation is meaningless.
Every algorithm must have a definite progression of step to transform the input into the desired output.
The effectiveness of an algorithm depends on the steps employed to compute data to get the intended output. If the computer cannot implement any of the steps, the algorithm is not effective.
An algorithm must have an end. A computer cannot process one task forever, therefore every algorithm must stop; this means you either get the intended output or a response that no solution is possible.
What is an Algorithm in coding?
Following the definition of algorithms, algorithms in coding are steps or methods employed in solving a particular problem in code, using a programming language.
There are many algorithms used in coding by programmers every day. Here, I will discuss a few commonly used algorithm.
Common algorithms in coding:
You’ve probably seen sort algorithms in action on various e-commerce websites and app stores. An example is sorting by price.
Sorting is the most common and most studied concept in Computer Science.
The idea is to arrange items of a list in a specific order. Many programming languages already have built-in sorting libraries, but it comes in handy if you know how they work.
There are at least seven sorting methods, which are used depending on the sorting requirements. These are :
- Bubble Sort
- Insertion Sort
- Merge Sort
- Quick Sort
- Radix style sorts, Counting Sort and Bucket Sort
- Heap Sort
- Bogo and Quantum Sort
Search engines utilize search algorithms. Searching algorithms are designed to look for an item from any data structure or database where it is stored.
Based on the type of search operation, these algorithms are generally classified into two categories:
Sequential Search: In this, the list or array is traversed sequentially and every element is checked. For example, Linear Search.
Interval Search: These algorithms are specifically designed for searching in sorted data-structures. These type of searching algorithms are much more efficient than Linear Search as they repeatedly target the centre of the search structure and divide the search space in half. For Example, Binary Search.
There are many types of search algorithms. A few of them are :
- Linear Search
- Binary Search
- Jump Search
- Interpolation Search
- Exponential Search
- Sublist Search (Search a linked list in another list)
- Fibonacci Search
- Dynamic Programming
Dynamic programming (DP) is a method for solving a complex problem by breaking it down into simpler subproblems. We solve the subproblems, remember their results and using them we make our way to solve the complex problem, quickly.
Read more about dynamic programming here.
There are many other algorithms that I cannot discuss here for want of space, some of which are listed below:
- String manipulation
- Tree Traversal
- List Traversal
- Hashing Functions
- Scheduling Algorithms
How to write an algorithm
There are many ways to develop an algorithm, but the steps listed below mirror the development process of an algorithm.
Here is an algorithm to write an algorithm:
1: Describe the problem clearly.
2: Analyze the problem.
3: Develop a high-level algorithm.
4: Refine the algorithm by adding more detail.
5: Review the algorithm.
A program is nothing more than a list of instructions, but you can create instructions in various ways to tell the computer how to perform the same task. If you want to give directions to tell someone how to get from the airport to your house, for example, you probably can tell that person two or three different ways. Each way eventually gets the person to your house, but one way may prove easier, another way may prove faster during the day, and the third way may prove more scenic.
By choosing the fastest set of instructions (the algorithm), you can make your program faster and more efficient.