Unit 1
Unit 1
Noida
Introduction to Algorithm
and Asymptotic Notations
Unit: 1
Qualification:
• 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.
CO To analyze and apply different optimization techniques like Knowledge, Analysis And
4 dynamic programming, backtracking and Branch & Bound to Apply
solve the complex problems
CO To understand the advanced concepts like NP Completeness and Knowledge, Analysis and
5 Fast Fourier Transform, to analyze and apply String Matching, Apply
Approximation and Randomized Algorithms to solve the
complex problems
PO PO1
CO.K PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO12
1 1
ACSE0401.1 3 3 3 3 2 - - - 2 2 - 3
ACSE0401.2 3 3 3 3 2 2 - 1 1 1 - 3
ACSE0401.3 3 3 2 3 3 2 - 2 1 1 2 3
ACSE0401.4 3 3 3 3 2 2 - 2 2 1 3 3
ACSE0401.5 2 2 2 2 2 2 - 2 1 1 1 2
Average 2.8 2.8 2.6 2.8 2.2 1.6 - 1.8 1.4 1.2 1.2 2.8
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 xCO10 =
20
1 2
2 2
. .
10 2
SECTION B
2. Attempt any three of the following: 3 x 10 = 30
1 10
2 10
June 13, 2022 Sachin Singh DAA Unit I 13
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
1 10
2 10
• Prerequisite
• Basic concept of c programming language.
• Concept of stack, queue and link list.
• Recap
• Flow Chart
• Algorithm
• https://youtu.be/6hfOvs8pY1k
• 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
.
• 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
Problem
Algorithm
Computer
Input output
• Correctness
• Efficiency
• Simplicity
• Generality
• Non Ambiguity
– Input
– Output
– Definiteness
– Finiteness
– Effectiveness
• Space Complexity
• Time Complexity
• Best Case
• Worst Case
• Average Case
• Prerequisite
• Definition of algorithm
• Recap
• Characteristics of algorithm
• 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 numberSachin
June 13, 2022
of steps
Singh
taken
DAA
on any
Unit I
instance of size n.28
Asymptotic Notations(CO1)- Objective
• Prerequisite
• Definition of algorithm
• Recap
• Cases of analysis of algorithm
Example
f(n)=4.n3+10.n2+5.n+1
Considering g(n)=n3,
Example
f(n)=4.n3+10.n2+5.n+1
Considering g(n)=n3,
Example
f(n)=4.n3+10.n2+5.n+1
Considering g(n)=n3,
Example
f(n)=4.n3+10.n2+5.n+1
Considering g(n)=n4,
lim (4.n3+10.n2+5.n+1/ n4 )=0
n→∞
f(n)=4.n3+10.n2+5.n+1
Considering g(n)=n2,
lim (4.n3+10.n2+5.n+1/ n2 )=0
n→∞
• Master method
• Prerequisite
• Algorithm
• Mathematical concepts of limits and summation
• Recap
• Asymptotic notations
T(n) = {
C
2T(n/2) +cn
n=1
n>1
• Master method
Master method
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).
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).
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).
Merge Sort: T(n) = 2T(n/2) + Θ(n). It falls in case 2 as c is 1 and Log ba]
is also 1. So the solution is Θ(n Logn)
• Prerequisite
• Algorithm
• loops- for while
• C programming
• Recap
• Recursion
Insertion Sort
• It inserts each element of the array into its proper position, leaving
progressively larger stretches of the array sorted.
• This sorting iterates down an array, and the part of the array already
covered is in order; then, the current element of the array is inserted
into the proper position at the head of the array, and the rest of the
elements are moved down, using the space just vacated by the element
inserted as the final space.
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
June 13, 2022 Sachin Singh DAA Unit I 57
Sorting(CO1)
Three Cases:
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.
Average case: When half of the array is sorted and half of the array is
unsorted it produces O(n2)
Heap Sort
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].
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
Heapify (A, i)
1. l ← left [i]
2. r ← right [i]
3. if l ≤ heap-size [A] OR A[l] > A[i]
4. then largest ← l
5. else largest ← i
6. if r ≤ heap-size [A] OR A[r] > A[largest]
7. then largest ← r
8. if largest ≠ i
9. then exchange A[i] ↔ A[largest]
10. Heapify (A, largest)
• 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.
• 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.
June 13, 2022 Sachin Singh DAA Unit I 68
Sorting(CO1)
HEAPSORT (A)
1. BUILD_HEAP (A)
2. for i ← length (A) down to 2 do
exchange A[1] ↔ A[i]
heap-size [A] ← heap-size [A] - 1
Heapify (A, 1)
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).
Counting Sort
• To sort d digits:
RADIX_SORT (A, d)
for i ← 1 to d do
use a stable sort to sort A on digit i
// counting sort will do the job
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).
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
Q5) In recursion, the condition for which the function will stop
calling itself is ____________
Q7) What is the running time of an insertion sort algorithm if the input
is pre-sorted?
Q10) What is recurrence for worst case of QuickSort and what is the
time complexity in Worst case?
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]
• 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
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
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]
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)
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]