0% found this document useful (0 votes)
3 views

Lecture-4_Searching Algorithms

The document covers searching algorithms, specifically Linear Search and Binary Search, detailing their definitions, implementations, and complexity analyses. Linear Search is simple and works on unsorted lists with a time complexity of O(n), while Binary Search is efficient for sorted lists with a time complexity of O(log n). It also includes simulations and questions for further understanding of the concepts presented.

Uploaded by

ovikhan753
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)
3 views

Lecture-4_Searching Algorithms

The document covers searching algorithms, specifically Linear Search and Binary Search, detailing their definitions, implementations, and complexity analyses. Linear Search is simple and works on unsorted lists with a time complexity of O(n), while Binary Search is efficient for sorted lists with a time complexity of O(log n). It also includes simulations and questions for further understanding of the concepts presented.

Uploaded by

ovikhan753
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/ 27

CSE214: Algorithms

Searching Algorithms

1
Today’s Contents
• Different Types of Algorithms Algorithm?
• Searching Algorithms
• Complexity Analysis of Searching Algorithms
• Questions for You!

2
Algorithms we are going to study

1. Searching Algorithms
2. Sorting Algorithms
3. Greedy Approaches
4. Dynamic Programming
5. Graph Algorithms

3
Searching Algorithms

4
Searching Algorithms

▪ Search for a target data from a data set.

▪ Searching Algorithms are designed to check for an


element or retrieve an element from any data structure
where it is stored.

▪ Two possible outcomes


▪ Target data is Found (Success)
▪ Target data is not Found (Failure)

5
Types of Searching Algorithms
▪ Based on the type of search operation, two popular
algorithms available:

1. Linear Search / Sequential Search


2. Binary Search

6
1. Linear Search

▪ It is also known as Sequential Search


▪ Linear search is a very basic and simple search algorithm.
▪ The data list or array is traversed sequentially and every
element is checked.

7
Simulation of Linear Search
Check each and every data in the list till the desired element
or value is found.

Suppose, we want to search 33 from the given array, Searching


will start from the first index and stop searching if the data is
found or the list is over.

8
Algorithm of Linear Search
Algorithm:

i = 1 flag = FALSE
while i <= n && Z != X[i] for i = 1 to n
do if A[i] == key
i = i+1
flag = TRUE;
if i < n then
FOUND if flag == TRUE
else FOUND
NOT FOUND else
NOT FOUND

9
Complexity Analysis of Linear Search

Algorithm:

flag = FALSE
for i = 1 to n
if A[i] == key
flag = TRUE;
• Best case: O(1)
if flag == TRUE
FOUND
• Worst Case: O(n)
else
NOT FOUND

10
Features of Linear Search Algorithm

▪ It is used for unsorted and unordered small list of


elements.
▪ It has a time complexity of O(n), which means the time is
linearly dependent on the number of elements, which is
not bad, but not that good too.
▪ It has a very simple implementation.

11
2. Binary Search
▪ Binary Search is used with sorted array or list.
▪ In binary search, we follow the following steps:
– We start by comparing the element to be searched with the element in the
middle of the list/array.

– If we get a match, we return the index of the middle element.

– If we do not get a match, we check whether the element to be searched is less


or greater than in value than the middle element.

– If the element/number to be searched is greater in value than the middle


number, then we pick the elements on the right side of the middle element (as
the list/array is sorted, hence on the right, we will have all the numbers greater
than the middle number), and start again from the step 1.

– If the element/number to be searched is lesser in value than the middle number,


then we pick the elements on the left side of the middle element, and start again
from the step 1. 12
Basic Idea of Binary Search

13
14
15
16
Simulation of Binay Search

17
Algorithm of Binary Search
low = 1 //[Start position]
Algorithm:
high = n //[Last position]
flag = false

while low <= high and flag = = false do

mid = (l+h) / 2
Xm = A[mid]
case:
Xm < Z : low = mid + 1
Xm > Z : high = mid - 1
Xm = = Z : flag = true

if flag = = true
FOUND
else
NOT FOUND
18
Search 55 mid
mid low mid low
low = 0
high = n-1
flag = false

while low <= high and flag = = false do


Step-1: mid = (low + high ) / 2 = (0 + 8) /2 = 4
mid = (low +high) / 2
A[mid] = 45 < 55
Xm = A[mid]
low = mid + 1 = 5
case:
Xm < Z : low = mid
+1
Xm > Z : high = mid Step-2: mid = (low + high ) / 2 = (5 + 8) /2 = 6
-1 A[mid] = 51 < 55
Xm = = Z : flag = true low = mid + 1 = 7

if flag = = true
Step-3: mid = (low + high ) / 2 = (7 + 8) /2 = 7
FOUND
A[mid] = 55 == 55
else
NOT FOUND found

19
Complexity Analysis of Binary Search

• Best case:

• Worst Case:

20
Features of Binary Search

▪ It is great to search through large sorted arrays.


▪ It has a time complexity of O(log n) which is a very good
time complexity.
▪ It has also a simple implementation.

21
22
23
Complexity Analysis of Two Algorithms

Algorithm Best case Worst case

Linear search O(1) O(N)

Binary search O(1) O(log N)

24
Try to answer!
• If we have an array of length 70000000 but sorted, which
searching algorithm will work fast and why??

• Which Searching Algorithm is followed to search any data


from a Dictionary?

• Search item 75 using binary search algorithm from the


following list.

A= [22, 3, 127, 515, 163, 69, 75, 88, 96, 10]


25
Try to answer!
Search 30 from the list using Binary Search. Show
each step.

10 15 21 25 30 40 51 60

26
Thank you!

27

You might also like