Unit 1 - Algorithm
Unit 1 - Algorithm
Introduction
❑ Problem solving is the process of overcoming issues, mistakes, errors, failures and risks to
move forward.
❑ It is the programmer who has to write down the solution to the problem in terms of simple
operations which the computer can understand and execute.
❑ In order to solve a problem by the computer one has to pass through certain stages or steps.
Steps Involved in Problem Solving:-
Computer Science: Algorithms form the basis of computer programming and are
used to solve problems ranging from simple sorting and searching to complex tasks
Use of the such as artificial intelligence and machine learning.
Algorithms: Mathematics: Algorithms are used to solve mathematical problems, such as finding
the optimal solution to a system of linear equations or finding the shortest path in a
graph.
Operations Research: Algorithms are used to optimize and make decisions in fields
such as transportation, logistics, and resource allocation.
Data Science: Algorithms are used to analyze, process, and extract insights from
large amounts of data in fields such as marketing, finance, and healthcare.
What is the need for algorithms?
Algorithms are necessary for solving complex problems efficiently and effectively.
They help to automate processes and make them more reliable, faster, and easier to perform.
Algorithms also enable computers to perform tasks that would be difficult or impossible for
humans to do manually.
They are used in various fields such as mathematics, computer science, engineering, finance, and
many others to optimize processes, analyze data, make predictions, and provide solutions to
problems.
What are the Characteristics of an Algorithm?
For some instructions to be an algorithm, it must
have the following characteristics:
✓ Clear and Unambiguous: The algorithm should be unambiguous. Each of its steps should be clear in all
aspects and must lead to only one meaning.
✓ Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs. It may or may not
take input.
✓ Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it should be well-
defined as well. It should produce at least 1 output.
✓ Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time.
✓ Feasible: The algorithm must be simple, generic, and practical, such that it can be executed with the available
resources. It must not contain some future technology or anything.
✓ Language Independent: The Algorithm designed must be language-independent, i.e. it must be just plain
instructions that can be implemented in any language, and yet the output will be the same, as expected.
✓ Input: An algorithm has zero or more inputs. Each that contains a fundamental operator must accept zero or
more inputs.
✓ Output: An algorithm produces at least one output. Every instruction that contains a fundamental operator
must accept zero or more inputs.
Definiteness: All instructions in an algorithm
must be unambiguous, precise, and easy to
interpret. By referring to any of the instructions
For some in an algorithm one can clearly understand
what is to be done. Every fundamental operator
instructions to be in instruction must be defined without any
an algorithm, it ambiguity.
There are several types of algorithms available. Some important algorithms are:
It is the simplest approach to a problem. A brute force algorithm is the first approach that comes to finding when we
see a problem.
Recursive Algorithm
2. Recursive Algorithm:
recursion
A recursive algorithm is based on recursion. In this case, a problem is broken into several sub-parts and called the
same function again and again.
Backtracking Algorithm
3. Backtracking Algorithm:
The backtracking algorithm builds the solution by searching among all possible solutions. Using this algorithm, we
keep on building the solution following criteria. Whenever a solution fails, we trace back to the failure point build
on the next solution and continue this process till we find the solution, or all possible solutions are looked after.
Searching Algorithm:
Searching algorithms are the ones that are used for searching elements or groups of elements from a
particular data structure. They can be of different types based on their approach or the data structure in
which the element should be found.
5. Sorting Algorithm:
Sorting is arranging a group of data in a particular manner according to the requirement. The algorithms
which help in performing this function are called sorting algorithms. Generally sorting algorithms are
used to sort groups of data in an increasing or decreasing manner.
6. Hashing Algorithm:
Hashing algorithms work similarly to the searching algorithm. But they contain an index with a key ID. In
hashing, a key is assigned to specific data.
7. Divide and Conquer Algorithm:
This algorithm breaks a problem into sub-problems, solves a single sub-problem, and merges the
solutions to get the final solution. It consists of the following three steps:
❑ Divide
❑ Solve
❑ Combine
Greedy Algorithm:
In this type of algorithm, the solution is built part by part. The solution for the
next part is built based on the immediate benefit of the next part. The one solution
that gives the most benefit will be chosen as the solution for the next part.
9. Dynamic Programming Algorithm:
This algorithm uses the concept of using the already found solution to avoid
repetitive calculation of the same part of the problem. It divides the problem into
smaller overlapping subproblems and solves them.
10. Randomized Algorithm:
In the randomized algorithm, we use a random number so it gives immediate
benefit. The random number helps in deciding the expected outcome.
To learn more about the types of algorithms refer to the article about “Types of
Algorithms“.
Advantages of Algorithms:
❖ It is easy to understand.
❖ An algorithm is a step-wise representation of a solution to a given problem.
❖ In an Algorithm the problem is broken down into smaller pieces or steps hence,
it is easier for the programmer to convert it into an actual program.
❖ Disadvantages of Algorithms:
❖ Writing an algorithm takes a long time so it is time-consuming.
❖ Understanding complex logic through algorithms can be very difficult.
❖ Branching and Looping statements are difficult to show in Algorithms(imp).
Disadvantages of Algorithms:
Step 1: Start
Step 2: Declare a variable x
Step 3: Take a input from user and store in x
Step 4: IF x % 2 == 0 THEN
PRINT Even
ELSE
PRINT Odd
Step 5: End
Flowchart Algorithm
Steps in Problem Solving - Coding, Compiling and
Execution
Flowchart to find Area of circle
Flowchart to find Sum of Two numbers
Flowchart to find Simple Interest
Flowchart to Find Largest among two
numbers
Flowchart:-
Advantages:- Disadvantages:-
Easy to make. It is a time-consuming process.
Mistakes can be easily identified. No scope for alteration or
Communication becomes effective and modification.
easy to understand. No man to computer communication.
Logics can be easily interpreted.
Difference b/w Algorithm and Flowchart:-
Algorithm Flowchart
It is complex to understand. It is easy to understand.
1. Internet
2. The Human Genome Project
3. E-Commerce
4. Page Rank
5. Weather Forecasting
6. Linear Programming
7. Shortest Path Algorithm
8. Other Important Applications of Algorithms
Pseudo code:-
Pseudo code is a simple way of describing a set of instructions that does not
have to use specific syntax.
Common pseudo code notation:-
1) Input
2) Output
3) While
4) For
5) Repeat until
6) If then else
Using Pseudo code:-
REPEAT
OUTPUT “What is the best subject you take?”
INPUT user inputs the best subject they take
STORE the user’s input in the answer variable
IF answer = ‘Computer Science’ THEN
OUTPUT ‘of course it is!’
ELSE
OUTPUT ‘try again!’
UNTIL answer = ‘Computer Science’
Flowchart for Pseudocode:-
Asymptotic notations:-
It is used to mathematically calculate the running time of any operation inside
an algorithm. OR
The asymptotic notations used for calculating the running time complexity of
an algorithm.
There are mainly three asymptotic notations:-
• Big-O Notation (O-notation)
• Omega Notation (Ω-notation)
• Theta Notation (Θ-notation)
Big-O Notation (O-notation):-
Big-O notation represents the upper bound of the running time of an algorithm.
Therefore, it gives the worst-case complexity of an algorithm.
• It is the most widely used notation for Asymptotic analysis.
• It specifies the upper bound of a function.
• It returns the highest possible output value(big-O) for a given input.
Big-Oh(Worst Case) It is defined as the condition that allows an algorithm to
complete statement execution in the longest amount of time possible.
If f(n) describes the running time of an algorithm,
f(n) is O(g(n)) if there exist a positive constant C
and n0 such that, 0 ≤ f(n) ≤ cg(n) for all n ≥ n0.
Definition:
Performance analysis of an algorithm is
the process of calculating space required
by that algorithm and time required by
that algorithm.
Priori and Posteriori Analysis
To analyze the algorithm, 2 phases are required.
1)Priori Analysis – “Priori” means before. This analysis is done before its
implementation.
Total time taken by the algorithm = The number of times the statement will be executed
(frequency count) x time taken for one execution
The notations used in Priori analysis are Big-oh (O), Omega (Ω), Theta(θ), small-oh(o).
a) Debugging: After execution of program if there are faulty results, then they are corrected
using this approach.
b) Profiling: It is the actual time taken by the algorithm to process the data.
1. Read data
2. Time (t1)
3. Process (data)
4. Time (t2)
5. Write (time = t2 – t1)
Priori and Posteriori Analysis
Difference between Priori Analysis and Posteriori Analysis
Double 8 bytes
The space needed by an algorithm consists of the following components:-
a) The fixed static part:- A fixed part that is a space required to store certain data
and variables, that are independent of the size of the problem. For example,
simple variables and constants used, program size, etc
b) The variable dynamic part:- A variable part is a space required by variables,
whose size depends on the size of the problem.
The overall space requirements for an algorithm is the sum of both the fixed
static part storage and variable dynamic part storage.
If P be a program, then space required for program P will be denoted by S(P).
S(p) = Cp + Sp
Time complexity:-
Input: A = 2, B = 6
Output: Quotient = 0, Remainder = 2
Input: A = 17, B = 5
Output: Quotient = 3, Remainder = 2
Integer and Absolute value function
Integer:
Integers are whole numbers. They can be positive, negative, or zero.
Numbers like -321, 497, 19345, and -976812 are all perfectly valid integers, but 4.5 is
not because 4.5 is not a whole number.
Floating point numbers are numbers with a decimal.
Absolute:
The use of the function abs in C programming is to return the absolute value of an
integer. By absolute value, it means the function returns the positive value of an
integer.
The parameter or argument of function abs in C is an integral value. To use the abs()
function in C, you need a header file called <stdlib.
C library - abs() function. This function only returns the positive integer.
For example, if we have an integer value of -2, we want to get the absolute number of
-2. We then used the abs() function to return the positive number 2.
Factorial Function
10 20
a b
We can use the assignment operator “=” to assign the value to a variable as shown below:
a=b
b=a
The assignment (a=b) has changed the value of a to 20 and the assignment (b= a) has changed the value
of b to 20. Since the value of a is 20 after assignment (a=b) and its value is sorted in b.
20 10
a b
We cannot solve the problem by assigning the value of ‘a’ to ‘b’ and ‘b’ to ‘a’. It is because once the value
of ‘a’ is assigned to ‘b’, then original value of ‘a’ is overwritten and will be lost.
Let us use temporary variable ‘t’ to solve this problem.
10 10 10
a b t
First assign the value of ‘a’ to ‘t’ (a=t)
10 20 10
a b t
Assign the value of ‘b’ to ‘a’ (a=b)
20 20 10
a b t
Assign the value of ‘t’ to ‘b’ (a=t)
20 10 10
a b t
Finally, the value of a and b are swapped using temporary variable ‘t’
Swap Two Numbers Using a Temporary Variable
Swapping two numbers means exchanging their values.
Example
Input: a = 5, b = 10
Output: a = 10, b = 5
Explanation: The values of a and b are exchanged.
Input: a = -1, b = 20
Output: a = 20, b = -1
Explanation: The values of a and b are exchanged.
The idea behind swapping two numbers using 3rd variable is simple. Store the value of
1st variable in temporary variable. Store the value of 2nd variable in the first variable. At last,
store the value of temp variable in 2nd variable. In this program, we are using a temporary
variable to hold the value of the first variable.
Code:
temp = var1;
var1 = var2;
var2 = temp;
In this method, we use a temporary variable to hold one of the
values, then we assign the value of the second variable to the first,
and finally, we assign the temporary value to the second variable.
✓ Store the value of a in a temporary variable temp.
✓ Assign the value of b to a.
✓ Assign the value stored in the temp to b.
Example
// C Program to Swap Two Numbers using a Temporary Variable
#include <stdio.h>
int main() {
int a = 5, b = 10, temp;
In this variation of swapping two variables, we are not using any temporary variable to
store the value. In the first variable we are storing the sum of both variable. Then, in next
step we are we are extracting the value of 1st variable by subtracting the value of
2nd variable form the sum & storing it in 2nd variable. At last, we are extracting the
original value of the 2nd variable & storing it in the 1st variable.
Code:
var1 = var1 + var2;
var2 = var1 - var2;
var1 = var1 - var2;
Swap Two Numbers Without Using a Temporary Variable
int main() {
int a = 5, b = 10; Output:
printf("Before swapping: a = %d, b = %d\n", a, b);
Before swapping: a = 5, b = 10
// Add the values of a and b After swapping: a = 10, b = 5
a = a + b;
// Subtract the value of b from a
b = a - b;
// Subtract the new value of b from a
a = a - b;
Example:
Check the grade of the students based on marks. First of all we will take input a mark of subject from the
candidate and according to following condition we will calculate the grade.
[Link] marks <50 then Grade is F
[Link] marks >=50 <60 then Grade is D
[Link] marks >=60 <70 then Grade is C
[Link] marks >=70 <80 then Grade is B
[Link] marks >=80 <90 then Grade is A
[Link] marks >=90 then Grade is A+
Summation
The symbol ∑ indicates summation and is used as a shorthand notation for
the sum of terms that follow a pattern.
For example, the sum of the first 4 squared integers, 12+22+32+42, follows
a simple pattern: each term is of the form i2, and we add up values from i=1
to i=4.
Sum = i1+i2+i3+i4+…….in
Computer can add two numbers at a time and return the sum of two numbers. So for this purpose we assign the
variable sum as 0.
Sum = sum+i1
Sum = sum+i2
Sum = sum+i3
Sum = sum+i4
sum = sum+i n
Fibonacci sequence
The Fibonacci numbers may be defined by the recurrence relation.
The Fibonacci numbers are the numbers in the following integer sequence: 0, 1, 1, 2, 3, 5,
8, 13, 21, 34, 55, 89, 144.
Examples:
return a; 34
#include <stdio.h>
int main() {
char ch = '5';
int intValue = ch - '0';
printf("The integer value is: %d\n", intValue);
return 0;
}
Output:
The integer value is: 5
Reversing the digits of an integer
We can just write the number in the reverse order of digits. In other
words, reversing a number means writing the given number in the reversed order
of digits.
For example, if the number is “12345”, the reverse number will be “54321”.
The reverse of a number means reversing the order of digits of a number.
For example, if number num = 12548, the reverse of number num is 84521.
Flow of Program To Reverse a Number
C Program to Reverse an Integer
// C program to implement
// the above approach
#include <stdio.h>
// Iterative function to reverse digits of num
int reverseDigits(int num)
{
int rev_num = 0;
while (num > 0)
{
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
// Driver code
int main()
{
int num = 4562;
printf("Reverse of is %d", reverseDigits(num));
getchar();
return 0;
}
Output:
Reverse of no. is 2654
Explanation
The above program takes an integer num as input. We use a while loop to iterate until the value of num
becomes 0. Inside the loop, the last digit of num is extracted using the modulo operator (num % 10). This digit
is then added to rev_num after multiplying it by 10, which means the existing digits of rev_num are shifted one
place to the left.
The value of num is updated by dividing it by 10, (num = num / 10). This removes the last digit of num in each
iteration, and terminates the loop when num becomes 0. rev_num is returned, which contains the reversed
digits of the original number.
num = 4562
rev_num = 0
rev_num = rev_num *10 + num%10 = 2
num = num/10 = 456
rev_num = rev_num *10 + num%10 = 20 + 6 = 26
num = num/10 = 45
rev_num = rev_num *10 + num%10 = 260 + 5 = 265
num = num/10 = 4
rev_num = rev_num *10 + num%10 = 2650 + 4 = 2654
num = num/10 = 0
Thank You…
End of Unit 1