BAsic Concept of Algorithm
BAsic Concept of Algorithm
Characteristics,
Types and How to write it
Table of Contents
What is an Algorithm?
Characteristics of an Algorithm
Factors of an Algorithm
Importance of an Algorithm
Approaches of an Algorithm
Analysis of an Algorithm
Types of Algorithms
• According to its formal definition, an algorithm is a finite set of instructions carried out
in a specific order to perform a particular task.
• Input: After designing an algorithm, the algorithm is given the necessary and desired
inputs.
• Processing unit: The input will be passed to the processing unit, producing the desired
output.
After defining what an algorithm is, you will now look at algorithm characteristics.
Characteristics of an Algorithm
• Input: An algorithm requires some input values. An algorithm can be given a value other
than 0 as input.
• Output: At the end of an algorithm, you will have one or more outcomes.
Moving on in this What is an Algorithm tutorial, you will look at why you need 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.
After understanding what is an algorithm, why you need an algorithm, you will look at how
to write one using an example.
What is an Algorithm?
• According to its formal definition, an algorithm is a finite set of instructions carried out
in a specific order to perform a particular task.
• Input: After designing an algorithm, the algorithm is given the necessary and desired
inputs.
• Processing unit: The input will be passed to the processing unit, producing the desired
output.
After defining what an algorithm is, you will now look at algorithm characteristics.
Characteristics of an Algorithm
• Output: At the end of an algorithm, you will have one or more outcomes.
• Finiteness: An algorithm must be finite. Finiteness in this context means that the
algorithm should have a limited number of instructions, i.e., the instructions should be
countable.
Moving on in this What is an Algorithm tutorial, you will look at why you need an algorithm.
Why Do You Need 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.
After understanding what is an algorithm, why you need an algorithm, you will look at how
to write one using an example.
• 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: Create an algorithm that multiplies two numbers and displays the output.
Step 1 − Start
Step 6 − print z
Step 7 − Stop
Algorithms instruct programmers on how to write code. In addition, the algorithm can be
written as:
Step 4 − display z
Step 5 − Stop
In algorithm design and analysis, the second method is typically used to describe an
algorithm. It allows the analyst to analyze the algorithm while ignoring all unwanted
definitions easily. They can see which operations are being used and how the process is
progressing. It is optional to write step numbers. To solve a given problem, you create an
algorithm. A problem can be solved in a variety of ways.
As a result, many solution algorithms for a given problem can be derived. The following
step is to evaluate the proposed solution algorithms and implement the most appropriate
solution.
As you progress through this "what is an Algorithm" tutorial, you will learn about some of
the components of an algorithm.
Factors of 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.
• Functionality: It takes into account various logical steps to solve a real-world problem.
• User-friendly: If the algorithm is difficult to understand, the designer will not explain it
to the programmer.
You will now see why an algorithm is so essential after understanding some of its
components.
Importance of an Algorithm
Theoretical Significance
When you are given a real-world problem, you must break it down into smaller modules.
To deconstruct the problem, you must first understand all of its theoretical aspects.
Practical Significance
As you all know, theory cannot be completed without practical application. As a result,
the significance of algorithms can be considered both theoretically and practically.
As you progress through this "what is an algorthim" tutorial, you will see algorithmic
approaches.
Approaches of an Algorithm
This algorithm uses the general logic structure to design an algorithm. It is also called an
exhaustive search algorithm because it exhausts all possibilities to provide the required
solution. There are two kinds of such algorithms:
1. Optimizing: Finding all possible solutions to a problem and then selecting the best one,
will terminate if the best solution is known.
2. Sacrificing: It will stop as soon as the best solution is found.
• Greedy Algorithm
This is an algorithm paradigm that makes the best choice possible on each iteration in
the hopes of choosing the best solution. It is simple to set up and has a shorter execution
time. However, there are very few cases where it is the best solution.
• Dynamic Programming
It improves the efficiency of the algorithm by storing intermediate results. It goes through
five steps to find the best solution to the problem:
2. After breaking down the problem into subproblems, it finds the best solution from
these subproblems.
4. Reuse the result to prevent it from being recomputed for the same subproblems.
Only integer programming problems can be solved using the branch and bound algorithm.
This method divides all feasible solution sets into smaller subsets. These subsets are
then evaluated further to find the best solution.
• Randomized Algorithm
As with a standard algorithm, you have predefined input and output. Deterministic
algorithms have a defined set of information and required results and follow some
described steps. They are more efficient than non-deterministic algorithms.
• Backtracking
It is an algorithmic procedure that recursively and discards the solution if it does not
satisfy the constraints of the problem.
Following your understanding of what is an algorithm, and its approaches, you will now
look at algorithm analysis.
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
Time Complexity
mul = 1;
for i=1 to n
// when the loop ends, then mul holds the multiplication of the n numbers
return mul;
The time complexity of the loop statement in the preceding code is at least n, and as the
value of n escalates, so does the time complexity. While the code's complexity, i.e.,
returns mul, will be constant because its value is not dependent on the importance of n
and will provide the result in a single step. The worst-time complexity is generally
considered because it is the maximum time required for any given input size.
Space Complexity
The amount of space an algorithm requires to solve a problem and produce an output is
called its space complexity. Space complexity, like time complexity, is expressed in big O
notation.
Finally after understanding what is an algorithm, its analysis and approches, you will look
at different types of algorithms.
Types of Algorithms
• Sort Algorithm
Search Algorithm
Every day, you look for something in your daily life. Similarly, in the case of a computer, a
large amount of data is stored in the computer, and whenever a user requests data, the
computer searches for that data in the memory and returns it to the user. There are
primarily two methods for searching data in an array:
• Linear Search
Linear search is a simple algorithm that begins searching for an element or a value at the
beginning of an array and continues until the required element is not found. It compares
the element to be searched with all the elements in an array; if a match is found, the
element index is returned; otherwise, -1 is returned. This algorithm can be applied to an
unsorted list.
• Binary Search
A binary algorithm is the most basic algorithm, and it searches for elements very quickly.
It is used to find an element in a sorted list. To implement the binary algorithm, the
elements must be stored in sequential order or sorted. If the elements are stored
randomly, binary search cannot be implemented.
Sort Algorithm