0% found this document useful (0 votes)
19 views103 pages

DAA-Unit-1 Students (204-25)

Uploaded by

anushkamishra545
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views103 pages

DAA-Unit-1 Students (204-25)

Uploaded by

anushkamishra545
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 103

DESIGN AND ANALYSIS OF ALGORITHMS

(DAA)

DEPARTMENT OF CSE &IT

@Dr. Shankar Thawkar-HOD


Introduction to Algorithms
▪ The word algorithm comes from the name of
Persian author “Abu Jafar Mohd. Ibn musa Al
Khawarizmi” ( 825 AD) , who wrote a text book on
Mathematics.

• Originally, “Algorisme” [old French] referred to just the Arabic number


system, but eventually it came to mean “Algorithm” as we know today.

@Dr. Shankar Thawkar-HOD


Introduction to Algorithms
▪ An Algorithm is a set of instructions to solve a particular problem.

Problem

algorithm

input “Computer” output


@Dr. Shankar Thawkar-HOD
Difference between Algorithm , Pseudocode & Program
• Algorithm :Systematic logical approach to solve any problem.It can
be written in natural language.

• Pseudocode : It is simple version of programming code that does


not require any strict programming language syntax.

• Program : it is exact code written in any particular programming


language

@Dr. Shankar Thawkar-HOD


Example of Algorithm

1. Input two positive integers, a and b.


2. If a<b, exchange a and b.
3. Divide a by b and get the remainder, r
4. If r=0, report b as the GCD of a and b
5. Replace a by b and replace b by r
6. Goto step 3

@Dr. Shankar Thawkar-HOD


Real-World Applications

Hardware design Compiler Search Engines Security

Computer Graphics AI and Robotics Satellite tracking

Machine Learning Computer


@Dr. Network
Shankar Thawkar-HOD Computational Biology
Characteristics of an Algorithm
▪ Input – it take zero or more quantity as input
▪ Output – produce at least one quantity as output
▪ Finiteness – it must terminate after finite steps
▪ Definiteness- every instruction must be clear and
unambiguous
▪ Effectiveness - every instruction must be basic

Example- 1. Input integer n


2. Divide n by 2 and get the remainder, r
3. If r=0, report n is even else n is odd
@Dr. Shankar Thawkar-HOD
Algorithm Design Techniques
▪ Divide and Conquer (DAC)
▪ Greedy method
▪ Dynamic Programming
▪ Backtracking
▪ Branch and Bound

▪ NP Problems

@Dr. Shankar Thawkar-HOD


Analysis of Algorithms

▪ A given problem can be solved using many


techniques. It means there are many ways to
solve a particular problem.

▪ So how will you decide which algorithm is


best.

@Dr. Shankar Thawkar-HOD


References
▪ Thomas H. Coreman, Charles E. Leiserson and Ronald L. Rivest, “Introduction to
Algorithms”, Printice Hall of India.
▪ E. Horowitz & S Sahni, "Fundamentals of Computer Algorithms", 3. Aho,
Hopcraft, Ullman, “The Design and Analysis of Computer Algorithms” Pearson
Education, 2008.
▪ LEE "Design & Analysis of Algorithms (POD)",McGraw Hill
▪ Richard E.Neapolitan "Foundations of Algorithms" Jones & Bartlett Learning
▪ Jon Kleinberg and Éva Tardos, Algorithm Design, Pearson, 2005.

@Dr. Shankar Thawkar-HOD


Complexity of an Algorithm
▪ Measured in two terms-
▪ Space complexity

▪ Time complexity

@Dr. Shankar Thawkar-HOD


Space Complexity
▪ It is measured in terms of the space required for the data
elements (variables and constant).

e.g.
Sum ( a, b)
{
int c;
c=a+b;
}
Space required= 3 unit ( for a, b, c)
@Dr. Shankar Thawkar-HOD
Time Complexity
▪ Time required for the execution of an Algorithm.
▪ Analysis are of two types-
▪ Priori Analysis

▪ Posterior Analysis

▪ Priori Analysis-
▪ Analyze the algorithm before execution on a machine.
▪ Use frequency count method.
▪ Time complexity of an algorithm is denoted by f(n), where n is input size.
▪ No unit for time
@Dr. Shankar Thawkar-HOD
Ex-2 Find the following algorithms-

a=b+c for i=1 to n


a=b+c
end

@Dr. Shankar Thawkar-HOD


Ex-3 consider the following algorithms-

for i=1 to n mat_mul(a,b)


for i=1 to n { int c;
a=b+c for i=1 to n
end for j=1 to n
end for k=1 to n
cij= aik* bkj
}

@Dr. Shankar Thawkar-HOD


Time Complexity
▪ Posterior Analysis- Analyze the algorithm after execution.
▪ It depends on following factors-
▪ Machine being used

▪ Language and its compiler

▪ Environment being used

▪ Unit of time is sec, ms, ns etc

@Dr. Shankar Thawkar-HOD


Best case, Average case and Worst case Analysis
• Consider a Linear search Algorithm
1. Read N of an Array A
2. Read searching element X
3. For I = 1 to N
4. If A[i]= X then display “ found X and exist”
5. End loop

Ex. -1 10 1 20 6 7 15 100 25 30

1. Best Case :
▪ It occur , if the searching element found at first position. i.e. f(n)=O(1)
▪ It indicate minimum time required for the algorithm.
@Dr. Shankar Thawkar-HOD
Best , Average and Worst case Analysis
Example- 10 1 20 6 7 15 100 25 30

2. Average case :
▪ In this, we assume that element may be found at any position. So we consider a
probability-
i.e. f(n)= 1/n + 2/n+ 3/n+……………..+n/n
= (n+1)/2= O(n)
▪ It indicate average time required for the algorithm.

3. Worst case :
▪ It occur , if the searching element is found at last position. Or does not found i.e.
f(n)=O(n)
▪ It indicate maximum time required for the algorithm.
@Dr. Shankar Thawkar-HOD
Asymptotic Notations
▪ Big oh ‘O’ Notation
▪ Omega ‘Ω’ Notation
▪ Theta ‘θ’ Notation

@Dr. Shankar Thawkar-HOD


Big oh ‘O’ Notation-
Definition-
f(n)= O(g(n)), if their exist positive constants c and n0 such that –
f(n) ≤ c g(n) ; for all n≥n0 , c is constant.

- It represent the worst case .


Example- 3n+2=O(n)

@Dr. Shankar Thawkar-HOD


Ex-2 : 10n2 + 2=O(n2) Ex-3 5n2 – 6n=O(n2)
We Know, We Know,
f(n) ≤ c g(n) f(n) ≤ c g(n)

@Dr. Shankar Thawkar-HOD


Omega ‘Ω’ Notation-
Definition-
f(n)= Ω(g(n)) if their exist positive constants c and n0 such that –
f(n) ≥ c g(n) ; for all n≥n0 , c is constant.

- It represent the best case .


Example- 3n+2= Ω(n)

@Dr. Shankar Thawkar-HOD


Theta ‘θ’ Notation-
Definition- f(n)= θ(g(n)) if their exist positive constants c1,c2 and n0 such that –
c1 g(n) ≤ f(n) ≤ c2 g(n) ; for all n≥n0

- It represent the Average case .


Ex- 5n+2= θ(n)
According to definition
c1 g(n) ≤ f(n) ≤ c2 g(n)

@Dr. Shankar Thawkar-HOD


@Dr. Shankar Thawkar-HOD
@Dr. Shankar Thawkar-HOD
Recurrence Relations
• It is used to express the complexity of an recursive algorithm.
Ex-1 Recursive algorithm to find factorial of number n
f(n)
{ if (n=0) return 1
else
return ( n * f(n-1))
}
So, Complexity is T(n)= T (n-1)+1

@Dr. Shankar Thawkar-HOD


Recurrence Relations
Ex-2 Recursive algorithm
fun (n)
{ if (n=1) return 1
else
return fun(n/2)
}
So, Complexity is T(n)= T (n/2)+c

Ex-3 Recursive algorithm


fun (n)
{ if (n=1) return 1
else
return (fun(n/2)+fun (n/2))
}
So, Complexity is T(n)= 2T (n/2)+c
@Dr. Shankar Thawkar-HOD
Recurrence Relations
Ex-4 : Recursive algorithm to generate Fibonacci series
f(n)
{ if (n ≤ 1) return 1
else
return (f (n-1)+f(n-2))
}

So, Complexity is T(n)= T (n-1)+ T (n-2)+ c


@Dr. Shankar Thawkar-HOD
Recurrence Relations
• Following methods are used to solve Recurrence relations-
▪ Master Method

▪ Iterative substitution method

▪ Recursion tree method

@Dr. Shankar Thawkar-HOD


Master Method / Master Theorem
Definition-
Let T(n)= a T(n/b)+ θ (nk logp n) where a≥1 , b>1, k ≥0 and p is a real number then

❑ Case 1 : if a>bk then T(n)= θ (nlogba )


❑ Case 2 : if a=bk
a. If p>-1 then T(n)= θ (nlogba logP+1 n)
b. If p= -1 then T(n)= θ (nlogba log log n)
c. If p< -1 then T(n)= θ (nlogba )
❑ Case 3 : a<bk
a. If p ≥ 0 then T(n)= θ (nk logP n)
b. If p<0 then T(n)= θ (nk)

@Dr. Shankar Thawkar-HOD


General form-
T(n)= a T(n/b)+ θ (nk logp n)

@Dr. Shankar Thawkar-HOD


General form-
T(n)= a T(n/b)+ θ (nk logp n)

@Dr. Shankar Thawkar-HOD


General form-
T(n)= a T(n/b)+ θ (nk logp n)

@Dr. Shankar Thawkar-HOD


General form-
T(n)= a T(n/b)+ θ (nk logp n)

@Dr. Shankar Thawkar-HOD


General form-
T(n)= a T(n/b)+ θ (nk logp n)

@Dr. Shankar Thawkar-HOD


General form-
T(n)= a T(n/b)+ θ (nk logp n)

@Dr. Shankar Thawkar-HOD


Problems based on Master Method
1. T(n) = 4T (n/2)+n General form-
T(n)= a T(n/b)+ θ (nk logp n)

Given-

Compare value of a with bk

@Dr. Shankar Thawkar-HOD


Problems based on Master Method
2. T(n) = 3T (n/2)+n2 General form-
T(n)= a T(n/b)+ θ (nk logp n)

Given-

Compare value of a with bk

@Dr. Shankar Thawkar-HOD


Problems based on Master Method
3. T(n) = 2T (n/2)+n log n General form-
T(n)= a T(n/b)+ θ (nk logp n)

Given-

Compare value of a with bk

@Dr. Shankar Thawkar-HOD


Problems based on Master Method
4. T(n) = T (n/2)+1 General form-
T(n)= a T(n/b)+ θ (nk logp n)

Given-

Compare value of a with bk

@Dr. Shankar Thawkar-HOD


Problems based on Master Method
5. T(n) = 4T (n/2)+ n2 logn General form-
T(n)= a T(n/b)+ θ (nk logp n)

Given-

Compare value of a with bk

@Dr. Shankar Thawkar-HOD


Problems based on Master Method
6. T(n) = T (2n/3)+ 1 General form-
T(n)= a T(n/b)+ θ (nk logp n)

Given-

Compare value of a with bk

@Dr. Shankar Thawkar-HOD


Thank You

@Dr. Shankar Thawkar-HOD


Iterative Substitution Method

▪ In this method, we have to expand the recurrence and express it as a summation


of terms depends only on n and initial condition.

▪ Following summation formulas are used in solving most of the problems using
iterative substitution method
𝒙𝒏+𝟏 −𝟏
𝐟𝐨𝐫 𝐱 ≠ 𝟏; σ𝒏𝒌=𝟎 𝒙𝒌 = 1+x+x2 +…… =
𝒙−𝟏

𝟏
for |x|<1 ; σ∞
𝒌=𝟎 𝒙𝒌
=
𝟏−𝒙

@Dr. Shankar Thawkar-HOD


Ex-1 . Solve the following recurrence relation using Iteration method.
T(n)= 2T(n/2)+n
➢ First we rewrite the recurrence as-
T(n)=n+2T(n/2)---------------------------- (1)

➢ Expand the recurrence in Eq. (1) as-


T(n)= n+2T(n/2)
=n+2[n/2+2T(n/4]
= n+n+4T(n/4)
=n+n+4[n/4+2T(n/8]
=n+n+n+8T(n/8)
=3n+8T(n/8)
=3n+23 T(n/23)
@Dr. Shankar Thawkar-HOD
Ex-2 . Solve the following recurrence relation using Iteration method.
T(n)= 3T(n/4)+n
➢First we rewrite the recurrence as-
T(n)=n+3T(n/4)---------------------(1)
➢ Expand the recurrence in Eq. (1) as-
T(n)=n+3T(n/4)

@Dr. Shankar Thawkar-HOD


Ex-3 . Solve the following recurrence relation using Iteration method.
T(n)= 4T(n/2)+n
➢First we rewrite the recurrence as-
T(n)=n+4T(n/2)-------------------------------------(1)

➢Expand the recurrence in Eq. (1) as-


T(n)=n+4T(n/2)

@Dr. Shankar Thawkar-HOD


Ex-4 . Solve the following recurrence relation using Iteration method.
T(n)=T(n-1)+n
➢First we rewrite the recurrence as-
T(n)= n+ T(n-1)-------------------------------------(1)

➢Expand the recurrence in Eq. (1) as-


T(n)= n+ T(n-1)

@Dr. Shankar Thawkar-HOD


Practice Problems
1. T(n)=8T(n/2)+n2 ; T(1)=1
2. T(n)=T(n/2)+n
3. T(n)= k T(n/k)+ n2 ;when T(1) =1 and k is any constant.
4. T(n)=4T(n/3)+ n2 when T(1)=1
5. T(n)= T(n-1)+ n4
Ans-
1. T(n) = (n3)
2. T(n) = (n)
3. T(n) = (n2)
4. T(n) = (n2)
5. T(n) = (n5)
@Dr. Shankar Thawkar-HOD
Thank You

@Dr. Shankar Thawkar-HOD


Recursion Tree Method
▪ It is used to visualize the working of iteration method.
Ex-1 T(n)= 2T(n/2)+n

@Dr. Shankar Thawkar-HOD


Ex-2 T(n)= T(n/3)+ T(2n/3)+n

@Dr. Shankar Thawkar-HOD


Practice Problems
1. T(n)= 3T(n/4)+ cn2
2. T(n)= 2T(n/2)+ n3
3. T(n)= T(n/2)+ 1
4. T(n)= T(n/10)+ T(9n/10)+n

Ans-
1. T(n) = (n2)
2. T(n) = (n3)
3. T(n) = (logn)
4. T(n) = (nlogn)

@Dr. Shankar Thawkar-HOD


Thank You

@Dr. Shankar Thawkar-HOD


Heap Sort

@Dr. Shankar Thawkar-HOD


Basic Concept
▪ Binary Search Tree- A complete binary tree T is said to binary search
tree , if value at any node N is greater than its left child but less than its
right child.

@Dr. Shankar Thawkar-HOD


What is Heap?
▪ Max-Heap- A binary search tree T is said to be heap (max-heap) , if
value at any node N is greater than its left child and right child.

Representation of Heap :

@Dr. Shankar Thawkar-HOD


Max-Heapify Procedure
▪ It is used to maintain the property of Heap
Max_heapify (A, i)
// A be an Array
// i is the index value
1. l= left(i) and r=right(i)
2. if l<heap_size[A] and A[l]>A[i] then
largest=l
else
largest=i
3. if r<heap_size[A] and A[r]>A[largest] then
largest=r
4. if largest ≠ i then exchange A[i] with A[largest]
5. Call Max_heapify(A, largest)

@Dr. Shankar Thawkar-HOD


.
How to Build Max-Heap?
Algo: Build-Max-Heap(A)
1. heap-size[A]=length[A]
2. For i= length[A/2] down to 1
3. max-heapify(A,i)

Build max-heap for-


Let A= < 20, 1, 5, 15, 10, 3, 6>

@Dr. Shankar Thawkar-HOD


Practice Questions

1. Is the sequence <23, 17, 14, 6, 13, 10, 1, 5, 7, 12> a max-heap?

@Dr. Shankar Thawkar-HOD


Practice Questions
2. Illustrate the operation of max-heapify(A,3) on the array A=< 27, 17, 3, 16, 10,
1,5, 12, 4, 8, 9, 0>

@Dr. Shankar Thawkar-HOD


Thank You

@Dr. Shankar Thawkar-HOD


How to perform Sorting using Heap Sort Algorithm?
▪ Heap-Sort Algorithm
Algo: Heap-Sort (A)
1. Build-Max-Heap(A)
2. For i= length[A] down to 2
3. Exchange A[1[ with A[i]
4. Call max-heap(A, 1)

Let A= < 3, 1, 10, 5, 9>

@Dr. Shankar Thawkar-HOD


Heap Sort

@Dr. Shankar Thawkar-HOD


Questions based on Heap Sort

1. Illustrate the operation of Heap-sort on array A=<25, 57, 48, 37, 12, 92, 86, 33>

2. What are the minimum and maximum number of nodes in a heap of height h.

3. Show that an n-element heap has height logn.

@Dr. Shankar Thawkar-HOD


Answers

3. Illustrate the operation of Heap-sort on array A=<25, 57, 48, 37, 12, 92, 86, 33>

@Dr. Shankar Thawkar-HOD


Answers

4. What are the minimum and maximum number of nodes in a heap of height h.

Let h be the height of the tree-

@Dr. Shankar Thawkar-HOD


Answers

5. Show that an n-element heap has height logn.

• Let h be the height of the tree and n be the maximum number of nodes then-

@Dr. Shankar Thawkar-HOD


Bubble Sort
– Let A be an array with n elements then bubble sort work as-

4 2 2 2 2 2
4 4 4 4 4 4 2 2 2
9 4 4 4 1 1
9 2 2 2 2 2 4 4 4
2 1 1 1 4 3
2 9 6 6 6 6 6 6 1
6 6 6 9 1 1 1 1 1 3 3 3 3 4
6
1 1 1 1 9 3 3 3 3 3 6 6 6 6 6
3 3 3 3 3 9 9 9
9 9 9 9 9 9 9

2 1 1 1 1
Logic for , n=6 for i= 1 to n-1
1 2 2 2 2
For i=1 , J=1 to 5 { for j=1 to n-i
3 3 3 3 3 i=2 j=1 to 4 { if (A[j]> A[j+1] then
4 4 4 4 i=3 j= 1 to 3 exchange A[j] with A[j+1]
4
6 6 6 6 i=4 j=1 to 2 }
6
9 9 i=5 j=1 to 1 } Complexity-
9 9 9 @Dr. Shankar Thawkar-HOD f(n)=O(n2)
Selection Sort
• Idea:
– Find the smallest element in the array
– Exchange it with the element in the first position
– Find the second smallest element and exchange it with
the element in the second position
– Continue until the array is sorted

@Dr. Shankar Thawkar-HOD


Example
8 4 6 9 2 3 1
1 2 3 4 9 6 8
1 4 6 9 2 3 8

1 2 6 9 4 3 8 1 2 3 4 6 9 8

1 2 3 4 6 8 9
1 2 3 9 4 6 8

1 2 3 4 6 8 9

@Dr. Shankar Thawkar-HOD


Alg.: SELECTION-SORT(A) 8 4 6 9 2 3 1
n ← length[A]
for i ← 1 to n - 1
do smallest ← i
for j ← i + 1 to n
do if A[j] < A[smallest]
then smallest ← j
exchange A[i] ↔ A[smallest]

Complexity f(n)=O(n 2
@Dr. Shankar Thawkar-HOD )
Insertion Sort

To insert 12, we need to make


room for it by moving first 36
and then 24.

@Dr. Shankar Thawkar-HOD


Insertion Sort

@Dr. Shankar Thawkar-HOD


Insertion Sort

@Dr. Shankar Thawkar-HOD


Insertion Sort

input array

5 2 4 6 1 3
at each iteration, the array is divided in two sub-arrays:
left sub-array right sub-array

sorted unsorted

@Dr. Shankar Thawkar-HOD


Insertion Sort
Alg.: INSERTION-SORT(A)
for j ← 2 to n
do key ← A[ j ]
// Insert A[ j ] into the sorted
sequence A[1 . . j -1]
i ← j-1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i←i–1

A[i + 1] ← key

➢ Insertion sort – sorts the elements in place


@Dr. Shankar Thawkar-HOD
Thank You

@Dr. Shankar Thawkar-HOD


Divide and Conquer Technique (DAC)
➢ In this technique a problem of size n is divided into smaller sub-problems. Then
sub-problems are solved independently to find the solution of original problem.

➢ Divide: Divide the problem into sub-problems


➢ Conquer: Solve these sub-problems recursively
➢ Combine : combine the solution of sub-problems to find the solution of original problem

➢ It is a bottom-up approach.
➢ The complexity of DAC algorithms is expressed as-
T(n)= aT(n/b)+f(n)

where , a= number of sub-problems, n/b = size of each sub-problem


f(n)= time required to divide and combine.
@Dr. Shankar Thawkar-HOD
Quick-Sort Algorithm
▪ It is based on DAC technique
▪ Divide- The array A[p..r] is partitioned into two sub-arrays A[p..q] & A[q+1..r] such
that each element of A[p..q] is less than or equal to “x” (pivot) and each element
of A[q+1..r] is greater than or equal to “x”
▪ Conquer – two sub-arrays A[p..q] & A[q+1..r] are sorted recursively

≤ x ≥

▪ Combine – It is trivial.

@Dr. Shankar Thawkar-HOD


Quick-Sort Algorithm
Algorithms Partition (A,p,r)
{
x=A[r] // pivot
quicksort (A, p, r)
i=p-1
{ for j=p to r-1
// A be an Array {
// p & r be a LB and UB if A[j]≤ x
if p<r then {
i=i+1
{
exchange A[i] with A[j]
q=partition (A,p,r) }}
quicksort(A,p,q) exchange A[i+1] with A[r]
quicksort(A,q+1,r) return (i+1) // value of q
}
}}
@Dr. Shankar Thawkar-HOD
Quick-Sort Algorithm

2 10 8 20 4 9 5 2 4 8 20 10 9 5

2 4 5 20 10 9 8
2 10 8 20 4 9 5
Partition (A,p,r)
{
x=A[r] // pivot
2 10 8 20 4 9 5 i=p-1
for j=p to r-1
{
if A[j]≤ x
2 10 8 20 4 9 5 {
i=i+1
exchange A[i] with A[j]
}}
2 10 8 20 4 9 5 exchange A[i+1] with A[r]
return (i+1) // value of q
}
@Dr. Shankar Thawkar-HOD
Quick-Sort Algorithm

2 4 5 20 10 9 8 Partition (A,p,r)
{
x=A[r] // pivot
i=p-1
2 4 5 20 10 9 8 for j=p to r-1
{
if A[j]≤ x
{
i=i+1
8 10 9 20 exchange A[i] with A[j]
2 4 5 }}
exchange A[i+1] with A[r]
return (i+1) // value of q
10 9 20 }

2 4
9 10

@Dr. Shankar Thawkar-HOD


Complexity of Quick-Sort Algorithm
▪ Best case : occurs when array is partitioned into two arrays of equal size.
T(n)=2T(n/2)+n
=ϴ(nlogn) (by MM)

▪ Worst case : occurs when array is partitioned such that one contain ‘0’ elements
other contain n-1 elements
T(n)=T(0)+T(n-1)+n
= ϴ (n2 ) (by Tree or iteration method)

▪ Average case : occurs when array is partitioned in the ratio n/10 : 9n/10
T(n) =T(n/10)+T(9n/10)+n
=ϴ(nlogn) (by@Dr.
Tree or iteration method)
Shankar Thawkar-HOD
Thank You

@Dr. Shankar Thawkar-HOD


Merge-Sort Algorithm
▪ It is based on DAC technique
▪ Divide- The array A[p..r] is partitioned into two sub-arrays (mid pioint).

▪ Conquer – two sub-arrays A[p..q] & A[q+1..r] are sorted recursively

▪ Combine – Merge two sorted sub-arrays.

@Dr. Shankar Thawkar-HOD


Merge-Sort Algorithm
Merge(A, p, q, r)
Algorithms
1 n1  q – p + 1
2 n2  r – q
MergeSort (A, p, r) 3 for i  1 to n1
// sort A[p..r] by divide & conquer 4 do L[i]  A[p + i – 1]
5 for j  1 to n2
6 do R[j]  A[q + j]
1 if p < r 7 L[n1+1]  
2 then q  (p+r)/2 8 R[n2+1]  
9 i1
3 MergeSort (A, p, q) 10 j1
4 MergeSort (A, q+1, r) 11 for k p to r
5 Merge (A, p, q, r) 12 do if L[i]  R[j]
13 then A[k]  L[i]
// merges A[p..q] with A[q+1..r] 14 ii+1
15 else A[k]  R[j]
16 jj+1
@Dr. Shankar Thawkar-HOD
Working of Merge-Sort Algorithm

12 10 8 2 40 9 15 2 8 9 10 12 15 40

12 10 8 2 40 9 15 2 8 10 12 9 15 40

12 10 8 2 40 9 15 10 12 2 8 9 40 15

12 10 8 2 40 9 12 10 8 2 40 9

@Dr. Shankar Thawkar-HOD


Complexity of Merge-Sort Algorithm
▪ The complexity of merge sort is give by

T(n)=2T(n/2)+n
= (nlogn) [ by Master method]

@Dr. Shankar Thawkar-HOD


Thank You

@Dr. Shankar Thawkar-HOD


Binary search Algorithm
▪ It is based on DAC technique

▪ Divide – Divide the given array A[p..r] at mid position

A[p..mid-1] Mid position A[mid+1..r]

▪ Conquer- recursively search 1 sub-array


▪ Combine- It is trivial

▪ Requirement –
▪ Array must be in sorted order.

@Dr. Shankar Thawkar-HOD


Binary search Algorithm
Binary_search(A,x,p,r) x= 50 5 10 15 20 25 30 40 50
//A be an array
// x be the searching element Call Binary_search(A, x, 1, 8)

//p and r be the LB and UB & Compare A[mid]=x, lies in 2nd half

Call Binary_search(A, x, 5, 8)
1. If p>r then return -1 // not found
25 30 40 50
2. mid=(p+r)/2
3. If A[mid]=x then Compare A[mid]=x
4. return mid
5. else Call Binary_search(A, x, 7, 8)
6. If x<A[mid] then
7. binary_search (A,x,p,mid-1) 40 50
8. else & Compare A[mid]=x , lies in 2nd half
9. binary_search (A,x,mid+1, r) Call Binary_search(A, x, 7, 8)

50
@Dr. Shankar Thawkar-HOD
Compare A[mid]=x , Found
x= 2
x= 10 5 10 15 20 25 30 40 50
5 10 15 20 25 30 40 50
Call Binary_search(A, x, 1, 8)
Call Binary_search(A, x, 1, 8)
Compare A[mid]=x , x lies in 1st half
Call Binary_search(A, x, 1, 3) Compare A[mid]=x , x lies in 1st half

5 10 15 Call Binary_search(A, x, 1, 3)

Call Binary_search(A, x, 1, 2) 5 10 15
Compare A[mid]=x , x found at 2nd position , Compare A[mid]=x , x lies in 1st half
Call Binary_search(A, x, 1, 1)

Compare A[mid]=x
Call Binary_search(A, x, 1, 0)
@Dr. Shankar Thawkar-HOD Since, LB>UB , So return -1
Complexity of Binary search Algorithm
▪ The complexity of binary search is give by

T(n)=T(n/2)+1
= (logn) [ by Master method]

@Dr. Shankar Thawkar-HOD


Thank You

@Dr. Shankar Thawkar-HOD


Sorting in Linear time
▪ These algorithms takes linear time i.e. O(n)
▪ All these algorithms are non-comparison based algorithms.
▪ Linear time algorithms are -
1. Counting Sort
2. Radix sort
3. Bucket Sort

@Dr. Shankar Thawkar-HOD


Counting Sort Algorithm
▪ This method assumes that each of the n input element is an integer in the range
0 to k. The algorithm will take O(n) time when k=n.

▪ The basic idea of counting sort is to determine, for each element x , the number
of elements less than x. This information is used to place the element x directly to
its correct place.

▪ For example if there are 17 elements less than x then x must be placed at position
18.

@Dr. Shankar Thawkar-HOD


Counting Sort Algorithm
Counting_sort(A,B,k)
1. For i=0 to k // initialize 2 5 3 0 2 3 0 2
C[i]=0

2. For j=1 to n // frequency count


C[A[j]]= C[A[j]]+1

3. For i=1 to k // find position


C[i]=C[i]+C[i-1]

4. For j=n to 1 // Sorting


B[C[A[j]]]=A[j]
C[A[j]]=C[A[j]]-1

Complexity= @Dr. Shankar Thawkar-HOD


Practice Problems
▪ Illustrate the operation of counting sort on following array-

6 0 2 0 1 3 4 6 1 3 2

@Dr. Shankar Thawkar-HOD


Counting Sort Algorithm
▪ Stable Sort –
▪ Count sort is a stable sort method.
▪ Stable sort preserves the input order among equal elements in output.
▪ Example- Counting Sort, Bubble Sort, Merge Sort, Insertion Sort etc
Example-

@Dr. Shankar Thawkar-HOD


Radix Sort Algorithm
▪ Suppose we have different types of numbers like 2-digit, 3-digit or d-digit.
Example - 1234

▪ So, radix sort perform sorting on ‘n’ numbers of d-digits where LSD is sorted
first then MSD is sorted

329
768
423
121
190
@Dr. Shankar Thawkar-HOD
Radix Sort Algorithm
Algorithm-

Radix_sort(A,d)
1. For j=1 to d do
use stable sort to array A on digit I

Complexity- if we use counting sort then

f(n)= d(n+k)
so, for k=n

f(n)= O(n)

@Dr. Shankar Thawkar-HOD


Bucket Sort Algorithm
▪ Let A be an array with n elements such that , 0 ≤ A[i] ≤1. An Array A contain all the
elements in the range 0 to 1. The algorithm uses auxiliary array B to perform
sorting. Mapping-
.79
B[i]= n*A[i]
.17
.39
.26
.72
.94
.21
.22
.23
.68 @Dr. Shankar Thawkar-HOD

You might also like