SlideShare une entreprise Scribd logo
1  sur  30
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

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 algoritmosluzenith_g
 
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
 
Linear Convolution using Matlab Code
Linear Convolution  using Matlab CodeLinear Convolution  using Matlab Code
Linear Convolution using Matlab CodeBharti Airtel Ltd.
 
Computer notes - Sorting
Computer notes  - SortingComputer notes  - Sorting
Computer notes - Sortingecomputernotes
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysisSoujanya V
 
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 analysisjayavignesh86
 
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_18012013Kurmendra 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 algortihmSajid Marwat
 
multi threaded and distributed algorithms
multi threaded and distributed algorithms multi threaded and distributed algorithms
multi threaded and distributed algorithms Dr Shashikant Athawale
 

Tendances (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
 

En vedette

Class diagram- UML diagram
Class diagram- UML diagramClass diagram- UML diagram
Class diagram- UML diagramRamakant Soni
 
Use Case diagram-UML diagram-2
Use Case diagram-UML diagram-2Use Case diagram-UML diagram-2
Use Case diagram-UML diagram-2Ramakant Soni
 
Use Case diagram-UML diagram-1
Use Case diagram-UML diagram-1Use Case diagram-UML diagram-1
Use Case diagram-UML diagram-1Ramakant Soni
 
Activity diagram-UML diagram
Activity diagram-UML diagramActivity diagram-UML diagram
Activity diagram-UML diagramRamakant Soni
 
Sequence diagram- UML diagram
Sequence diagram- UML diagramSequence diagram- UML diagram
Sequence diagram- UML diagramRamakant 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 IntroductionRamakant Soni
 
Activity Diagram
Activity DiagramActivity Diagram
Activity DiagramAshesh R
 

En vedette (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
 

Similaire à What is Algorithm - An Overview

01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptxKokilaK25
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfAmayJaiswal4
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysisDr. 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
 
module1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdfmodule1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdfShiwani Gupta
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to AlgorithmsVenkatesh Iyer
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
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.pptISHANAMRITSRIVASTAVA
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programmingOye Tu
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsNagendraK18
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithmK 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/ThetaPriyanka Rana
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingSaurabh 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_.pptxarslanzaheer14
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematicalbabuk110
 

Similaire à 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
 

Plus de Ramakant Soni

GATE 2021 Exam Information
GATE 2021 Exam InformationGATE 2021 Exam Information
GATE 2021 Exam InformationRamakant 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 WarehouseRamakant Soni
 
NOSQL- Presentation on NoSQL
NOSQL- Presentation on NoSQLNOSQL- Presentation on NoSQL
NOSQL- Presentation on NoSQLRamakant Soni
 
Huffman and Arithmetic coding - Performance analysis
Huffman and Arithmetic coding - Performance analysisHuffman and Arithmetic coding - Performance analysis
Huffman and Arithmetic coding - Performance analysisRamakant Soni
 
UML daigrams for Bank ATM system
UML daigrams for Bank ATM systemUML daigrams for Bank ATM system
UML daigrams for Bank ATM systemRamakant Soni
 

Plus de 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
 

Dernier

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 

Dernier (20)

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 

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