SlideShare a Scribd company logo
1Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Ramakant Soni
Assistant Professor
CS Dept., BKBIET Pilani
ramakant.soni1988@gmail.com
What is an Algorithm ?
It is a step by step procedure or a set of steps to accomplish a task.
“An algorithm is any well-defined computational procedure that takes some value, or set of
values, as input and produces some value, or set of values as output."
Introduction to Algorithms by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein
Source: www.akashiclabs.com
2Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Were do we use them?
 To find the best path to travel (Google Maps).
 For weather forecasting.
 To find structural patterns and cure diseases.
 For making games that can defeat us in it.(chess)
 For the processing done at the server each time we check the mail.
 When we take a selfie, edit them, post them to social media and get likes.
 To buy some products online and pay for it sitting at our home.
 For the synchronization in the traffic lights of the whole city.
 For software and techniques used to make animation movie.
 Even a simple program of adding, subtracting, multiplying is an algorithm and also calculating the speed, path, fuel of a
space shuttle is done by using algorithms.
 And many more……….(almost everywhere!)
3Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Write algorithm
Compute Complexity
& Optimize
Code program
Changes/ Modifications
Source: openclassroom.stanford.edu
Algorithm Example: Algorithm to add two numbers entered by user.
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
#include <stdio.h>
int main()
{
int firstNo, secondNo, sum;
printf ("Enter two integers: ");
// Two integers entered by user is stored using scanf() function
scanf("%d %d",&firstNo, &secondNo);
// sum of two numbers in stored in variable sum
sum = firstNo + secondNo;
// Displays sum
printf ("%d + %d = %d", firstNo, secondNo, sum);
return 0;
}
Enter two integers: 12 ,11 Sum=12 + 11 = 23
5Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Algorithm to find largest among three numbers.
Step 1: Start
Step 2: Declare variables a, b and c.
Step 3: Read variables a, b and c.
Step 4: If a>b
If a>c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b>c
Display b is the largest number.
Else
Display c is the largest number.
Step 5: Stop
#include <stdio.h>
int main()
{
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1>=n2)
{ if(n1>=n3)
printf ("%.2lf is the largest number.", n1);
else
printf ("%.2lf is the largest number.", n3);
}
else
{ if(n2>=n3)
printf ("%.2lf is the largest number.", n2);
else
printf ("%.2lf is the largest number.",n3);
}
return 0;
}
6Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Step 1: Start
Step 2: Declare variables n, factorial and i.
Step 3: Initialize variables: factorial←1
i←1
Step 4: Read value of n
Step 5: Repeat the steps until i=n
factorial ← factorial*i
i←i+1
Step 6: Display factorial
Step 7: Stop
#include <stdio.h>
int main()
{
int n, i;
unsigned long long factorial = 1;
printf("Enter an integer: ");
scanf("%d",&n);
// show error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else
{
for(i=1; i<=n; ++i)
{
factorial *= i; // factorial = factorial*i;
}
printf("Factorial of %d = %llu", n, factorial);
}
return 0;
}
Algorithm to find the factorial of a number entered by user.
Enter an integer: 10 Factorial of 10 = 10*9*8*7*6*5*4*3*2*1=3628800
7Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Algorithm to find the Fibonacci series till term≤ n.
Step 1: Start
Step 2: Declare variables first_term, second_term and temp.
Step 3: Initialize variables first_term←0 second_term←1
Step 4: Display first_term and second_term
Step 5: Repeat the steps until second_term ≤ n
temp ← second_term
second_term ← second_term + first term
first_term ← temp
Display second_term
Step 6: Stop
#include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm = 0;
Printf ("Enter the number of terms: ");
Scanf ("%d",&n);
// displays the first two terms which is always 0 and 1
Printf ("Fibonacci Series: %d, %d, ", t1, t2);
// i = 3 because the first two terms are already displayed
for (i=3; i <= n; ++i)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
printf ("%d ", nextTerm);
}
return 0;
}
Enter a positive integer: 100 Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
8Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Algorithm Analysis
In computer science, the analysis of algorithms is the determination of the amount of resources (such as time
and storage) necessary to execute them.
The efficiency or running time of an algorithm is stated as a function relating the input length to the number
of steps (time complexity) or storage locations (space complexity).
1) Worst Case Analysis (Usually Done):In the worst case analysis, we calculate upper bound on running time of
an algorithm by considering worst case (a situation where algorithm takes maximum time)
2) Average Case Analysis (Sometimes done) :In average case analysis, we take all possible inputs and calculate
computing time for all of the inputs.
3) Best Case Analysis (Bogus) :In the best case analysis, we calculate lower bound on running time of an
algorithm.
Source: http://quiz.geeksforgeeks.org/lmns-algorithms/
9Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Asymptotic Notations
• Θ Notation: The Theta notation bounds a functions from above and below, so it defines exact asymptotic behavior.
Θ((g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that 0 <= c1*g(n) <= f(n) <= c2*g(n) for all n >= n0}
• Big O Notation: The Big O notation defines an upper bound of an algorithm, it bounds a function only from above.
O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 <= f(n) <= cg(n) for all n >= n0}
• Ω Notation: The Omega notation provides an asymptotic lower bound.
Ω (g(n)) = {f(n): there exist positive constants c and n0 such that 0 <= cg(n) <= f(n) for all n >= n0}.
Source: http://quiz.geeksforgeeks.org/lmns-algorithms/
10Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Big O Complexity Chart
Source: http://bigocheatsheet.com/
11Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Insertion Sort
Statement Running Time of Each Step
InsertionSort (A, n) {
for i = 2 to n { c1n
key = A[i] c2(n-1)
j = i - 1; c3(n-1)
while (j > 0) and (A[j] > key) { c4T
A[j+1] = A[j] c5(T-(n-1))
j = j - 1 c6(T-(n-1))
} 0
A[j+1] = key c7(n-1)
} 0
}
T = t2 + t3 + … + tn where ti is number of while expression evaluations for the ith for loop iteration
12Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Insertion Sort Example
13Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Insertion Sort Analysis
• T(n) = c1n + c2(n-1) + c3(n-1) + c4T + c5(T - (n-1)) + c6(T - (n-1)) + c7(n-1) + c8T + c9n + c10
= = O(n2)
• What can T be?
• Best case -- inner loop body never executed- sorted elements
• ti = 1  T(n) is a linear function
• Worst case -- inner loop body executed for all previous elements
• ti = i  T(n) is a quadratic function
14Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Merge Sort
MergeSort(A, left, right)
{
if (left < right)
{
mid = floor((left + right) / 2);
MergeSort (A, left, mid);
MergeSort (A, mid+1, right);
Merge(A, left, mid, right);
// Merge() takes two sorted subarrays of A and merges them into a single sorted subarray of A
}
}
When n ≥ 2, time for merge sort steps:
Divide : Just compute mid as the average of left and right, which takes constant time i.e. Θ(1).
Conquer : Recursively solve 2 sub-problems, each of size n/2, which is 2T(n/2).
Combine : MERGE on an n-element subarray takes Θ(n) time
15Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
MERGE (A, left, mid, right )
1. n1 ← mid − left + 1
2. n2 ← right − mid
3. Create arrays L[1 . . n1 + 1] and R[1 . . n2 + 1]
4. FOR i ← 1 TO n1
5. DO L[i] ← A[left + i − 1]
6. FOR j ← 1 TO n2
7. DO R[j] ← A[mid + j ]
8. L[n1 + 1] ← ∞
9. R[n2 + 1] ← ∞
10. i ← 1
11. j ← 1
12. FOR k ← left TO right
13. DO IF L[i ] ≤ R[ j]
14. THEN A[k] ← L[i]
15. i ← i + 1
16. ELSE A[k] ← R[j]
17. j ← j + 1
Merge Sort
Source: http://www.personal.kent.edu
16Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Analysis of Merge Sort
Statement Running Time of Each Step______
MergeSort (A, left, right) T(n)
{ if (left < right) (1)
{ mid = floor((left + right) / 2); (1)
MergeSort (A, left, mid); T(n/2)
MergeSort (A, mid+1, right); T(n/2)
Merge(A, left, mid, right); (n)
// Merge() takes two sorted subarrays of A and merges them into a single sorted subarray of A
}}
• So T(n) = (1) when n = 1, and
2T(n/2) + (n) when n > 1
Complexity= O(n log n)
17Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
The Master Theorem
• Given: a divide and conquer algorithm
• An algorithm that divides the problem of size n into a sub-problems, each of
size n/b
• Let the cost of each stage (i.e., the work to divide the problem + combine
solved sub-problems) be described by the function f(n)
• T(n) = a*T(n/b) + f(n)
18Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
The Master Theorem
if T(n) = a*T(n/b) + f(n) , then
 
 
 
 
 
 



































1
0
largefor)()/(
AND)(
)(
)(
)(
log)(
log
log
log
log
log
c
nncfbnaf
nnf
nnf
nOnf
nf
nn
n
nT
a
a
a
a
a
b
b
b
b
b



19Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Quick Sort
Quicksort(A, p, r)
{ if (p < r)
{ q = Partition(A, p, r);
Quicksort(A, p, q);
Quicksort(A, q+1, r);
}
}
Another divide-and-conquer algorithm
• The array A[p .. r] is partitioned into two non-empty subarrays A[p .. q] and A[q+1 ..r]
Invariant: All elements in A[p .. q] are less than all elements in A[q+1..r]
• The subarrays are recursively sorted by calls to quicksort
Unlike merge sort, no combining step: two subarrays form an already-sorted array
Actions that takes place in the partition() function:
• Rearranges the subarray in place
• End result:
• Two subarrays
• All values in first subarray  all values in second
• Returns the index of the “pivot” element separating the two subarrays
20Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Quick Sort Partition Pseudo code
21Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Quicksort(A, p, r)
{ if (p < r)
{ q = Partition(A, p, r);
Quicksort(A, p, q);
Quicksort(A, q+1, r);
}
}
Quick Sort Example & Analysis
In the worst case ( unbalanced partition ):
T(1) = (1)
T(n) = T(n - 1) + (n)
Works out to
T(n) = (n2)
In the best case ( balanced partition ):
T(n) = 2T(n/2) + (n)
Works out to
T(n) = (n log n)
22Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Linear Search
23Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
function find_Index (array, target)
{
for(var i = 0; i < array.length; ++i)
{
if (array[i] == target)
{
return i;
}
}
return -1;
}
• A linear search searches an element or value from an array till the
desired element or value is not found and it searches in a
sequence order.
• It compares the element with all the other elements given in the
list and if the element is matched it returns the value index else it
return -1.
• Running Time: T(n)= a(n-1) + b
Example: Search 5 in given data 5
Binary Search
24Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
binary_search(A, low, high)
low = 1, high = size(A)
while low <= high
mid = floor(low + (high-low)/2)
if target == A[mid]
return mid
else if target < A[mid]
binary_search(A, low, mid-1)
else
binary_search(A, mid+1, high)
else
“target was not found”
Binary Search is an instance of divide-and-conquer paradigm.
Given an ordered array of n elements, the basic idea of binary search
is that for a given element we "probe" the middle element of the
array.
We continue in either the lower or upper segment of the array,
depending on the outcome of the probe until we reached the
required (given) element.
Complexity Analysis:
Binary Search can be accomplished in logarithmic time in the worst
case , i.e., T(n) = θ(log n).
Binary Search Example
25Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
1
2
3
Source: http://www.csit.parkland.edu
Algorithm to check Palindrome
26Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or
forward.
function isPalindrome (text)
if text is null
return false
left ← 0
right ← text.length - 1
while (left < right)
if text[left] is not text[right]
return false
left ← left + 1
right ← right - 1
return true
Complexity:
The first function isPalindrome has a time
complexity of O(n/2) which is equal to O(n)
Palindrome Example:
CIVIC
LEVEL
RADAR
RACECAR
MOM
Algorithm to check Prime/ Not Prime
27Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or
forward.
isPrime (int n){
int i;
if (n==2)
return 1;
if (n % 2==0)
return 0;
for (i = 3; i < =sqrt(n); i++)
if (n % I == 0)
return 0;
return 1;
}
Complexity:
O(sqrt(n)))
Common Data Structure Operations
Source: http://bigocheatsheet.com/
28Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
Array Sorting Algorithms
Source: http://bigocheatsheet.com/
29Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
All The Best !
30Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani

More Related Content

What's hot

Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmos
luzenith_g
 
Convolution
ConvolutionConvolution
Convolution
Sadia Shachi
 
Lec23
Lec23Lec23
Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)
Kumar
 
Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )
swapnac12
 
Time andspacecomplexity
Time andspacecomplexityTime andspacecomplexity
Time andspacecomplexity
LAKSHMITHARUN PONNAM
 
Linear Convolution using Matlab Code
Linear Convolution  using Matlab CodeLinear Convolution  using Matlab Code
Linear Convolution using Matlab Code
Bharti Airtel Ltd.
 
Computer notes - Sorting
Computer notes  - SortingComputer notes  - Sorting
Computer notes - Sorting
ecomputernotes
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
Soujanya V
 
Time complexity
Time complexityTime complexity
Time complexity
Katang Isip
 
Lecture 3 insertion sort and complexity analysis
Lecture 3   insertion sort and complexity analysisLecture 3   insertion sort and complexity analysis
Lecture 3 insertion sort and complexity analysis
jayavignesh86
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
Kurmendra Singh
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
Sajid Marwat
 
Big o
Big oBig o
Mechanical Engineering Homework Help
Mechanical Engineering Homework HelpMechanical Engineering Homework Help
Mechanical Engineering Homework Help
Matlab Assignment Experts
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
Muhammad Muzammal
 
multi threaded and distributed algorithms
multi threaded and distributed algorithms multi threaded and distributed algorithms
multi threaded and distributed algorithms
Dr Shashikant Athawale
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
sohelranasweet
 
Slide2
Slide2Slide2
Lec3
Lec3Lec3

What's hot (20)

Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmos
 
Convolution
ConvolutionConvolution
Convolution
 
Lec23
Lec23Lec23
Lec23
 
Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)
 
Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )Asymptotic notations(Big O, Omega, Theta )
Asymptotic notations(Big O, Omega, Theta )
 
Time andspacecomplexity
Time andspacecomplexityTime andspacecomplexity
Time andspacecomplexity
 
Linear Convolution using Matlab Code
Linear Convolution  using Matlab CodeLinear Convolution  using Matlab Code
Linear Convolution using Matlab Code
 
Computer notes - Sorting
Computer notes  - SortingComputer notes  - Sorting
Computer notes - Sorting
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
 
Time complexity
Time complexityTime complexity
Time complexity
 
Lecture 3 insertion sort and complexity analysis
Lecture 3   insertion sort and complexity analysisLecture 3   insertion sort and complexity analysis
Lecture 3 insertion sort and complexity analysis
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Big o
Big oBig o
Big o
 
Mechanical Engineering Homework Help
Mechanical Engineering Homework HelpMechanical Engineering Homework Help
Mechanical Engineering Homework Help
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
multi threaded and distributed algorithms
multi threaded and distributed algorithms multi threaded and distributed algorithms
multi threaded and distributed algorithms
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Slide2
Slide2Slide2
Slide2
 
Lec3
Lec3Lec3
Lec3
 

Viewers also liked

Proble, Solving & Automation
Proble, Solving & AutomationProble, Solving & Automation
Proble, Solving & Automation
Janani Satheshkumar
 
Class diagram- UML diagram
Class diagram- UML diagramClass diagram- UML diagram
Class diagram- UML diagram
Ramakant Soni
 
Use Case diagram-UML diagram-2
Use Case diagram-UML diagram-2Use Case diagram-UML diagram-2
Use Case diagram-UML diagram-2
Ramakant Soni
 
Use Case diagram-UML diagram-1
Use Case diagram-UML diagram-1Use Case diagram-UML diagram-1
Use Case diagram-UML diagram-1
Ramakant Soni
 
Activity diagram-UML diagram
Activity diagram-UML diagramActivity diagram-UML diagram
Activity diagram-UML diagram
Ramakant Soni
 
Sequence diagram- UML diagram
Sequence diagram- UML diagramSequence diagram- UML diagram
Sequence diagram- UML diagram
Ramakant Soni
 
Collaboration diagram- UML diagram
Collaboration diagram- UML diagram Collaboration diagram- UML diagram
Collaboration diagram- UML diagram
Ramakant Soni
 
Use case specification dan activity diagram [INTERNAL EDUCATIONAL PURPOSED]
Use case specification dan activity diagram [INTERNAL EDUCATIONAL PURPOSED]Use case specification dan activity diagram [INTERNAL EDUCATIONAL PURPOSED]
Use case specification dan activity diagram [INTERNAL EDUCATIONAL PURPOSED]
Theo Pratama
 
UML Diagrams- Unified Modeling Language Introduction
UML Diagrams- Unified Modeling Language IntroductionUML Diagrams- Unified Modeling Language Introduction
UML Diagrams- Unified Modeling Language Introduction
Ramakant Soni
 
Activity Diagram
Activity DiagramActivity Diagram
Activity Diagram
Ashesh R
 

Viewers also liked (10)

Proble, Solving & Automation
Proble, Solving & AutomationProble, Solving & Automation
Proble, Solving & Automation
 
Class diagram- UML diagram
Class diagram- UML diagramClass diagram- UML diagram
Class diagram- UML diagram
 
Use Case diagram-UML diagram-2
Use Case diagram-UML diagram-2Use Case diagram-UML diagram-2
Use Case diagram-UML diagram-2
 
Use Case diagram-UML diagram-1
Use Case diagram-UML diagram-1Use Case diagram-UML diagram-1
Use Case diagram-UML diagram-1
 
Activity diagram-UML diagram
Activity diagram-UML diagramActivity diagram-UML diagram
Activity diagram-UML diagram
 
Sequence diagram- UML diagram
Sequence diagram- UML diagramSequence diagram- UML diagram
Sequence diagram- UML diagram
 
Collaboration diagram- UML diagram
Collaboration diagram- UML diagram Collaboration diagram- UML diagram
Collaboration diagram- UML diagram
 
Use case specification dan activity diagram [INTERNAL EDUCATIONAL PURPOSED]
Use case specification dan activity diagram [INTERNAL EDUCATIONAL PURPOSED]Use case specification dan activity diagram [INTERNAL EDUCATIONAL PURPOSED]
Use case specification dan activity diagram [INTERNAL EDUCATIONAL PURPOSED]
 
UML Diagrams- Unified Modeling Language Introduction
UML Diagrams- Unified Modeling Language IntroductionUML Diagrams- Unified Modeling Language Introduction
UML Diagrams- Unified Modeling Language Introduction
 
Activity Diagram
Activity DiagramActivity Diagram
Activity Diagram
 

Similar to What is Algorithm - An Overview

01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
KokilaK25
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
AmayJaiswal4
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
Dr. Rajdeep Chatterjee
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)
Mumtaz Ali
 
Recurrences
RecurrencesRecurrences
module1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdfmodule1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdf
Shiwani Gupta
 
Lect-2.pptx
Lect-2.pptxLect-2.pptx
Lect-2.pptx
mrizwan38
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
Venkatesh Iyer
 
algorithm_analysis1
algorithm_analysis1algorithm_analysis1
algorithm_analysis1
Mohamed Elsayed
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
pppepito86
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.ppt
ISHANAMRITSRIVASTAVA
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
Oye Tu
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
NagendraK18
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
K Hari Shankar
 
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Priyanka Rana
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional Programming
Saurabh Singh
 
04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptx04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptx
arslanzaheer14
 
Python.pptx
Python.pptxPython.pptx
10. Recursion
10. Recursion10. Recursion
10. Recursion
Intro C# Book
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
babuk110
 

Similar to What is Algorithm - An Overview (20)

01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)
 
Recurrences
RecurrencesRecurrences
Recurrences
 
module1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdfmodule1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdf
 
Lect-2.pptx
Lect-2.pptxLect-2.pptx
Lect-2.pptx
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
algorithm_analysis1
algorithm_analysis1algorithm_analysis1
algorithm_analysis1
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.ppt
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional Programming
 
04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptx04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptx
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
10. Recursion
10. Recursion10. Recursion
10. Recursion
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
 

More from Ramakant Soni

GATE 2021 Exam Information
GATE 2021 Exam InformationGATE 2021 Exam Information
GATE 2021 Exam Information
Ramakant Soni
 
Role of Data Cleaning in Data Warehouse
Role of Data Cleaning in Data WarehouseRole of Data Cleaning in Data Warehouse
Role of Data Cleaning in Data Warehouse
Ramakant Soni
 
Internet of things
Internet of thingsInternet of things
Internet of things
Ramakant Soni
 
NOSQL- Presentation on NoSQL
NOSQL- Presentation on NoSQLNOSQL- Presentation on NoSQL
NOSQL- Presentation on NoSQL
Ramakant Soni
 
Huffman and Arithmetic coding - Performance analysis
Huffman and Arithmetic coding - Performance analysisHuffman and Arithmetic coding - Performance analysis
Huffman and Arithmetic coding - Performance analysis
Ramakant Soni
 
UML daigrams for Bank ATM system
UML daigrams for Bank ATM systemUML daigrams for Bank ATM system
UML daigrams for Bank ATM system
Ramakant Soni
 

More from Ramakant Soni (6)

GATE 2021 Exam Information
GATE 2021 Exam InformationGATE 2021 Exam Information
GATE 2021 Exam Information
 
Role of Data Cleaning in Data Warehouse
Role of Data Cleaning in Data WarehouseRole of Data Cleaning in Data Warehouse
Role of Data Cleaning in Data Warehouse
 
Internet of things
Internet of thingsInternet of things
Internet of things
 
NOSQL- Presentation on NoSQL
NOSQL- Presentation on NoSQLNOSQL- Presentation on NoSQL
NOSQL- Presentation on NoSQL
 
Huffman and Arithmetic coding - Performance analysis
Huffman and Arithmetic coding - Performance analysisHuffman and Arithmetic coding - Performance analysis
Huffman and Arithmetic coding - Performance analysis
 
UML daigrams for Bank ATM system
UML daigrams for Bank ATM systemUML daigrams for Bank ATM system
UML daigrams for Bank ATM system
 

Recently uploaded

BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 

Recently uploaded (20)

BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 

What is Algorithm - An Overview

  • 1. 1Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani Ramakant Soni Assistant Professor CS Dept., BKBIET Pilani ramakant.soni1988@gmail.com
  • 2. What is an Algorithm ? It is a step by step procedure or a set of steps to accomplish a task. “An algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values as output." Introduction to Algorithms by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein Source: www.akashiclabs.com 2Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 3. Were do we use them?  To find the best path to travel (Google Maps).  For weather forecasting.  To find structural patterns and cure diseases.  For making games that can defeat us in it.(chess)  For the processing done at the server each time we check the mail.  When we take a selfie, edit them, post them to social media and get likes.  To buy some products online and pay for it sitting at our home.  For the synchronization in the traffic lights of the whole city.  For software and techniques used to make animation movie.  Even a simple program of adding, subtracting, multiplying is an algorithm and also calculating the speed, path, fuel of a space shuttle is done by using algorithms.  And many more……….(almost everywhere!) 3Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 4. Write algorithm Compute Complexity & Optimize Code program Changes/ Modifications Source: openclassroom.stanford.edu
  • 5. Algorithm Example: Algorithm to add two numbers entered by user. Step 1: Start Step 2: Declare variables num1, num2 and sum. Step 3: Read values num1 and num2. Step 4: Add num1 and num2 and assign the result to sum. sum←num1+num2 Step 5: Display sum Step 6: Stop #include <stdio.h> int main() { int firstNo, secondNo, sum; printf ("Enter two integers: "); // Two integers entered by user is stored using scanf() function scanf("%d %d",&firstNo, &secondNo); // sum of two numbers in stored in variable sum sum = firstNo + secondNo; // Displays sum printf ("%d + %d = %d", firstNo, secondNo, sum); return 0; } Enter two integers: 12 ,11 Sum=12 + 11 = 23 5Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 6. Algorithm to find largest among three numbers. Step 1: Start Step 2: Declare variables a, b and c. Step 3: Read variables a, b and c. Step 4: If a>b If a>c Display a is the largest number. Else Display c is the largest number. Else If b>c Display b is the largest number. Else Display c is the largest number. Step 5: Stop #include <stdio.h> int main() { double n1, n2, n3; printf("Enter three numbers: "); scanf("%lf %lf %lf", &n1, &n2, &n3); if (n1>=n2) { if(n1>=n3) printf ("%.2lf is the largest number.", n1); else printf ("%.2lf is the largest number.", n3); } else { if(n2>=n3) printf ("%.2lf is the largest number.", n2); else printf ("%.2lf is the largest number.",n3); } return 0; } 6Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 7. Step 1: Start Step 2: Declare variables n, factorial and i. Step 3: Initialize variables: factorial←1 i←1 Step 4: Read value of n Step 5: Repeat the steps until i=n factorial ← factorial*i i←i+1 Step 6: Display factorial Step 7: Stop #include <stdio.h> int main() { int n, i; unsigned long long factorial = 1; printf("Enter an integer: "); scanf("%d",&n); // show error if the user enters a negative integer if (n < 0) printf("Error! Factorial of a negative number doesn't exist."); else { for(i=1; i<=n; ++i) { factorial *= i; // factorial = factorial*i; } printf("Factorial of %d = %llu", n, factorial); } return 0; } Algorithm to find the factorial of a number entered by user. Enter an integer: 10 Factorial of 10 = 10*9*8*7*6*5*4*3*2*1=3628800 7Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 8. Algorithm to find the Fibonacci series till term≤ n. Step 1: Start Step 2: Declare variables first_term, second_term and temp. Step 3: Initialize variables first_term←0 second_term←1 Step 4: Display first_term and second_term Step 5: Repeat the steps until second_term ≤ n temp ← second_term second_term ← second_term + first term first_term ← temp Display second_term Step 6: Stop #include <stdio.h> int main() { int i, n, t1 = 0, t2 = 1, nextTerm = 0; Printf ("Enter the number of terms: "); Scanf ("%d",&n); // displays the first two terms which is always 0 and 1 Printf ("Fibonacci Series: %d, %d, ", t1, t2); // i = 3 because the first two terms are already displayed for (i=3; i <= n; ++i) { nextTerm = t1 + t2; t1 = t2; t2 = nextTerm; printf ("%d ", nextTerm); } return 0; } Enter a positive integer: 100 Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 8Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 9. Algorithm Analysis In computer science, the analysis of algorithms is the determination of the amount of resources (such as time and storage) necessary to execute them. The efficiency or running time of an algorithm is stated as a function relating the input length to the number of steps (time complexity) or storage locations (space complexity). 1) Worst Case Analysis (Usually Done):In the worst case analysis, we calculate upper bound on running time of an algorithm by considering worst case (a situation where algorithm takes maximum time) 2) Average Case Analysis (Sometimes done) :In average case analysis, we take all possible inputs and calculate computing time for all of the inputs. 3) Best Case Analysis (Bogus) :In the best case analysis, we calculate lower bound on running time of an algorithm. Source: http://quiz.geeksforgeeks.org/lmns-algorithms/ 9Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 10. Asymptotic Notations • Θ Notation: The Theta notation bounds a functions from above and below, so it defines exact asymptotic behavior. Θ((g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that 0 <= c1*g(n) <= f(n) <= c2*g(n) for all n >= n0} • Big O Notation: The Big O notation defines an upper bound of an algorithm, it bounds a function only from above. O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 <= f(n) <= cg(n) for all n >= n0} • Ω Notation: The Omega notation provides an asymptotic lower bound. Ω (g(n)) = {f(n): there exist positive constants c and n0 such that 0 <= cg(n) <= f(n) for all n >= n0}. Source: http://quiz.geeksforgeeks.org/lmns-algorithms/ 10Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 11. Big O Complexity Chart Source: http://bigocheatsheet.com/ 11Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 12. Insertion Sort Statement Running Time of Each Step InsertionSort (A, n) { for i = 2 to n { c1n key = A[i] c2(n-1) j = i - 1; c3(n-1) while (j > 0) and (A[j] > key) { c4T A[j+1] = A[j] c5(T-(n-1)) j = j - 1 c6(T-(n-1)) } 0 A[j+1] = key c7(n-1) } 0 } T = t2 + t3 + … + tn where ti is number of while expression evaluations for the ith for loop iteration 12Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 13. Insertion Sort Example 13Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 14. Insertion Sort Analysis • T(n) = c1n + c2(n-1) + c3(n-1) + c4T + c5(T - (n-1)) + c6(T - (n-1)) + c7(n-1) + c8T + c9n + c10 = = O(n2) • What can T be? • Best case -- inner loop body never executed- sorted elements • ti = 1  T(n) is a linear function • Worst case -- inner loop body executed for all previous elements • ti = i  T(n) is a quadratic function 14Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 15. Merge Sort MergeSort(A, left, right) { if (left < right) { mid = floor((left + right) / 2); MergeSort (A, left, mid); MergeSort (A, mid+1, right); Merge(A, left, mid, right); // Merge() takes two sorted subarrays of A and merges them into a single sorted subarray of A } } When n ≥ 2, time for merge sort steps: Divide : Just compute mid as the average of left and right, which takes constant time i.e. Θ(1). Conquer : Recursively solve 2 sub-problems, each of size n/2, which is 2T(n/2). Combine : MERGE on an n-element subarray takes Θ(n) time 15Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 16. MERGE (A, left, mid, right ) 1. n1 ← mid − left + 1 2. n2 ← right − mid 3. Create arrays L[1 . . n1 + 1] and R[1 . . n2 + 1] 4. FOR i ← 1 TO n1 5. DO L[i] ← A[left + i − 1] 6. FOR j ← 1 TO n2 7. DO R[j] ← A[mid + j ] 8. L[n1 + 1] ← ∞ 9. R[n2 + 1] ← ∞ 10. i ← 1 11. j ← 1 12. FOR k ← left TO right 13. DO IF L[i ] ≤ R[ j] 14. THEN A[k] ← L[i] 15. i ← i + 1 16. ELSE A[k] ← R[j] 17. j ← j + 1 Merge Sort Source: http://www.personal.kent.edu 16Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 17. Analysis of Merge Sort Statement Running Time of Each Step______ MergeSort (A, left, right) T(n) { if (left < right) (1) { mid = floor((left + right) / 2); (1) MergeSort (A, left, mid); T(n/2) MergeSort (A, mid+1, right); T(n/2) Merge(A, left, mid, right); (n) // Merge() takes two sorted subarrays of A and merges them into a single sorted subarray of A }} • So T(n) = (1) when n = 1, and 2T(n/2) + (n) when n > 1 Complexity= O(n log n) 17Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 18. The Master Theorem • Given: a divide and conquer algorithm • An algorithm that divides the problem of size n into a sub-problems, each of size n/b • Let the cost of each stage (i.e., the work to divide the problem + combine solved sub-problems) be described by the function f(n) • T(n) = a*T(n/b) + f(n) 18Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 19. The Master Theorem if T(n) = a*T(n/b) + f(n) , then                                                1 0 largefor)()/( AND)( )( )( )( log)( log log log log log c nncfbnaf nnf nnf nOnf nf nn n nT a a a a a b b b b b    19Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 20. Quick Sort Quicksort(A, p, r) { if (p < r) { q = Partition(A, p, r); Quicksort(A, p, q); Quicksort(A, q+1, r); } } Another divide-and-conquer algorithm • The array A[p .. r] is partitioned into two non-empty subarrays A[p .. q] and A[q+1 ..r] Invariant: All elements in A[p .. q] are less than all elements in A[q+1..r] • The subarrays are recursively sorted by calls to quicksort Unlike merge sort, no combining step: two subarrays form an already-sorted array Actions that takes place in the partition() function: • Rearranges the subarray in place • End result: • Two subarrays • All values in first subarray  all values in second • Returns the index of the “pivot” element separating the two subarrays 20Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 21. Quick Sort Partition Pseudo code 21Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani Quicksort(A, p, r) { if (p < r) { q = Partition(A, p, r); Quicksort(A, p, q); Quicksort(A, q+1, r); } }
  • 22. Quick Sort Example & Analysis In the worst case ( unbalanced partition ): T(1) = (1) T(n) = T(n - 1) + (n) Works out to T(n) = (n2) In the best case ( balanced partition ): T(n) = 2T(n/2) + (n) Works out to T(n) = (n log n) 22Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 23. Linear Search 23Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani function find_Index (array, target) { for(var i = 0; i < array.length; ++i) { if (array[i] == target) { return i; } } return -1; } • A linear search searches an element or value from an array till the desired element or value is not found and it searches in a sequence order. • It compares the element with all the other elements given in the list and if the element is matched it returns the value index else it return -1. • Running Time: T(n)= a(n-1) + b Example: Search 5 in given data 5
  • 24. Binary Search 24Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani binary_search(A, low, high) low = 1, high = size(A) while low <= high mid = floor(low + (high-low)/2) if target == A[mid] return mid else if target < A[mid] binary_search(A, low, mid-1) else binary_search(A, mid+1, high) else “target was not found” Binary Search is an instance of divide-and-conquer paradigm. Given an ordered array of n elements, the basic idea of binary search is that for a given element we "probe" the middle element of the array. We continue in either the lower or upper segment of the array, depending on the outcome of the probe until we reached the required (given) element. Complexity Analysis: Binary Search can be accomplished in logarithmic time in the worst case , i.e., T(n) = θ(log n).
  • 25. Binary Search Example 25Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani 1 2 3 Source: http://www.csit.parkland.edu
  • 26. Algorithm to check Palindrome 26Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. function isPalindrome (text) if text is null return false left ← 0 right ← text.length - 1 while (left < right) if text[left] is not text[right] return false left ← left + 1 right ← right - 1 return true Complexity: The first function isPalindrome has a time complexity of O(n/2) which is equal to O(n) Palindrome Example: CIVIC LEVEL RADAR RACECAR MOM
  • 27. Algorithm to check Prime/ Not Prime 27Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. isPrime (int n){ int i; if (n==2) return 1; if (n % 2==0) return 0; for (i = 3; i < =sqrt(n); i++) if (n % I == 0) return 0; return 1; } Complexity: O(sqrt(n)))
  • 28. Common Data Structure Operations Source: http://bigocheatsheet.com/ 28Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 29. Array Sorting Algorithms Source: http://bigocheatsheet.com/ 29Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani
  • 30. All The Best ! 30Ramakant Soni, Asst. Professor, CS Dept., BKBIET Pilani