0% found this document useful (0 votes)
22 views167 pages

UNIT 1 daa

Uploaded by

Sm
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)
22 views167 pages

UNIT 1 daa

Uploaded by

Sm
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/ 167

Noida Institute of Engineering and Technology, Greater

Noida

Introduction to Algorithm
and Asymptotic Notations

Unit: 1

Design & Analysis of Algorithms


Ankita Sharma
ACSE0401
Assistant Professor
CSE.
CSE 5TH SEM

Ankita Sharma ACSE0401 DAA Unit I


1
9/28/24
Evaluation Scheme

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 2


Syllabus

Ankita Sharma ACSE0401 DAA Unit I


9/28/24 3
Branch wise Application

• First, we will start with the internet which is very much important for our daily life and we cannot
even imagine our life without the internet and it is the outcome of clever and creative algorithms.
Numerous sites on the internet can operate and falsify this huge number of data only with the help of
these algorithms.

• The everyday electronic commerce activities are massively subject to our data, for example, credit or
debit card numbers, passwords, OTPs, and many more. The centre technologies used incorporate
public-key cryptocurrency and digital signatures which depend on mathematical algorithms.

• Even an application that doesn't need algorithm content at the application level depends vigorously on
the algorithm as the application relies upon hardware, GUI, networking, or object direction and all of
these create a substantial use of algorithms.

• There are some other vital use cases where the algorithm has been used such as if we watch any video
on YouTube then next time we will get related-type advice as recommended videos for us.

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 4


Course Objective

Upon completion of this course, students will be able to do the


following:

• Knowledge of basic principles of algorithm design and anlysis


• To Apply different problem solving approaches for advanced data
structure.
• To apply divide and conquer method for solving merge sort , quick
sort, matrix multiplication and Greedy Algorithm for solving
different Graph Problem.
• Apply different optimization techniques like dynamic programming
, backtracking and Branch & Bound to solve the complex problem.
• Synthesize efficient algorithms in common engineering design
situations.

Ankita Sharma ACSE0401 DAA Unit I


9/28/24 5
CO-PO and PSO Mapping

Design and Analysis of Algorithm (ACSE-0401)

CO.K PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12

ACSE0401.1 3 3 3 3 2 2 1 - 1 - 2 2

ACSE0401.2 3 3 3 3 2 2 1 - 1 1 2 2

ACSE0401.3 3 3 3 3 3 2 2 - 2 1 2 3

ACSE0401.4 3 3 3 3 3 2 2 1 2 1 2 3

ACSE0401.5 3 3 3 3 3 2 2 1 2 1 2 2

Average 3 3 3 3 2.6 2 1.6 0.4 1.6 0.8 2 2.2

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 6


Program Educational Objectives(PEOs)

PEO1: To have an excellent scientific and engineering breadth so as to


comprehend, analyze, design and provide sustainable solutions for
real-life problems using state-of-the-art technologies.
PEO2:To have a successful career in industries, to pursue higher studies or
to support enterpreneurial endeavors and to face global challenges.
PEO3:To have an effective communication skills, professional attitude,
ethical values and a desire to learn specific knowledge in emerging
trends, technologies for research, innovation and product
development and contribution to society.
PEO4: To have life-long learning for up-skilling and re-skilling for
successful professional career as engineer, scientist, enterpreneur
and bureaucrat for betterment of society

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 7


End Semester Question Paper Template

B TECH
(SEM-V) THEORY EXAMINATION 20__-20__
DESIGN AND ANALYSIS OF ALGORITHMS
Time: 3 Hours Total
Marks: 100
Note: 1. Attempt all Sections. If require any missing data; then choose
suitably.
SECTION A
1.Q.No.
Attempt all questions in brief.
Question Marks 2 COx 10
1 = 20 2
2 2
. .
10 2

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 8


End Semester Question Paper Templates

SECTION B
2. Attempt any three of the following: 3 x 10 = 30

Q.No. Question Mark CO


s
1 10
2 10
. .
5 SECTION C 10
3. Attempt any one part of the following: 1 x 10 = 10
Q.No. Question Marks CO

1 10
2 10
Ankita9/28/24
Sharma ACSE0401 DAA Unit I 9
End Semester Question Paper Templates
4. Attempt any one part of the following: 1 x 10 = 10

Q.No. Question Marks CO

1 10
2 10
5. Attempt any one part of the following: 1 x 10 = 10
Q.No. Question Marks CO
1 10
2 10
6. Attempt any one part of the following: 1 x 10 = 10
Q.No. Question Mark CO
s
1 10
2 10

9/28/24
Ankita Sharma ACSE0401 DAA Unit I 10
Prerequisite and Recap

Prerequisite
• Basic concept of c programming language.
• Concept of stack, queue and link list.

Recap
• Flow Chart
• Algorithm

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 11


Introduction About Subject With Videos

• The word Algorithm means “a process or set of rules to be followed in calculations


or other problem-solving operations”. Therefore Algorithm refers to a set of
rules/instructions that step-by-step define how a work is to be executed upon in
order to get the expected results.

• https://youtu.be/6hfOvs8pY1k

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 12


Unit Content

• Introduction to Algorithm
• Characteristics of Algorithm
.
• Analysis of Algorithm
• Asymptotic Notations
• Recurrence Relation
• Sorting and order Statistics
• Shell sort,
• Heap sort,
• Sorting in linear time

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 13


Unit Objective

.
• This is an introductory chapter of Design & Analysis of Algorithm covering
the concept, importance and characteristics of algorithms. The complexity
and its calculation has been explained. Further, recursion and different
methodologies to solve them have also been provided.

• In other part of unit different sorting algorithm – Insertion, Heap sort,


Quick sort, bucket sort, Radix Sort and Counting sort have been discussed
along with their complexities

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 14


Topic Objective

• The objective of this topic is to make students understand


about
• Need of algorithm
• Problem statement
• Why study algorithm
• Characteristics of Algorithm

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 15


Introduction to the Algorithm(CO1)
• 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 in a finite amount of time.

Set of rules to
obtain the
INPUT expected output OUTPUT
from given
input

Algorithm

• An algorithm is thus a sequence of computational steps that transform


the input into the output.

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 16


Why Algorithm is Important ?

• Algorithm allow students to break down problems and conceptualize


solutions in terms of discrete steps (Expanded form).

• Algorithm are used to find best possible way to solve a problem .

• They help to automate processes and make them more reliable,


faster, and easier to perform.

• Algorithms also enable computers to perform tasks that would be


difficult or impossible for humans to do manually.

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 17


Characteristics of Algorithm

• Correctness

• Efficiency
• Simplicity

• Generality

• Non Ambiguity

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 18


Criteria of Algorithm

All algorithms must satisfy following criteria-:

– Input
– Output
– Definiteness
– Finiteness
– Effectiveness

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 19


Algorithm

• An algorithm is a set of steps of operations to solve a problem.

• An algorithm is an efficient method that can be expressed within


finite amount of time and space.

• Algorithm is independent from any programming languages.

• Algorithm design include creating an efficient algorithm to solve a


problem in an efficient way using minimum time and space.

• Time is directly proportional to the number of operations on a


program.

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 20


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

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 21


Difference between Algorithm , Pseudocode & Program

example : Linear Search


Algorithm- 1. Start from left elemnt of arr[] and one by one compare X with
each element of arr[].
2. if X match , return index of element .
3. else return -1.

Pseudocode –
Function Lsearch(list,X)
for index ß 0 to length(list)
if list[index]==X then
return index
END If
END LOOP
return -1
END Function

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 22


Analysis of Algorithm(CO1)- Objective

The objective of this topic is to make students understand about

• Space Complexity
• Time Complexity
• Best Case
• Worst Case
• Average Case

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 23


Analysis of Algorithm(CO1)- Objective

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 24


Analysis of Algorithm(CO1)

How to measure time

Count total number of fundamental operation in program and this


total will give the rough estimate of the running time in terms of
input size.

• Best Case
The minimum number of steps taken on any instance of size n.

• Average Case
An average number of steps taken on any instance of size n.

• Worst Case
The maximum number of steps taken on any instance of size n.
9/28/24 Ankita Sharma ACSE0401 DAA Unit I 25
Running time for Algorithm
Algorithm Cost No. of times
Sum(A,n)
{
s =0 C1 1
for i=1 to n do C2 n+1
s = s+A[i] C3 n
return s C4 1
}

Total Running Time = c1 + c2(n+1)+c3(n) + c4


T(n) = Linear function

Megha Gupta ACSE0401 DAA


9/28/24 26
Unit I
Running time for Algorithm

Algorithm Cost No. of times


For each row i from 1 to rows(A): c1 rows(A)+1
rows(A)*(columns(A)+1)
For each column j from 1 to columns(A): c2

C[i][j] = A[i][j] + B[i][j] c3 rows(A) * columns(A)

Megha Gupta ACSE0401 DAA


9/28/24 27
Unit I
Running time for Algorithm

Algorithm Cost No. of times


DAA(n)
{
while(n>1)
C1 log2(n)+1
{
n=n/2 C2 log2(n)
print(n)
} c3 log2(n)
}

Megha Gupta ACSE0401 DAA


9/28/24 28
Unit I
Analysis of Algorithm(CO1)

Let’s take an example to understand this better –

This is Constant Time Complexity.


The above statement is only printed one time. Thus the time taken by
the algorithm is constant.

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 29


Analysis of Algorithm(CO1)

This is Linear Time Complexity


In the above code, the size of the input is taken as 5, thus the algorithm
is executed 5 times.
9/28/24 Ankita Sharma ACSE0401 DAA Unit I 30
Analysis of Algorithm(CO1)

• The time taken by the algorithm will not be constant as the above
code contains a ‘for loop’.

• From this, we can conclude that if the statement of an algorithm


has only been executed once, the time taken will always remain
constant.

• If the statement is in a for loop, the time taken by an algorithm to


execute the statement increases as the size of the input increases.

• If the algorithm contains nested loops or a combination of both “ a


single executed statement and a loop statement” , the time is taken
by the algorithm will increase according to the number of times
each statement is executed
9/28/24 Ankita Sharma ACSE0401 DAA Unit I 31
Analysis of Algorithm(CO1)

This is Quadratic Time Complexity

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 32


Analysis of Algorithm(CO1)

– The term "analysis of algorithms" was coined by Donald


Knuth.

– To estimate the complexity function for arbitrarily large input.

– Analysis of algorithms is the determination of the amount of


time and space resources required to execute it.

– The efficiency or running time of an algorithm is stated as a


function relating the input length to the number of steps, known
as time complexity, or volume of memory, known as space
complexity.

– The main concern of analysis of algorithms is the required time


or performance

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 33


Complexity of Algorithm(CO1)

Time Complexity : RULES FOR EXAMPLES


1.SINGLE STATEMENT
c = a+b
Total Time complexity= O (1)
2. LOOP
For i ß 1 to n
s ß s+ a[i]
Total Time complexity =O(n)
3.Nested Loop
for iß 1 to m
for j ß 1 to m
s ß s +a [i]
Total Time complexity =O(m2)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 34


Complexity of Algorithm(CO1)

4. Consecutive Statement = P1 ,P2 //Consider previous 2 statements p1 and


p2 //
Total Time Complexity = O(n2)

5.If else Total Time Complexity = O(n2)

6. While loop
while (n > 0)
{ i ß i+1 ;
nß n/2;
} Total Time Complexity = O(log n )
###RECURSION :fact(n)
if(n<=1)
return 1
else
return n*fact(n-1)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 35


Asymptotic Notations(CO1)- Objective

The objective of this topic is to make students understand about

• Five Asymptotic notations


§ Big Theta Notation
§ Big Oh Notation
§ Big Omega Notation
§ Small Oh Notation
§ Small Omega Notation

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 36


Asymptotic Notations(CO1)- Objective

• Asymptotic notation are mathematical tools to represent time


complexity of algorithm for asymptotic analysis.

• The main idea of asymptotic analysis is to have a measure of


efficiency of algorithms that doesn’t depend on machine specific
constants and doesn’t require algorithms to be implemented and
time taken by programs to be compare d.

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 37


Asymptotic Notations (CO1)

The asymptotic running time of an algorithm are defined in terms of


functions whose domains are the set of natural numbers. The
following are the types of asymptotic notation

§ Theta Notation
§ Big Oh Notation
§ Big Omega Notation
§ Small Oh Notation
§ Small Omega Notation

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 38


Big Oh Notation

The function f(n) = O(g(n)) (read as “ f of n is big oh of g of n”) if there


exist positive constants c and n0 such that
f(n)≤cg(n) for all n≥ n0

t
i
m
e

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 39


Big Oh Notation

Example

Let us consider a given function,

f(n)=2n2 + n

Considering g(n)=n2 //at most

f(n)⩽3.g(n)
n ⩽ n2 for all the values of n>=1.
(you can prove that after n_o value of f(n) does not fluctuate
by putting value of n 1 or greater )

Hence, the complexity of f(n) can be represented as


9/28/24
O(g(n)), i.e. O(n
Ankita Sharma
2)
ACSE0401 DAA Unit I 40
Big Omega Notation

The function f(n) = Ω(g(n)) (read as “ f of n is Ω of g of n”) if there


exist positive constants c and n0 such that
cg(n)≤ f(n) for all n≥ n0

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 41


Big Omega Notation
Example

Let us consider a given function,

f(n)=2n2 + n

Considering g(n)=n2 //greatest lower bound we choose


(choose value of c carefully and we have to choose closest
value )
f(n)>=2.g(n)
for all the values of n>=0 this condition will be true.

Hence, the complexity of f(n) can be represented as


Ω (g(n)), i.e. Ω(n2)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 42


Theta Notation

We use ‚Θ notation for asymptotically tight bounds


The function f(n) = Θ(g(n)) (read as “ f of n is theta of g of n”) if there
exist positive constants c1 and c2 and n0 such that
c1g(n)≤f(n)≤c2g(n) for all n≥ n0

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 43


Theta Notation

Example

Let us consider a given function,

f(n)=2n2 + n

Considering g(n)=n2,

2.g(n)⩽f(n)⩽3.g(n) for all the large values of n.

Hence, the complexity of f(n) can be represented as


θ(g(n)), i.e. θ(n2)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 44


Small Oh Notation

• The asymptotic upper bound big oh provided by O-notation may


or may not be asymptotically tight.

• We use o-notation to denote an upper bound that is not


asymptotically tight.
f(n) = o(g(n)) if f(n)< c(g(n)) for all n≥ n0 and c>0

• f(n) becomes insignificant relative to g(n) as n approaches infinity:


lim [f(n) / g(n)] = 0
n®¥

• g(n) is an upper bound for f(n) that is not asymptotically tight.

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 45


Small Oh Notation

Example

Let us consider a given function,

f(n)=4.n3+10.n2+5.n+1

Considering g(n)=n4; // g(n) can’t be equal to f(n) , can only


be greater
lim (4.n3+10.n2+5.n+1/ n4 )=0
n→∞

Hence, the complexity of f(n) can be represented as


o(g(n)), i.e. o(n4)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 46


Small Omega Notation

• lower bound that is not asymptotically tight


f(n) = ω(g(n)) if c(g(n))< f(n) for all n≥ n0

• f(n) becomes arbitrarily large relative to g(n) as n approaches


infinity:
lim [f(n) / g(n)] = ¥.
n®¥

• g(n) is a lower bound for f(n) that is not asymptotically tight.

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 47


Small Omega Notation
Example

Let us consider a given function,

f(n)=4.n3+10.n2+5.n+1

Considering g(n)=n2, // g(n) can’t be equal to f(n) , can only be


smaller than f(n)
lim (4.n3+10.n2+5.n+1/ n2 )=0
n→∞

Hence, the complexity of f(n) can be represented as


ω (g(n)), i.e. ω(n4)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 48


Algorithm Time Complexity

Time
Complexity
Comparison
Amortized analysis

• Amortized analysis is a method of analyzing algorithms that can


help us determine an upper bound on the complexity of an
algorithm.

• This is particular useful when analyzing operations on data


structures, when they they involve slow, rarely occurring
operations and fast, more common operations.

• With this disparity between each operations’ complexity, it is


difficult to get a tight bound on the overall complexity of a
sequence of operations using worst-case analysis.

• Amortized analysis provides us with a way of averaging the slow


and fast operations together to obtain a tight upper bound on the
overall algorithm runtime.
9/28/24 Ankita Sharma ACSE0401 DAA Unit I 50
Amortized analysis

• In simple terms we can say , In Amortized Analysis, we analyze a


sequence of operations and guarantee a worst-case average time
that is lower than the worst-case time of a particularly
expensive operation.

• The idea is to spread the cost of these expensive operations over


multiple operations, so that the average cost of each operation is
constant or less.

• The key idea behind amortized analysis is to spread the cost of


an expensive operation over several operations. For example,
consider a dynamic array data structure that is resized when it runs
out of space. The cost of resizing the array is expensive, but it can
be amortized over several insertions into the array, so that the
average time complexity of an insertion operation is constant.
9/28/24 Ankita Sharma ACSE0401 DAA Unit I 51
Amortized analysis

• Amortized analysis is useful for designing efficient algorithms for


data structures such as dynamic arrays, priority queues, and
disjoint-set data structures.

• It provides a guarantee that the average-case , time complexity of


an operation is constant, even if some operations may be expensive.

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 52


Amortized analysis
• Amortized Cost in Dynamic Array:
1. Cheap Operations (adding elements): Most of the time,
adding an element is a simple and quick operation, costing,
let's say, 1 coin.
2. Expensive Operation (resizing): Occasionally, when the
array is full, we need to resize it, which may cost more, say,
5 coins.
• Now, if you look at a sequence of, let's say, 10 operations,
and only once you had to resize, the total cost might be
something like 10 (cheap) + 5 (expensive) = 15 coins.
• The amortized cost per operation would then be 15 / 11 =
1.36 coins on average.

Megha Gupta ACSE0401 DAA


9/28/24 53
Unit I
Recursion (CO1)
• The process in which a function calls itself directly or indirectly is called
recursion and the corresponding function is called as recursive function.

• Using recursive algorithm, certain problems can be solved quite easily.

• Examples of such problems are Towers of Hanoi


(TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.

• The running time of recursive algorithms is represented by an equation


that describes a function in terms of its value on smaller functions. For
eg.

C n=1
T(n) = {
2T(n/2) +cn n>1

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 54


Methods of solving Recurrences

A recurrence is an equation or inequality that describes a function in


terms of its values on smaller inputs. To solve a Recurrence Relation
means to obtain a function defined on the natural numbers that satisfy
the recurrence.
For Example, the Worst Case Running Time T(n) of the MERGE SORT
Procedures is described by the recurrence.

There are four methods for solving Recurrence:


Substitution Method
Iteration Method
Recursion Tree Method
Master Method

Reference video :
https://www.youtube.com/watch?v=HhT1VueqQTo

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 55


Master Method

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 56


Master Method

Questions done in class

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 57


Master Method

Master method

• Master Method is a direct way to get the solution.

• The master method applies to recurrences of the form


T(n) = a T(n/b) + f (n) ,
where a ³ 1, b > 1, and f is asymptotically positive.

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 58


Master Method Example Questions

• There are three cases:


• Case I f (n) = O(nlogba – e) for some constant e > 0.
f (n) grows polynomially slower than nlogba (by an ne factor).
Solution: T(n) = Q(nlogba)

Example
. T(n) = 4T(n/2) + n
Here a = 4, b = 2 Þ nlogba = n2; f (n) = n.
CASE 1: f (n) = O(n2 – e) for e = 1.
\ T(n) = Q(n2).

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 59


Master Method

• Case II f (n) = Q(nlogba lgkn) for some constant k ³ 0.


f (n) and nlogba grow at similar rates.
Solution: T(n) = Q(nlogba lgk+1n) .

Example

T(n) = 4T(n/2) + n2
Here a = 4, b = 2 Þ nlogba = n2; f (n) = n2.
CASE 2: f (n) = Q(n2lg0n), that is, k = 0.
\ T(n) = Q(n2lg n).

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 60


Master Method

Case III f (n) = W(nlogba + e) for some constant e > 0.


f (n) grows polynomials faster than nlogba (by an ne factor),
and f (n) satisfies the regularity condition that a f (n/b) £ c f (n)
for some constant c < 1.
Solution: T(n) = Q( f (n) ) .

Example
T(n) = 4T(n/2) + n3
a = 4, b = 2 Þ nlogba = n2; f (n) = n3.
CASE 3: f (n) = W(n2 + e) for e = 1 and 4(cn/2)3 £ cn3 for c =
1/2.
\ T(n) = Q(n3).

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 61


Megha Gupta ACSE0401 DAA
9/28/24 62
Unit I
Megha Gupta ACSE0401 DAA
9/28/24 63
Unit I
Master theorem

Megha Gupta ACSE0401 DAA


9/28/24 64
Unit I
Example of Master Method

Examples of some standard algorithms whose time complexity


can be evaluated using Master Method

Merge Sort: T(n) = 2T(n/2) + Θ(n). It falls in case 2 as c is 1 and


Logba] is also 1. So the solution is Θ(n Logn)

Binary Search: T(n) = T(n/2) + Θ(1). It also falls in case 2 as c is 0 and


Logba is also 0. So the solution is Θ(Logn)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 65


Questions done in class

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 66


Substitution Method

Megha Gupta ACSE0401 DAA


9/28/24 67
Unit I
Substitution Method

Megha Gupta ACSE0401 DAA


9/28/24 68
Unit I
Substitution Method

Megha Gupta ACSE0401 DAA


9/28/24 69
Unit I
Substitution Method

Megha Gupta ACSE0401 DAA


9/28/24 70
Unit I
Substitution Method

Megha Gupta ACSE0401 DAA


9/28/24 71
Unit I
Substitution Method

Megha Gupta ACSE0401 DAA


9/28/24 72
Unit I
9/28/24 Ankita Sharma ACSE0401 DAA Unit I 73
Question 1: T(n) = 2T(n/2) + c

Solution :

Step 1: Draw a recursive tree

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 74


• Step 2: Calculate the work done or cost at each level and count total no of
levels in recursion tree

• Count the total number of levels –


Choose the longest path from root node to leaf node.

n/20 → n/21 → n/22 → ………→ n/2k

Ankita Sharma ACSE0401 DAA


9/28/24 75
Unit I
• Size of problem at last level = n/2k
• At last level size of problem becomes 1
n/2k = 1
2k = n
k = log2(n)

Total no of levels in recursive tree = k +1 = log2(n) + 1


Step 3: Count total number of nodes in the last level and calculate cost of last
level
• No. of nodes at level 0 = 20 = 1
• No. of nodes at level 1 = 21 = 2

• No. of nodes at level log2(n) = 2log2(n) = nlog2(2) = n


• Cost of sub problems at level log2(n) (last level) = n * T(1) =n* 1=n

Ankita Sharma ACSE0401 DAA


9/28/24 76
Unit I
Step 4: Sum up the cost all the levels in recursive tree

• T(n) = c + 2c + 4c + —- + (no. of levels-1) times + last level cost


• = c + 2c + 4c + —- + log2(n) times + Θ(n)
• = c(1 + 2 + 4 + —- + log2(n) times) + Θ(n)
• 1 + 2 + 4 + —– + log2(n) times –> 20 + 21 + 22 + —– + log2(n) times –>
Geometric Progression(G.P.)
• = c(n) + Θ(n)

Thus, T(n) = Θ(n)

Ankita Sharma ACSE0401 DAA


9/28/24 77
Unit I
Ankita Sharma ACSE0401 DAA
9/28/24 78
Unit I
Ankita Sharma ACSE0401 DAA
9/28/24 79
Unit I
Iteration Method
T(N) = T(N-1) + 1, T(1) = 1
Solution:
Here T(N) = T(N-1) + 1-----------eq.1

first find value of T(N-1) = T(N-2) + 1.

Place T(N-1) in eq.1

T(N) = (T(N-2) + 1 ) + 1
T(N) = T(N-2) + 2----------------eq. 2
Now find T(N-2) and place in eq.2
T(N) = T(N-3) + 3
……………………..
……………………..

From the above pattern, we get T(N) = T(N- (N-1)) + (N-1)

Thus T(N) = T(1) + (N-1) and since T(1)=1, so we place value of T(1)
Thus we get , T(N)=O(N) .

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 80


Practice Questions :
1. Find the time complexity for the following recurrence relation using
Iteration Method
• T(n) = T(n-1) + n, T(1) = 1

2. Using the recurrence for the binary search given below, find the time
complexity of binary search:
• T(n) = T(n/2) + 1 , T(1) = 1

3. Find an Asymptotic bound on T, using Substitution Method.


• T(n)= 2T(n/2) + n , n>1

4. Consider the recurrence relation: T(n) = T(n/3) + T(2n/3) + n , solve


using Recursion tree method.

5. Solve the following recurrence relation using Master’s theorem-


• T(n) = 3T(n/2) + n2

Ankita Sharma ACSE0401 DAA


9/28/24 81
Unit I
Sorting and Order Statistics

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 82


Insertion Sorting

Insertion sort is a simple sorting algorithm that works similarly to the way
you sort playing cards in your hands.

The array is virtually split into a sorted and an unsorted part. Values from the
unsorted part are picked and placed in the correct position in the sorted part.

Complexity Analysis of Insertion Sort:


Time Complexity of Insertion Sort
The worst-case time complexity of the Insertion sort is O(N^2)
The average case time complexity of the Insertion sort is O(N^2)
The time complexity of the best case is O(N).

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 83


Sorting(CO1)

Insertion Sort Algorithm

Algorithm: We use a procedure INSERTION_SORT. It takes as


parameters an array A[1.. n] and the length n of the array.

INSERTION_SORT (A)

1. FOR j ← 2 TO length[A]
2. DO key ← A[j]
3. //{Put A[j] into the sorted sequence A[1 . . j − 1]}
4. i ← j − 1
5. WHILE i > 0 and A[i] > key
6. DO A[i +1] ← A[i]
7. i←i−1
8. A[i + 1] ← key
9/28/24 Ankita Sharma ACSE0401 DAA Unit I 84
Sorting(CO1)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 85


Sorting(CO1)
Insertion Sort

Three Cases: (Explaination )

Best-Case The best case occurs if the array is already sorted. the while-loop
in line 5 executed only once for each j. This happens if given array A is
already sorted. T(n) = an + b = O(n) It is a linear function of n.

Worst case The worst-case occurs if the array is sorted in reverse order i.e.,
in decreasing order. the worst-case occurs, when line 5 executed j times for
each j. This can happens if array A starts out in reverse order T(n) =
an2 + bn + c = O(n2)

Average case: When half of the array is sorted and half of the array is
unsorted it produces O(n2)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 86


Insertion Sorting (Algo)/Pseudocode

30 50 10 60 20 40
Insertion sort (A,n)
{ 1 2 3 4 5 6
for j ß 2 to n
{
Key = A[j]
i= j-1
while(i>0 and A[i] > key)
{
A[i+1] =A[i]
i=i-1
}
A[i+1]=key
}

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 87


Shell Sort

Shell sort is the generalization of insertion sort, which overcomes the


drawbacks of insertion sort by comparing elements separated by a gap
of several positions.

It is a sorting algorithm that is an extended version of insertion sort.


Shell sort has improved the average time complexity of insertion sort.

As similar to insertion sort, it is a comparison-based and in-place sorting


algorithm. Shell sort is efficient for medium-sized data sets.
Case
Time Complexity Time Complexity
Best O(nlog n)
Average O(nlog n)
Worst O(n^2)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 88


Shell Sort Algo

for (gap =n/2 ; gap >=1 ; gap = gap/2 )


{
for (j=gap ; j < n ; j++)
{
for (i=j-gap ; i >=0 ; i=i-gap )
{
if (a[i+gap] > a[i] )
{break;
}
Else
Swap (a[i + gap] , a[i] ) }
}}}

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 89


Time complexity analysis Shell Sort
• Complexity in the Worst-Case Scenario: Less Than or Equal to O (n2)

• The worst-case complexity for shell sort, according to the Poonen Theorem, is (N
log N)2/(log log N)2, or (N log N)2/log log N), or (N(log N)2), or something in
between.

• Complexity in the Best Case: O(n*Log n)


The total number of comparisons for each interval (or increment) is equal to
the size of the array when it is already sorted.

• Complexity in the Average Case: O(n*log n)


It's somewhere around O. (n1.25).

• The degree of complexity is determined by the interval picked. The above


complexity varies depending on the increment sequences used. The best increment
sequence has yet to be discovered.

Megha Gupta ACSE0401 DAA


9/28/24 90
Unit I
Comparison of Sorting Algorithms

Ankita Sharma ACSE0401 DAA


9/28/24 91
Unit I
Sorting in Linear Time
• We know that it is not possible to sort n elements faster than Ω(n lg
n) in the worst case when using only comparisons (i.e. in the
decision tree model). A question we can ask is: Are there other ways
to sort? What else could we use besides comparisons? Let’s think
about the following problem: Exercise: Give an O(n) algorithm to sort
n integers in the range 0..999. As we can see from this example,
non-comparison sorting algorithm exist, but they crucially exploit
special properties of the input (such as integers in a small range).
The most common ones are bucket sort, counting sort and radix
sort.

Ankita Sharma ACSE0401 DAA


9/28/24 92
Unit I
Sorting(CO1)

Heap Sort

• Heap sort is a comparison based sorting technique based on Binary


Heap data structure.

• The binary heap data structure is an array that can be viewed as


almost complete binary tree.

• Each node of the binary tree corresponds to an element of the array.

• It is similar to selection sort where we first find the maximum


element and place the maximum element at the end.

• We repeat the same process for remaining element.

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 93


Sorting(CO1)

Heap Sort
• For Example
• We represent heaps in level order, going from left to right.

• The array corresponding to the heap above is [25, 13, 17, 5, 8, 3].

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 94


Sorting(CO1)
Heap Sort
The root of the tree A[1] and given index i of a node, the indices of its parent,
left child and right child can be computed as
(if indexing of array starts from 1 )

PARENT (i) = return floor(i/2)


LEFT (i) = return 2i
RIGHT (i) = return 2i + 1

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 95


Sorting(CO1)
Heap Property
1. Max-heap property: A max-heap is a binary tree such that. - the data
contained in each node is greater than (or equal to) the data in that node's
children. ie. for every node i other than the root
A[PARENT(i)] >= A[i]

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 96


Sorting(CO1)
2. Min-heap property: A min-heap is a binary tree such that. - the data
contained in each node is less than (or equal to) the data in that node's children.
- the binary tree is complete
for every node i other than the root
A[PARENT(i)] <= A[i]

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 97


Heap Sorting(CO1)

6 STEPS OF A HEAP SORT ALGORITHM :

1. Transform the array into a binary tree by inserting each element as a node
in a breadth-first manner.
2. Convert the binary tree into a max heap, ensuring that all parent nodes are
greater than or equal to their child nodes.
3. Swap the root node — the largest element — with the last element in the
heap.
4. Call the heapify() function to restore the max heap
5. Repeat step 3 and 4 until the heap is sorted, and exclude the last element
from heap on each iteration
6. After each swap and heapify () call, ensure the max heap property is
satisfied

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 98


Heap Sorting(CO1)

1. TRANSFORM THE ARRAY INTO A BINARY TREE

unsorted array we have [12, 11, 31, 3, 5, 7, 9]

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 99


Heap Sorting(CO1)

2. CONVERT THE BINARY TREE INTO A MAX HEAP

• In a max heap, all parent nodes must have values that are greater than or equal to
the values of their children.
• Means swapping node 12 and node 31 positions in the tree to satisfy the
requirements for a max-heap tree.

max heap = [31, 11, 12, 3, 5, 7, 9]

• Double-check complete binary tree


satisfies all of the necessary properties

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 100


Heap Sorting(CO1)

3. SWAP THE ROOT NODE WITH THE LAST ELEMENT IN THE


HEAP (DELETION)

swapping the element in the first position of the max-heap array with the
element in the last position of the max-heap array.

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 101


Heap Sorting(CO1)

we will be omitting the last value because it’s in a sorted position. Therefore,
we move forward with the following array into the next step:
[9, 11, 12, 3, 4, 7]

Now, transform the array into a tree, then the tree into a max heap.

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 102


Heap Sorting(CO1)

4. CALL THE HEAPIFY() FUNCTION

• The process of converting the tree or array into a max heap as heapify.
• The tree structure may no longer satisfy the requirements of a max heap
after the root node has been swapped
• The heapify () function should be called again to restore the max heap
property.
rearranged heap [12, 11, 9, 3, 5, 7]
• And again, we swap the values in the first and last position of the max-heap array
representation.

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 103


Heap Sorting(CO1)

5. REPEAT STEPS 3 AND 4 UNTIL THE HEAP IS SORTED

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 104


Heap Sorting(CO1)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 105


Heap Sorting(CO1)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 106


Heap Sorting(CO1)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 107


Heap Sorting(CO1)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 108


Heap Sorting(CO1)

6. ENSURE THE MAX HEAP PROPERTY IS SATISFIED

The final sorted array: [3, 5, 7, 9, 11, 12, 31]

The heap sort algorithm's time complexity is O ( n log n) in all cases—


best case, worst case, and average case.

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 109


Sorting(CO1)

Four basic procedures on heap are

1. Heapify, which runs in O( n) time.

2. Build-Heap, which runs in linear time.

3. Heap Sort, which runs in O(n lg n) time.

4. Extract-Max, which runs in O(lg n) time.

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 110


Sorting(CO1)

Heap Sort Algorithm

HEAPSORT (A)
1. BUILD_HEAP (A)
2. for i ← length (A) down to 2 do //reducing heap size
exchange A[1] ↔ A[i] // last & first swap
heap-size [A] ← heap-size [A] – 1 // as element del heap size reduces
Heapify (A, 1) // starts from root

The HEAPSORT procedure takes time O(n lg n), since the call to
BUILD_HEAP takes time O(n) and each of the n -1 calls to Heapify takes
time O(lg n).

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 111


Sorting(CO1)

Build Max Heap (A)

1. heap-size [A] ← length [A] //heap equals to array size


2. For i ← lower bound (length [A])/2 down to 1
// from where elements will be check
3. Do Max Heapify [A, i]

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 112


Sorting(CO1)

Heapify (A, i)

1. l ← left [i]
2. r ← right [i]
3. if l ≤ heap-size [A] and A[l] > A[i]
4. then largest ← l
5. else largest ← i
6. if r ≤ heap-size [A] and A[r] > A[largest]
7. then largest ← r
8. if largest ≠ i
9. then exchange A[i] ↔ A[largest]
10. Heapify (A, largest)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 113


Sorting(CO1)
Characteristics of Heap Sort Algorithm

• The heap sort combines the best of both merge sort and insertion
sort. Like merge sort, the worst case time of heap sort is O(n log n)
and like insertion sort, heap sort sorts in-place.

• The heap sort algorithm starts by using procedure BUILD-HEAP to


build a heap on the input array A[1 . . n].

• Since the maximum element of the array stored at the root A[1], it can
be put into its correct final position by exchanging it with A[n] (the last
element in A).

• If we now discard node n from the heap than the remaining elements
can be made into heap. Note that the new element at the root may
violate the heap property.
9/28/24 Ankita Sharma ACSE0401 DAA Unit I 114
Sorting(CO1)
Example: A=[7, 4, 3, 1, 2]

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 115


Priority Queue
• A priority queue is a type of queue that
arranges elements based on their priority
values.

• Elements with higher priority values are


typically retrieved before elements with
lower priority values.

Megha Gupta ACSE0401 DAA


9/28/24 116
Unit I
Operation on Binary Heap
• insert(p): Inserts a new element with
priority p.
• extractMax(): Extracts an element with
maximum priority.
• getMax(): Returns an element with
maximum priority.
• IncreaseKey(i, p): Increase the priority of
an element pointed by i to p.
Megha Gupta ACSE0401 DAA
9/28/24 117
Unit I
Insert 32 in this tree

Megha Gupta ACSE0401 DAA


9/28/24 118
Unit I
Megha Gupta ACSE0401 DAA
9/28/24 119
Unit I
Extract Max

Megha Gupta ACSE0401 DAA


9/28/24 120
Unit I
Extract Max

Megha Gupta ACSE0401 DAA


9/28/24 121
Unit I
Change Priority/Increase Key

Change the priority of nodes 11 to 35

Megha Gupta ACSE0401 DAA


9/28/24 122
Unit I
Priority Queue Algorithm
Algorithm to find number with highest priority

Heap_Maximum(A)
return A[1]

Running Time Constant (1)

Megha Gupta ACSE0401 DAA


9/28/24 123
Unit I
Priority Queue Algorithm
Algorithm to find number with highest priority
and swap with first element.

Heap_Extract_Maximum(A, heap_size)
if A.heap_size<1
error “heap underflow”
Max = A[1] Running Time –
A[1] = A[heap_size] O(log2 n)
heap_size = heap_size-1
Max_heapify(A,1)
return max
Megha Gupta ACSE0401 DAA
9/28/24 124
Unit I
Heap-Increase-Key(A, i, key)

Heap-Increase-Key(A, i, key)
// Input: A: an array representing a heap, i: an array index, key: a
new key greater than A[i]
// Output: A still representing a heap where the key of A[i] was
increased to key
// Running Time: O(log n) where n =heap-size[A]
1 if key < A[i]
2 error(“New key must be larger than current key”)
3 A[i] ← key
4 while i > 1 and A[Parent(i)] < A[i]
Running Time –
5 exchange A[i] and A[Parent(i)] O(log2 n)
6 i ← Parent(i)

Megha Gupta ACSE0401 DAA


9/28/24 125
Unit I
Max_Heap-Insert(A, key)

Max_Heap-Insert(A, key)
heap_size = heap_size +1
A[heap_size ] = -infinity //consider it smallest
Heap-Increase-Key(A, heap_size, key)

Running Time –
O(log2 n)

Megha Gupta ACSE0401 DAA


9/28/24 126
Unit I
Sorting(CO1)
Counting Sort (non comparison based )

• Counting sort is a stable sorting technique, which is used


to sort objects according to the keys that are small numbers.

• It counts the number of keys whose key values are same.

• This sorting technique is effective when the difference between


different keys are not so big, otherwise, it can increase the space
complexity

• It works by counting the number of objects having distinct key values


(kind of hashing).

• Then there will be some arithmetic to calculate the position of each


object in the output sequence.
9/28/24 Ankita Sharma ACSE0401 DAA Unit I 127
Sorting(CO1)
COUNTING_SORT (A, B, k)
1. for i ← 0 to k do
2. c[i] ← 0
3. for j ← 1 to length[A] do
4. c[A[j]] ← c[A[j]] + 1
5. //c[i] now contains the number of elements equal to i
6. for i ← 1 to k do
7. c[i] ← c[i] + c[i-1]
8. // c[i] now contains the number of elements ≤ i
9. for j ← length[A] downto 1 do
10. B[c[A[j]]] ← A[j]
11. c[A[j]] ← c[A[j]] – 1
Analysis-The average time complexity for Counting Sort is
O(n). The space complexity for Counting Sort is O(n+k).

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 128


Sorting(CO1)
Counting Sort

9/28/24 Megha Gupta ACSE0401 DAA Unit I 129


Sorting(CO1)
Radix Sort

• The idea of Radix Sort is to do digit by digit sort starting from


least significant digit to most significant digit.
• Radix sort uses counting sort as a subroutine to sort.
• Radix sort is a non-comparative integer sorting algorithm that
sorts data with integer keys by grouping keys by the individual
digits which share the same significant position and value.

• To sort d digits:
function radixSort(arr)
maxNum = maximum element in arr
exp = 1
while maxNum / exp > 0
countingSort(arr, exp)
exp *= 10
9/28/24 Ankita Sharma ACSE0401 DAA Unit I 130
Sorting(CO1)
Radix Sort
Complexity Analysis of Radix Sort:
Time Complexity:

•Radix sort is a non-comparative integer sorting algorithm that sorts data with integer
keys by grouping the keys by the individual digits which share the same significant
position and value. It has a time complexity of O(d * (n + b)), where d is the number of
digits, n is the number of elements, and b is the base of the number system being used.

•In practical implementations, radix sort is often faster than other comparison-based
sorting algorithms, such as quicksort or merge sort, for large datasets, especially when
the keys have many digits. However, its time complexity grows linearly with the
number of digits, and so it is not as efficient for small datasets.

Auxiliary Space:
•Radix sort also has a space complexity of O(n + b), where n is the number of elements
and b is the base of the number system. This space complexity comes from the need to
create buckets for each digit value and to copy the elements back to the original array
after each digit has been sorted.
9/28/24 Megha Gupta ACSE0401 DAA Unit I 131
Sorting(CO1)

Radix Sort:
Following example shows how Radix sort operates on four 3-digits
number.

Radix sort complexity is O(kn) for n keys which are integers of word
size k. For all there cases time i.e best , worst and average time
complexity is O(kn).

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 132


Sorting(CO1)
Bucket Sort
• Bucket sort, is a sorting algorithm that works by partitioning an array
into a number of buckets.

• Each bucket is then sorted individually, by using a different sorting


algorithm.

• Divide [0, 1) into n equal-sized buckets.

• Distribute the n input values into the buckets.

• Sort each bucket.

• Then go through buckets in order, listing elements in each one

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 133


Sorting(CO1)

BUCKET_SORT (A)

1. n ← length [A]
2. For i = 1 to n do
3. Insert A[i] into list B[nA[i]]
4. For i = 0 to n-1 do
5. Sort list B with Insertion sort
6. Concatenate the lists B[0], B[1], . . B[n-1] together in order.

• Time Complexity: O(n + k) for best case and average case and
O(n^2) for the worst case.
• Space Complexity: O(nk) for worst case

9/28/24 Megha Gupta ACSE0401 DAA Unit I 134


Sorting(CO1)
Bucket Sort

9/28/24 Megha Gupta ACSE0401 DAA Unit I 135


Daily Quiz

Q1) Process of inserting an element in stack is called


____________

Q2) Process of removing an element from stack is called


__________

Q3) Master’s theorem is used for?

Q4) How many cases are there under Master’s theorem?

Q5) In recursion, the condition for which the function will stop
calling itself is ____________

Q6) What is the average case running time of an insertion


sort algorithm?
9/28/24 Ankita Sharma ACSE0401 DAA Unit I 136
Daily Quiz

Q7) What is the running time of an insertion sort algorithm if the input
is pre-sorted?

Q8) In C, what are the basic loops required to perform an insertion


sort?

Q9)Which of the following sorting algorithm is best suited if the


elements are already sorted?

Q10) What is recurrence for worst case of QuickSort and what is the
time complexity in Worst case?

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 137


Weekly Assignment

Q1 Solve the recurrence relation by iteration T (n) = T (n-1) + n4


[CO1]
Q2 Rank the following by growth rate 2lg n, (√2)lg n, log n!, log (logn),
log2n, (1/3) n, n1/lg n, (3/2) n

[CO1]
Q3 Solve the recurrence: T (n) = 50 T (n/49) + log n!
[CO1]
Q4 Solve the following recurrence: T (n) = √n T (√n) + n
[CO1]
Q5 Use the master method to give tight asymptotic bounds for the
following recurrence. T (n) = 4T (n/2) + n.
[CO1]
Q6 Solve the recurrence T (n) = 2 T (√n) + 1 by making a change of
variables. Your solution should be asymptotically tight. Do not worry
9/28/24about whether
Ankita Sharma values
ACSE0401 DAA areI
Unit integral
138
[CO1]
Weekly Assignment

Q8) How will you sort following array of 5 elements using heap sort
5, 9, 1, 17 and 6. [CO1]
Q9) llustrate the operation of INSERTION-SORT on the array
A = 31, 41, 59, 26, 41, 58 [CO1]
Q10) What do you mean by ‘Stable sorting algorithms’? Quick sort is
unstable where as merge is an stable sorting algorithm. Do you
agree with the above statement? Justify your answer. [CO1]
Q11) Analyze the running time of quick sort in the average case.
Q12) What is time complexity of counting sort? Sort 1, 9, 3, 3, 4, 5, 6,
7, 7, 8 by counting sort. [CO1]
Q13) Find out the worst case running time of merge sort. [CO1]

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 139


Faculty Video Links, You tube & NPTEL
Video Links and Online Courses Details
• You tube/other Video Links
• https://www.youtube.com/watch?v=yRM3sc57q0c&list=PLXFMml
k03Dt7Q0xr1PIAriY5623cKiH7V
• https://www.youtube.com/watch?v=A03oI0znAoc
• https://www.youtube.com/watch?v=Nd0XDY-jVHs
• https://www.youtube.com/watch?v=4V30R3I1vLI
• https://www.youtube.com/watch?v=IawM82BQ4II
• https://www.youtube.com/watch?v=1K9ebQJosvo
• https://www.youtube.com/watch?v=OynWkEj0S-s
• https://www.youtube.com/watch?v=CyknhZbfMqc

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 140


Faculty Video Links, You tube & NPTEL
Video Links and Online Courses Details

• You tube/other Video Links

• https://www.youtube.com/watch?v=BO145HIUHRg
• https://www.youtube.com/watch?v=mB5HXBb_HY8
• https://www.youtube.com/watch?v=6pV2IF0fgKY
• https://www.youtube.com/watch?v=7h1s2SojIRw
• https://www.youtube.com/watch?v=HqPJF2L5h9U
• https://www.youtube.com/watch?v=JMlYkE8hGJM
• https://www.youtube.com/watch?v=pEJiGC-ObQE

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 141


MCQ s
Q1. Two main measures for the efficiency of an algorithm are
a. Processor and memory b. Complexity and
capacity
c. Time and space d. Data and space

Q2. The Worst case occur in linear search algorithm when


a. Item is somewhere in the middle of the array
b. Item is not in the array at all
c. Item is the last element in the array
d. Item is the last element in the array or is not there at all

Q3. The worst case runtime of linear search(recursive) algorithm


a) O(n) b) O(logn) c) O(n2) d) O(nx)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 142


MCQ s

Q4. Which of the following sorting algorithms is the fastest?


(a) Merge sort (b) Quick sort (c) Insertion sort (d) Shell sort
Q5. What is the worst case time complexity of a quick sort algorithm?
(a) O(N) (b) O(N log N) (c) O(N2) (d) O(log N)
Q6. Quick sort is a __________
(a) greedy algorithm
(b) divide and conquer algorithm
(c) dynamic programming algorithm
(d) backtracking algorithm
Q7. What is the worst case time complexity of merge sort?
(a) O(n log n) (b) O(n2) (c) O(n2 log n) (d) O(n log
n2)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 143


MCQ
Q8. The recurrence relation for the linear search recursive algorithm
a)T(n-2)+c b) 2T(n-1)+c c) T(n-1)+c d) T(n+1)+c

Q9. If algo time complexity is O(1), then what we called this ?


a) Linear b)Constant c)Exponent

Q10. List out all stable sort and unstable sort.

Stable Sorting Algorithms: Unstable Sorting Algorithms:


1. Insertion Sort 1.Heap Sort
2. Merge Sort 2.Quick Sort
3. Bubble Sort 3.Selection Sort
4. Counting Sort 4.Shell Sort
5. Tim Sort

Megha Gupta ACSE0401 DAA


9/28/24 144
Unit I
Glossary Question

Q.1 The complexity of searching an element from a set of n elements using Binary
search algorithm is_______
a. O(n log n)
b. O(log n)
c. O(n2) Incorrect
d. O(n)
Q.2______ is a condition that is always true at a particular point in an algorithm.
a. assertion
b. constant
c. exception
d. invariant Correct
Q.3 The running time of quick sort depends on the _________ .
a. Selection of pivot elements
b. Number of input
c. Number of passes
d. Arrangements of the elements

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 145


Glossary Question

Q.7 __________ is the worst case running time of shell sort, using Shell’s increments
a) O(N)
b) O(N log N)
c) O(log N)
d) O(N2)
Q.8 Heap sort is an implementation of ____________ using a descending priority
queue.
a) insertion sort
b) selection sort
c) bubble sort
d) merge sort
Q.9 The descending heap property is ___________
a) A[Parent(i)] = A[i]
b) A[Parent(i)] <= A[i]
c) A[Parent(i)] >= A[i]
d) A[Parent(i)] > 2 * A[i]

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 146


Glossary Question

Q.10 ______is wort case time complexity of Heap sort?


a) O(nlogn)
b) O(n2logn)
c) O(n2)
d) O(n3)

Q.11 In insertion sort, the average number of comparisons required to place the
7th element into its correct position is ______.
a) 9
b) 4
c) 7
d) 14
Q.12 _______ is the average case complexity of selection sort?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 147


Old Question Papers(2020-2021)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 148


Old Question Papers(2020-2021)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 149


Old Question Papers(2020-2021)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 150


Old Question Papers(2020-2021)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 151


Old Question Papers(2020-2021)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 152


Old Question Papers(2020-2021)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 153


Old Question Papers(2021-2022)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 154


Old Question Papers(2021-2022)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 155


Old Question Papers(2021-2022)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 156


Old Question Papers(2021-2022)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 157


Old Question Papers(2021-2022)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 158


Old Question Papers(2021-2022)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 159


Old Question Papers(2021-2022)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 160


Old Question Papers(2021-2022)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 161


Old Question Papers(2021-2022)

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 162


Expected Questions for University Exam
Q1 Differentiate between asymptotic notation O and Ω. [CO1]
Q2 Use a recursion tree to give an asymptotically tight solution to the
recurrence T(n) = T(αn) + T((1 - α)n) + cn, where α is a constant in
the range 0 <α< 1 and c> 0 is also a constant. [CO1]
Q3 Find the time complexity of the recurrence relation
T(n)= n +T(n/10)+T(7n/5) [CO1]
Q4 Compare Time Complexity with Space Complexity. [CO1]
Q5 What are the characteristics of the algorithm? [CO1]
Q6 Show that the solution to T (n) = 2T (⌊n/2⌋ + 17) + n is O (n lg
n).[CO1]
Q7 Solve the recurrence: T (n) = 50 T (n/49) + log n!
[CO1]
Q8 Solve the recurrence using recursion tree method:
[CO1]
9/28/24
T (n) = T (n/2) + Ankita
T (n/4)Sharma
+ T (n/8) +n
ACSE0401 DAA Unit I 163
Expected Questions for University Exam

Q9. Write an algorithm to sort the given array of dement using Quick-
sort. Illustrate the operation of PARTITION procedure on the
array = < 2, 8, 7, 1, 3, 5, 6, 4 > . [CO1]
Q10. Apply BUCKET SORT algorithm on the following array 0 ∙ 78,
0 ∙ 17, 0 ∙ 39, 0 ∙ 26, 0 ∙ 72, 0 ∙ 94, 0 ∙ 21, 0 ∙ 21, 0 ∙ 12, 0 ∙ 23, 0 ∙ 68
[CO1]
Q11. Why Counting sort is called stable sort. [CO1]
Q12. Distinguish between Quick sort and Merge sort, and arrange the
following numbers in increasing order using merge sort
18, 29, 68, 32, 43, 37, 87, 24, 47, 50. [CO1]
Q13. What is divide and conquer strategy and explain the binary
search with suitable example. [CO1]

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 164


Recap Of Unit

This is an introductory chapter of Design & Analysis of Algorithm


covering the concept, importance and characteristics of algorithms. The
complexity and its calculation has been explained. Further, recursion
and different methodologies to solve them have also been provided.
In other part of unit different sorting algorithm – Insertion, Heap sort,
Quick sort, bucket sort, Radix Sort and Counting sort have been
discussed along with their complexities.
.

9/28/24 Ankita Sharma ACSE0401 DAA Unit I 165


9/28/24 Ankita Sharma ACSE0401 DAA Unit I 166
Ankita Sharma ACSE0401 DAA
9/28/24 167
Unit I

You might also like