An algorithm is the steps taken to solve a problem, a problem can be solved using various steps or algorithms. In the programming world, we refer algorithm to represent codes which are capable of giving a proper solution, with the help of code we instruct the computer so it can work according to the code.
There are 5 main types of algorithms we often implement on a data structure.
- Searching Algorithm
- Sorting Algorithm
- Inserting algorithm
- Updating Algorithm
- Deleting algorithm.
In searching, we try to make an algorithm that capable of searching a specific element from the data structure.
Sorting algorithms capable of sort the data structure elements in ascending and descending order.
In Inserting algorithm, we try to write a code that can insert new elements in the data structure. The new element could be inserted randomly or specified to some specific location in the data structure.
With update algorithms, we can update or edit the elements already present in the data structure.
Delete algorithms can delete specific or random elements from a Data Structure.
Characteristics of a Good Algorithm
To Solve a problem there could be many procedures, to be a good algorithm there are some characteristics to follow.
The algorithm must be unambiguous and lead to a proper result, every step must give a proper meaning. In Algorithm, there should not be a single executable statement which has nothing to do with the result.
Input depend on the type of problem we are solving, so if the problem demands on user-defined input the algorithm must have input in it.
There must be a limited and finite number of steps in an Algorithm. The last step must be capable of giving an appropriate answer.
The Algorithm must be feasible, mean it could be implemented using code.
An algorithm must be independent which mean all the steps mentioned in the algorithm must be applied to each programming language.
The algorithm must be capable of giving the appropriate result.
How to write an Algorithm?
As we know that an Algorithm is an approach to solve a problem using steps, so there is no such standard to write an algorithm. We have provided some characteristics of a good algorithm, make sure any algorithm you write must follow all those characteristics.
One of the main characteristics of an algorithm is Independency, which means the algorithm you use should not apply to a specific programming language. We know that every programming language has similar data structures and statements, though syntax could be different.
Before we code the algorithm in a programming language we use pseudo code to write the algorithm and pseudo code must contain those statements which are common to all programming languages such as loops, if..else statements, class or structure, assignment operators, etc.
For example, let’s write an algorithm to multiple 2 number X and Y:
>>> ask the user to enter a number and store it in a variable X
>>> Ask the user to enter another integer and store it in a value Y
>>> declare a variable Z
>>> Use * operator to multiple X & Y, X*Y
>>> Assign X*Y to Z
>>> Z = X*Y
>>> Print the value of Z, print Z
We analyze an algorithm to check the efficiency, there are two phases where we can analyse an algorithm, after and before implementation also known as Priori and Posterior Analysis.
In the prior analysis, we analyze an algorithm theoretically. We read the algorithm and try to analyse theoretically how efficient the algorithm is and how it would solve the problem. There is a problem with prior analysis because it is analysed by humans so there is a chance that they would not completely estimate the efficiency of the algorithm.
In posterior Analysis, we first implement the algorithm using a specific programming language and then check it live efficiency. Posterior Analysis provides the accurate efficiency of an algorithm. It provides the proper and correct stats of algorithm performance on machine, memory and processors or CPU.
Complexity is a measure to analyze an Algorithm. For example, if two algorithms are capable of performing the same task how can we say which algorithm is better for that we analyse their complexity.
There are two major Complexities we often consider when we compare two algorithms based on their complexity.
- Time Complexity
- Space Complexity
The Time Complexity of an algorithm deals with the amount of time or steps taken by the algorithm to obtain the appropriate answer. In Computer Science we represent the time complexity of an algorithm or program using a mathematically function statement O(n), where n represents the total number of executable steps taken by the algorithm to get the result.
The algorithm which has less time complexity would be considered as a better algorithm.
The Space complexity of an Algorithm deals with the amount of memory space occupied by the algorithm if it will be implemented using a specific language.
If two algorithms have the same time complexity then the algorithm occupying less space in the memory would be considered as a better algorithm.