Algorithms
An algorithm can be stated as a step-by-step procedure that defines a set of instructions that must be carried out in a specific order to produce the desired result. Algorithms are generally developed independently of other programming languages, which means that an algorithm can be executed or implemented in more than one programming language.
Characteristics of an Algorithm
Each and every procedure can’t be called an algorithm. An algorithm should have the following characteristics
Unambiguous − Algorithm should be clear and unambiguous. Each of its steps or phases, and their inputs as well as outputs should be clear and must lead to only one meaning.
Input − An algorithm should have zero or more well-defined inputs.
Output − An algorithm should have one or more well-defined outputs, and should match the desired output.
Finiteness − Algorithms must terminate after a finite number of steps.
Feasibility − It should be feasible with the available resources.
Independent − An algorithm should have step-by-step directions, which should be independent of any other programming code.
Categories of Algorithms
Search − Algorithm to search an item in a data structure.
Sort − Algorithm to sort items in a certain order.
Insert − Algorithm to insert item in a data structure.
Update − Algorithm to update an existing item in a data structure.
Delete − Algorithm to delete an existing item from a data structure.
Need of an Algorithm
Scalability
It aids in your understanding of scalability. When you have a sizable real-world problem, you must break it down into small steps to analyze it quickly.
Performance
The real world is challenging to break down into smaller steps. If a problem can be easily divided into smaller steps, it indicates that the problem is feasible.
Factors of an Algorithm
The following are the factors that arec considered while designing an algorithm
Modularity - This feature was perfectly designed for the algorithm if you are given a problem and break it down into small-small modules or small-small steps, which is a basic definition of an algorithm.
Correctness - An algorithm’s correctness is defined as when the given inputs produce the desired output, indicating that the algorithm was designed correctly. An algorithm’s analysis has been completed correctly.
Maintainability - It means that the algorithm should be designed in a straightforward, structured way so that when you redefine the algorithm, no significant changes are made to the algorithm.
Functionality - It takes into account various logical steps to solve a real-world problem.
Robustness - Robustness refers to an algorithm’s ability to define your problem clearly.
User-friendly - If the algorithm is difficult to understand, the designer will not explain it to the programmer.
Simplicity - If an algorithm is simple, it is simple to understand.
Extensibility - Your algorithm should be extensible if another algorithm designer or programmer wants to use it.
Writing an Algorithm
There are no well-defined standards for writing algorithms. It is, however, a problem that is resource-dependent.
Algorithms are never written with a specific programming language in mind.
As you all know, basic code constructs such as loops like do, for, while, all programming languages share flow control such as if-else, and so on.
An algorithm can be written using these common constructs.
Algorithms are typically written in a step-by-step fashion, but this is not always the case.
Algorithm writing is a process that occurs after the problem domain has been well-defined.
That is, you must be aware of the problem domain for which you are developing a solution.
Example
Problem − Design an algorithm to add two numbers and display the result.
- Step 1 − START
- Step 2 − declare three integers a, b & c
- Step 3 − define values of a & b
- Step 4 − add values of a & b
- Step 5 − store output of step 4 to c
- Step 6 − print c
- Step 7 − STOP
Algorithms tell the programmers how to code the program. Alternatively, the algorithm can be written as −
- Step 1 − START ADD
- Step 2 − get values of a & b
- Step 3 − c ← a + b
- Step 4 − display c
- Step 5 − STOP
In design and analysis of algorithms, usually the second method is used to describe an algorithm. It makes it easy for the analyst to analyze the algorithm ignoring all unwanted definitions. He can observe what operations are being used and how the process is flowing. Writing step numbers, is optional.
Analysis of an Algorithm
The algorithm can be examined at two levels: before and after it is created. The two algorithm analyses are as follows:
Priori Analysis
In this context, priori analysis refers to the theoretical analysis of an algorithm performed before implementing the algorithm.
Before implementing the algorithm, various factors such as processor speed, which does not affect the implementation, can be considered.
Posterior Analysis
In this context, posterior analysis refers to a practical analysis of an algorithm.
The algorithm is implemented in any programming language to perform the experimental research.
This analysis determines how much running time and space is required.