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

Module 10 Linear Vs Binary

This document compares linear and binary search algorithms. It explains that linear search sequentially scans each element to find a match, resulting in O(n) complexity as it must scan all elements in the worst case. Binary search divides the search space in half at each step by comparing the target to the middle element. This reduces the number of searches needed to O(log n) complexity. The document provides a step-by-step example of binary search to find the element 70 in a sorted array.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Module 10 Linear Vs Binary

This document compares linear and binary search algorithms. It explains that linear search sequentially scans each element to find a match, resulting in O(n) complexity as it must scan all elements in the worst case. Binary search divides the search space in half at each step by comparing the target to the middle element. This reduces the number of searches needed to O(log n) complexity. The document provides a step-by-step example of binary search to find the element 70 in a sorted array.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

IT122

Linear vs Binary
Search
Joie Ann M. Mac
Objectives:
• To learn about Linear Search
• To learn about Binary Search
• To differentiate the two
What is a Linear Search?
• A linear search is also known as a sequential search that
simply scans each element at a time. Suppose we want to
search an element in an array or list; we simply calculate its
length and do not jump at any item.
What is a Linear Search?
Complexity of Linear search

• As linear search scans each element one by one until the


element is not found. If the number of elements increases,
the number of elements to be scanned is also increased. We
can say that the time taken to search the elements is
proportional to the number of elements. Therefore, the
worst-case complexity is O(n)
What is a Binary Search?
• A binary search is a search in which the middle element is
calculated to check whether it is smaller or larger than the
element which is to be searched. The main advantage of using
binary search is that it does not scan each element in the list.
Instead of scanning each element, it performs the searching to
the half of the list. So, the binary search takes less time to search
an element as compared to a linear search.

What is a Binary Search?
There are three cases used in the binary search:
• Case 1: data<a[mid] then left = mid+1.
• Case 2: data>a[mid] then right=mid-1
• Case 3: data = a[mid] // element is found
What is a Binary Search?
We want to search for 70 element from the above array.

In the above figure, we can observe that a[mid]>data, so again, the


value of mid will be calculated in the next step.
What is a Binary Search?
We want to search for 70 element from the above array.
What is a Binary Search?
Step 1: First, we calculate the middle element of an array. We
consider two variables, i.e., left and right. Initially, left =0 and
right=9 as shown in the below figure:
What is a Binary Search?
The middle element value can be calculated as:

Therefore, mid = 4 and a[mid] = 50. The element to be


searched is 70, so a[mid] is not equal to data. The case 2 is
satisfied, i.e., data>a[mid].
What is a Binary Search?
Step 2: As data>a[mid], so the value of left is incremented by
mid+1, i.e., left=mid+1. The value of mid is 4, so the value of
left becomes 5. Now, we have got a subarray as shown in the
below figure:
What is a Binary Search?
Now again, the mid-value is calculated by using the above formula,
and the value of mid becomes 7. Now, the mid can be represented
as:

In the above figure, we can observe that a[mid]>data, so again, the


value of mid will be calculated in the next step.
What is a Binary Search?
Step 3: As a[mid]>data, the value of right is decremented by
mid-1. The value of mid is 7, so the value of right becomes 6.
The array can be represented as:
What is a Binary Search?
The value of mid will be calculated again. The values of left
and right are 5 and 6, respectively. Therefore, the value of mid
is 5. Now the mid can be represented in an array as shown
below:
What is a Binary Search?
• Step 4: As a[mid]<data, the left value is incremented by
mid+1. The value of mid is 5, so the value of left becomes 6.
• Now the value of mid is calculated again by using the formula
which we have already discussed. The values of left and right
are 6 and 6 respectively, so the value of mid becomes 6 as
shown in the below figure:

You might also like