What is Algorithm? Properties and Real-life Applications

Before the invention of computers, there were algorithms. Now computers are everywhere, so algorithms are everywhere! Algorithms lie at the heart of computing. If we observe our surroundings, we can find several algorithms working to solve our daily life problems: Social media networks, GPS applications, Google search, e-commerce platforms, Netflix recommendation systems, etc. All of these applications are powered by high performance algorithms. On other side, algorithmic problem solving techniques are also popular for coding interviews to get a high-paying job in the software industry. In simple words: learning algorithms is one of the critical career skills for programmers!

Critical ideas to think about!

Definition of an Algorithm

An algorithm is a well-defined step-by-step procedure to transform a given input into the desired output to solve a computational problem. In other words, an algorithm is a tool for solving a well-specified computational problem. Note: Computational problem is a collection of questions that computers might be able to solve. For example, the problem of sorting is a computational problem.

Example: Linear search

Given an array A[] of n elements, write an algorithm to search for a given element k in A[]. If k is present, return the index where it is present; otherwise, return -1.

Extracting all relevant details from the problem:

Input: X[] = [ 5, 8, - 3, - 4, 13, 9, - 17, 7, 12], k = 13 Output: 4 Explanation: Element 13 is present at index 4. Input: X[] = [ 5, 8, - 3, - 4, 13, 9, - 17, 7, 12], k = 11 Output: -1 Explanation: Element 11 is not present in X[].

Linear search idea

The value k can be present at any index in the array because we don’t know the input distribution. So a simple strategy would be:

Linear search pseudocode

int linearSearch(int X[], int n, int k)  for(int i = 0; i  n; i = i + 1)  if(X[i] == k) return i > return -1 >

Critical ideas to remember!

Always ask the following questions related to input for every algorithmic problem:

Properties of a good algorithm

A good algorithm must be correct, efficient, finite, and easy to implement.

  1. 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.
  2. 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.
  3. 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.
  4. 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!
  5. Finiteness: A good algorithm must terminate after a finite set of steps. We should avoid infinite recursion or infinite loop conditions.
  6. Simplicity and elegance: An algorithm should be easy to understand and implement.

Why analysing efficiency is important?

Suppose computers were infinitely fast and computer memory was free. Would you have any reason to study algorithms? But the reality is that computers may be fast but not infinitely fast, and memory may be inexpensive but not free. So, running time and space are essential resources for defining the performance of a computer program. Think!

In computer science, several factors are crucial for an algorithm's performance, including code correctness, functionality, user-friendliness, modularity, scalability, security, and maintainability, as well as the programmer's time. The critical question is: why do we analyze the performance of an algorithm? Here are a few important reasons:

Suppose we would like to run two different sorting algorithms on two different computers A and B, where computer B is 1000 times slower than computer A. To compare their performances, we run the slower sorting algorithm Insertion sort on faster computer A and run the faster sorting algorithm Merge sort on slower computer B.

What differences do we observe? Still, computer B is taking much less time than computer A, if the input size is large. This gap will increase further if we increase the input size. This would be one of the reasons for learning algorithms and their efficiency. Think!

An algorithm is a technology!

The performance of a system depends on choosing efficient algorithms as much as on choosing fast hardware. Even applications that do not require algorithms directly at the application level rely heavily on algorithms. For example:

Overall, algorithms are at the core of almost all computer applications. Just as rapid innovations are being made in other computer technologies, they are also being made in algorithms!

Critical ideas to think!

Real-life applications of algorithms and data structures

Application of array data structure

Application of linked list data structure

Application of stack data structure

Application of queue data structure

Application of hashing in data structure

Application of tree data structure

Application of graph data structure

Application of dynamic programming

Application of greedy algorithms

Application of backtracking

Application of number theory

Other applications of algorithms and data structures

Coding problems to warm-up in algorithms

Content references

If you have any queries/doubts/feedback, please write us at contact@enjoyalgorithms.com. Enjoy learning, Enjoy system design, Enjoy algorithms!