0% found this document useful (0 votes)
11 views22 pages

Apex Institute of Technology Department of Computer Science & Engineering

Uploaded by

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

Apex Institute of Technology Department of Computer Science & Engineering

Uploaded by

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

Apex Institute of Technology

Department of Computer Science & Engineering


Bachelor of Engineering (Computer Science & Engineering)
Design and Analysis of Algorithms– (21CSH-282)
Prepared By: Mr. Vikas Kumar (E13657)

07/21/2024 DISCOVER . LEARN . EMPOWER


1
Content

Searching: Linear Search


Binary Search.

2
Linear Search

 Linear search is a straightforward method of searching.

 It is sometimes referred to as a sequential search.

 Assume A is an n-dimensional array. We wish to find an element in this array. This


element will be referred to as a ‘Key.’

 The Key is compared to each element of the array A one by one until the Key is
discovered. The algorithm stops in two cases:

• when a key element is discovered or


• when the whole array is searched.

3
Algorithm of Linear Search

Algorithm LINEAR_SEARCH(A, Key)


// Description: Perform a linear search on array A to
search element Key
// Input: Array of length n and Key to be searched
// Output: Success/failure message

flag ← 0
for i ← 1 to n do
if Key == A[i] then
print “Element Found on Location”, i
flag ← 1
break
end
end

if flag == 0 then
print “Element Not Found”
end 4
Complexity

Best Case:
If the key element is discovered in the first location, the algorithm just needs one comparison. A
number of comparisons should be independent of array size in the best scenario. If the key is at
the first place of the array, the method does one comparison regardless of the length of the array.
As a result, the best-case running time for linear searching is T(n) = O(1).

Worst Case:
If the key element is at the last place or is not there at all, the algorithm performs the maximum
number of comparisons. The array as a whole must be scanned. The number of comparisons
grows linearly with the size of the array. As a result, the worst-case running time of the linear
search is T(n) = O(n).

The problem size is reduced by one on each iteration, and the method does one comparison. So
recurrence for linear search can be defined as T(n) = T(n – 1) + 1. The solution to this recurrence
also demonstrates that the linear search’s worst-case running time is O(n).

5
Complexity

Average Case:
The average case occurs when an element is neither at the first location nor at the
last. The key element may be near the beginning of the array or maybe near to end,
or it may be somewhere near the middle. On average, the algorithm does (n / 2)
comparisons.

Thus, T (n) = O(n/2) = O(n)

6
Recurrence Equation:

Let us solve the recurrence:

In every iteration, the algorithm does one comparison and the problem size is
reduced by 1. Hence, the recurrence of linear search can be formulated as,

T(n) = 0, for n = 0

T(n) = T(n – 1) + 1, for n > 0

7
Recurrence Equation:

T(n – 1) = T(n – 2) + 1

⇒ T(n) = [T(n – 2) + 1] + 1 = T(n – 2) + 2

T(n – 2) = T(n – 3) + 1

⇒ T(n) = [T(n – 3) + 1] + 2 = T(n – 3) + 3

After k iterations,

T(n) = T(n – k) + k

For k = n

T(n) = T(n – n) + n = T(0) + n

The cost of solving problem of size 0 is definitely 0, so T(0) = 0

Hence, T(n) = O(n)


8
Example:

Simulate the linear search algorithm on an array A {44, 22, 88, 11, 55, 33, 66,
77} with Key = 55.

Here, the element to be searched is 55, so Key = 55.

Linear search scans the array one by one element and compares it with the h Key.
If the Key is found, the search stops, otherwise, it scans the next element and
compares it with the key.

9
Example:

10
Binary Search

 The binary search method is more efficient than the linear search method.

 The array must be sorted for binary search, which is not necessary for linear search.

 Binary search is a search strategy based on divide and conquer.

 The method divides the list into two halves at each step and checks whether the element
to be searched is on the top or lower side of the array.

 If the element is located in the center, the algorithm returns.

11
Binary Search

Let us assign the minimum and maximum index of the array to variables low and high, respectively.
The middle index is computed as (low + high)/2.

In every iteration, the algorithm compares the middle element of the array with a key to be searched.
The initial range of search is A[0] to A[n – 1]. When the key is compared with A [mid], there are three
possibilities :

The array is already sorted, so if key < A[mid] then the key cannot be present in the bottom half of the
array. The search space of the problem will be reduced by setting the high index to (mid – 1). The new
search range would be A[low] to A[mid – 1].
If key > A[mid] then the key cannot be present in the upper half of the array. The search space of the
problem will be reduced by moving the low index to (mid + 1). The new search range would be A[mid
+ 1] to A[high].
If key = A[mid], the search is successful, and the algorithm halts.

This process is repeated till the index low is less than the high or the element is found.
12
Algorithm for Binary Search

Algorithm BINARY_SEARCH(A, Key) Binary search reduces search space by half in


// Description : Perform a binary search on array A
// Input : Sorted array A of size n and Key to be searched every iteration. In a linear search, the search
// Output : Success / Failure space was reduced by one only.
low ← 1
high ← n If there are n elements in an array, binary
while low < high do search, and linear search have to search among
mid ← (low + high) / 2 (n / 2) and (n – 1) elements respectively in the
if A[mid] == key then
return mid second iteration.
else if A[mid] < key then In the third iteration, the binary search has to
low ← mid + 1 scan only (n / 4) elements, whereas the linear
else
high ← mid – 1 search has to scan (n – 2) elements.
end This shows that a binary search would hit the
end bottom very quickly.
return 0
13
Complexity Analysis of Binary Search

Best Case:
In binary search, the key is initially compared to the array’s middle element. If the key is in the center of the
array, the algorithm only does one comparison. As a result, the algorithm’s best-case running time is T(n) = 1.

Worst Case:
In every iteration, the binary search space is decreased by half, allowing for maximum log2n array divisions.
If the key is at the leaf of the tree or it is not present at all, then the algorithm does log2n comparisons, which is
the maximum.
The number of comparisons increases in logarithmic proportion to the amount of the input. As a result, the
algorithm’s worst-case running time would be T(n) = O(log2 n).

The problem size is reduced by a factor of two after each iteration, and the method does one comparison.
Recurrence of binary search can be written as T(n) = T(n/2) + 1.
The solution to this recurrence leads to the same running time, i.e. O(log2n).

14
Recurrence Equation:

T (n) = T(n/2) + 1 …(1)

Substitute n by n/2 in Equation (1) to find T(n/2)

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

Substitute value of T(n/2) in Equation (1),

T(n) = T(n/22) + 1 …(3)

Substitute n by n/2 in Equation (2) to find T(n/4),

T(n/4) = T(n/8) + 1

Substitute value of T(n/4) in Equation (3),

T(n) = T(n/23) + 3

15
Recurrence Equation:
After k iterations,

T(n) = T(nk) + k

Binary tree created by binary search can have maximum height log2n

So, k = log2n ⇒ n = 2k

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

= T(1) + k

From base case of recurrence,

T(n) = 1 + k = 1 + log2n

T(n) = O(log2n)

16
Complexity Analysis of Binary Search

Average Case:
The average case for binary search occurs when the key element is neither in the
middle nor at the leaf level of the search tree.

On average, it does half of the log2 n comparisons, which will turn out as

T (n) = O(log2 n).

17
Example

nums = [2, 3, 5, 7, 8, 10, 12, 15, 18, 20]


target = 7

18
Example

19
TASKS END OF LECTURE LEARNING (TELL):

Task 1:

Simulate the linear search algorithm on an array A {44, 22, 88, 11, 55, 33, 66, 77} with Key = 66.

Task 2:
Given a sorted array containing duplicates, efficiently find each element’s frequency without traversing the whole
array Linear Search.

Task 3:
Given a positive number, return the square root of it. If the number is not a perfect square, return the floor of its
square root.

Task 4:
Implement a ternary search algorithm and compare its performance with a binary search algorithm.

Task 5:

Simulate the Binary search algorithm on an array A {44, 22, 88, 11, 55, 33, 66, 77} with Key = 66.

20
References
• Fundamentals of Computer Algorithms 2nd Edition (2008) by Horowitz, Sahni
and Rajasekaran
• Introduction to Algorithms 3rd Edition (2012) by Thomas H Cormen, Charles E
Lieserson, Ronald

21
THANK YOU

For queries
Email: [email protected]

07/21/2024 22

You might also like