Algorithms are an important part of solving the problems, whether to make a to-do list, searching a book in the library, or making complex software. As computer science students, many people have to learn it to make it to a job interview or create an app that will create a buzz on the internet. So let us demystify the algorithm jargon.
What is the Algorithm?
Set of steps required to solve a problem is called an algorithm. For example, you want to make a recipe. What will you do? what steps you will follow. The algorithm has certain steps to follow and after following those steps we get the result.
In the software world, the term algorithm means the steps taken in solving the programming problem. Such as searching some of the array elements or searching the nearest path between two points are examples of the algorithm.
Let us talk about one example of searching the element from the array of 10 elements.
In this array of the 10 digits suppose we are finding the digit 4. Now we talk about what approach we can take to search the digit 4. Let's talk about Linear Search
in Linear Search, we check the digit against all elements of the array. so if the value is 4 linear searches will take 4 steps to search the item. it will work like this
4 != 1
4 = 4
Hence, in the above example, the program took 4 steps in completing the task of searching the number. To solve any given problem there may be more than one algorithms. We need to know which algorithm will be best to solve our problem in a given time.
Let's talk about the features of the algorithm. There are few points which we should consider while making an algorithm.
Characteristics of the Algorithm
- Clearly defined problem statement, input and output
- The steps in the algorithm should be in a very specific order.
- The steps also need to be distinct.
- The Algorithm should produce a result
- The Algorithm should complete in finite time
An algorithm should take exact steps to solve the problem for the same input and the result should be the same each time. for example in the above example when we were doing a linear search and we searched for 4 in the array. each time it should take 4 steps and produce the same result. If sometimes it takes 4 steps and some times 5 steps then the algorithm is not correct.
Which Algorithm is good?
As we know that any given problem can be solved using multiple algorithms, now how do know which algorithm is good? how we should choose the best one for our problem. well, there are two criteria on which an algorithm is considered.
What is correctness?
The algorithm should produce the exact output which is required for our program. if it is not correct then the algorithm is not correct. for example, if have made an algorithm to add two numbers and we give it two inputs suppose we give it 5 and 3 as inputs the exact result which should come is 8 but if it does not then the algorithm is not correct.
After getting the correct algorithm we need to test it in terms of its efficiency.
What is efficiency?
There are basically two ways to determine the efficiency when it comes to the algorithms.
Time means the amount of time taken by an algorithm to produce a result or in completion of the algorithm. It should take minimum time to finish the task. it is called time complexity. If an algorithm takes infinite time to finish its job it is a bad algorithm.
Space if the algorithm is fast but it takes all the space which is available in the RAM then it is also a bad algorithm. An algorithm should take minimum space to complete its task.
This is the first part of the series Algorithms and Data structure next part will be published next week.