Analysis and Design of Algorithm (ADA) : Amity School of Engineering & Technology (CSE)
Analysis and Design of Algorithm (ADA) : Amity School of Engineering & Technology (CSE)
Introduction
1
Amity School of Engineering & Technology (CSE)
2
Amity School of Engineering & Technology (CSE)
3
Amity School of Engineering & Technology (CSE)
4
Amity School of Engineering & Technology (CSE)
5
Amity School of Engineering & Technology (CSE)
6
Amity School of Engineering & Technology (CSE)
Learning Outcomes
Students will be able to
Design,
Analyze a
Apply Knowledge Implement and
problem and
of Mathematics, Understand and evaluate a
identify and
Science, Analyze the computer-based
define the
Engineering and recursive and system, process,
computing
computing non recursive component or
requirements
approaches to algorithms programmer to
appropriate to its
the problem. meet desired
solution.
results.
7
Amity School of Engineering & Technology (CSE)
Introduction
• Binary search is the most efficient searching algorithm.
• Binary search, also known as a half-interval search.
• For binary search, the array must be sorted in either ascending
or descending order (limitation).
• Binary search follows divide and conquer approach.
• It's time complexity of O(log2n) makes it very fast as
compared to other searching algorithms.
8
Amity School of Engineering & Technology (CSE)
Divide-and-Conquer Algorithm
• Step 1: If the problem size is small, solve this problem directly;
otherwise, split the original problem into 2 sub-problems with equal
sizes.
5-9
Amity School of Engineering & Technology (CSE)
Algorithm Binary-Search
Binary Search(A[], min, max, d)
A : Array of Elements
min: Lowest Index of A
max: Highest index of A
d: key element
Step 1: (a) Repeat while min <=max
mid=(min+max)/2
(b) if d= A[mid]
Write “Successful Search”
Return mid.
© Else if d < A[mid]
max=mid-1
(d) else
min = mid +1
Step2: Return Null
Step 3: Exit
5 - 10
Amity School of Engineering & Technology (CSE)
Iteration Method
do until the pointers low and high meet each other.
mid = (low + high)/2
if (x == A[mid])
return mid
else if (x > A[mid]) // x is on the right side
low = mid + 1
else // x is on the left side high = mid - 1
11
Amity School of Engineering & Technology (CSE)
Example
Let us consider an array arr = {1, 5, 7, 8, 13, 19, 20, 23, 29}. Find the location
of the item 23 in the array [3].
1st Iteration
beg = 0
end = 8
mid = 4
a[mid] = a[4] = 13 < 23, therefore
II Iteration
• beg = mid +1 = 5
• end = 8
• mid = 13/2 = 6
• a[mid] = a[6] = 20 < 23, therefore;
13
Amity School of Engineering & Technology (CSE)
arr = {1, 5, 7, 8, 13, 19, 20, 23, 29}. To Find the item 23
III iteration
beg = mid + 1 = 7
end = 8
mid = 15/2 = 7
a[mid] = a[7]
a[7] = 23 = item;
therefore, set location = mid;
The location of the item will be 7.
14
Amity School of Engineering & Technology (CSE)
Time Complexity
Recurrence Relation for binary search algorithm:
5 - 15
Amity School of Engineering & Technology (CSE)
Time Complexity
Let we calculate the time complexity of binary search [4]:
T(n) =
1 + T(n/2) =
1 + (1 + T(n/4)) = 2 + T(n/4) =
2 + (1 + T(n/8)) = 3 + T(n/8) = ...
k + T(n/2k) = ...
log n + T(n/2log n) = log n + T(1) = (*)
log n + 1 = Θ(log n).
16
Amity School of Engineering & Technology (CSE)
17
Amity School of Engineering & Technology (CSE)
Summary
• Learned binary search which searches in a sorted array by repeatedly
dividing the search interval in half.
• Binary search is an optimal searching algorithm with which we can
search the desired element efficiently.
• The idea of binary search is to use the information that the array is
sorted and reduce the time complexity to O(log2 n).
• Best Case: ɵ(1)
• Average Case: ɵ(log2n)
• Worst Case: ɵ(log2n)
18