
An Algorithm is a step-by-step procedure that helps us in solving computational problems. Using the right algorithm to solve a particular kind of problem increases the efficiency of code in terms of space and time complexity.
Space Complexity: Space complexity is the amount of memory space the algorithm is required for giving the desired output. The memory space is consumed by the input data, temporary data, and output data of the algorithm. (alert-success)
Time Complexity: Time complexity is the amount of time required by an algorithm for the execution and providing the desired output. It depends on the number of iterative or recursive steps involve in the execution of the algorithm.
(alert-success)
Sometimes, many get confused between the term Algorithm and Program. An algorithm is a designing part of solving a problem and the Program is the implementation part of solving that problem.
We can write an Algorithm in any language like the English language or by using some mathematical notations. But the program for that algorithm must be written using programming languages like C, C++, Java, or Python.
How To Analyse an Algorithm?
You can find more than one algorithms for solving a particular problem after that you have to analyze them and use the most efficient one.
The analysis of an algorithm is done base on its efficiency. The two important terms used for the analysis of an algorithm are “Priori Analysis” and “Posterior Analysis”.
Priori Analysis: It is done before the actual implementation of the algorithm when the algorithm is written in the general theoretical language. In this, the efficiency of the algorithm is calculated base on its complexity. It is just an approximate analysis. (alert-passed)
Posterior Analysis: It is done after the actual implementation and execution of the algorithm using any programming language like C, C++, Java, or Python. It is the actual analysis in which the space and time complexity of the algorithm is calculated more correctly. (alert-passed)
Analysis of Algorithms Based on Space and Time Complexity.
The complexity of any algorithm is based upon two important factors, space complexity, and time complexity but it also depends on many other factors like which language you are using, which compiler, or which machine you are using for the implementation of the algorithm so measuring the exact time is very difficult.
Therefore, the time complexity of an algorithm is not measured in time units like seconds or microseconds. It depends on the size of the input, the bigger the size of the input more the time an algorithm will take.
We always analyze the algorithm for bigger input because sometimes it seems similar for smaller input. If the size of the input is n, then f(n) which is a function of n denotes the time complexity.
For example:
The time complexity of a program is given by a function f(n) = 2n^2+2n+1. If we analyze the function for different inputs (n=1, n=10, n=100, n=1000), we can observe the n^2 term as more dominant over other terms in the given equation so we can ignore the smaller terms like n or any constant because it is difficult to calculate the exact function for time complexity and in the asymptotic term we can say that the time complexity for the given function is O(n^2).
This method of measuring the approximate complexity is known as asymptotic complexity. (alert-success)
Now, I hope you understand the above example and the term asymptotic but even if you are unable to get it completely don't worry as we are going to cover this in more detail soon.
First, we need to learn more about the types of asymptotic notations. There are three asymptotic notations that are used to calculate the space and time complexity of an algorithm. They are Big O, Big Ω, and Big Θ. But every time we are not going to calculate all three for an algorithm.
Types of Asymptotic notation.
We use these three notations to express the complexity of an algorithm in three different cases:
Big O is going to say the worst-case time or upper bound which means, if we are giving any time complexity in terms of Big O, it says that in any case, your time will not exceed this.
Big Ω is going to say the best case time or lower bound which means, in any case, you can’t achieve better than this. In this notation, we analyze the minimum number of steps that need to be executed.
Big Θ is going to say the average case time, somewhere in between the worst and best. In this notation, we calculate the average by considering different types of inputs including the best case and the worst case.
In practice, we are not interested in what is the best time the algorithm could execute. We are always interested in what is the worst-case time that will be taken by this algorithm for any input.
Lower Bound < Average Bound < Upper Bound
We use the average case when the best case and worst case are the same and when both cases are taking the same time.
We want to search for an element using linear search, if the element we want to search is 5, then in the best case we can search it in 1 comparison. In 1 step we can find it out.
=> Ω(1) time complexity and it says that you can never go down beyond this.
Now if we want to search 3 out of n elements in the array then we need to compare all the elements in the worst-case O(n) and it says that you can never go beyond this complexity.
C++ Example Code for Linear Search.
//Linear Search Algorithm code in C++
#include <iostream>
using namespace std;
int linearSearch(int arr[], int n, int target) {
for (int i = 0; i < n; i++) {
if (arr[i] == target) {
return i; // return the index of the target element if found
}
}
return -1; // return -1 if target element is not found
}
int main() {
int arr[] = {5, 2, 8, 3, 1, 9};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 3;
int index = linearSearch(arr, n, target);
if (index != -1) {
cout << "Element found at index: " << index << endl;
} else {
cout << "Element not found." << endl;
}
return 0;
}
Output:
Element found at index: 3
In the above code, we have implemented the linear search algorithm to find the target element in the given array. The algorithm iterates through each element of the array and compares it with the target element. If a match is found, the index is returned; otherwise, -1 is returned to indicate that the element was not found.
Now I hope you get some idea of how we can calculate the best, worst, and average time complexity of an algorithm. We are going to practice a lot of questions to master this concept in our upcoming articles.
Related Post:
👉Support Us by Sharing our post with others if you really found this useful and also share your valuable feedback in the comment section below so we can work on them and provide you best 😊.(alert-success)
Thank you for this simple explanation! Very helpful
ReplyDeleteThank you so much!
Delete