0% found this document useful (0 votes)
29 views18 pages

Linear and Binary Search Explained

Uploaded by

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

Linear and Binary Search Explained

Uploaded by

okokji4201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Searching

P R ESENTED BY:
DR . MI L I DHA R
A S S ISTANT P ROF ESSOR, G A LGOTI AS U N I V ERSITY
Introduction
•How do we search a ball 5 occurs in the given list

1 3 4 2 5

•We need 5 comparisons if we check each ball one by one.

04-09-2024 Dr. Mili Dhar 2


Linear Search
public static int search(int arr[], int N, int x)
{
for (int i = 0; i < N; i++) {
if (arr[i] == x)
return i;
}
return -1;
}

04-09-2024 Dr. Mili Dhar 3


Time and Space Complexity of Linear Search
Time Complexity:
Best Case: In the best case, the key might be present at the first index. So the
best case complexity is O(1)
Worst Case: In the worst case, the key might be present at the last index i.e.,
opposite to the end from which the search has started in the list. So the worst-
case complexity is O(N) where N is the size of the list.
Average Case: O(N)
Auxiliary Space: O(1) as except for the variable to iterate through the list, no
other variable is used.

04-09-2024 Dr. Mili Dhar 4


Applications
Unsorted Lists: When we have an unsorted array or list, linear search is most
commonly used to find any element in the collection.
Small Data Sets: Linear Search is preferred over binary search when we have
small data sets with
Searching Linked Lists: In linked list implementations, linear search is
commonly used to find elements within the list. Each node is checked
sequentially until the desired element is found.
Simple Implementation: Linear Search is much easier to understand and
implement as compared to Binary Search or Ternary Search.

04-09-2024 Dr. Mili Dhar 5


Can we improve our searching?

04-09-2024 Dr. Mili Dhar 6


Binary Search
•Binary search is applicable if the array is sorted.

BASIC IDEA
• Look for the target in the middle.
• Operates by repeatedly dividing the search interval in half.

•In every step, we reduce the number of elements to search in by half

04-09-2024 Dr. Mili Dhar 7


How Binary Search Works
Step-by-Step Process:
[Link] with the middle element of the array.
[Link] the target value equals the middle element, the search is done.
[Link] the target value is less than the middle element, narrow the interval to the lower half.
[Link] the target value is greater than the middle element, narrow the interval to the upper half.
[Link] steps 1-4 until the target value is found or the interval is empty.

1 2 3 4 5

5 5 5

04-09-2024 Dr. Mili Dhar 8


Pseudocode
int BinarySearch (int arr[], int low, int high, int key)
{
if (high>= low)
int mid = (low+high) / 2;
if (arr[mid] = = key)
return mid;
6
if ( arr[mid] > key)
return BinarySearch(arr, low, mid-1, key);
else
return BinarySearch(arr, mid+1, high, key);
}

04-09-2024 Dr. Mili Dhar 9


Time Complexity
Therefore,
n
n/2k = 1

n/2
=> log2(n) = log2 (2k)
n/2
=> k = log2(n)
n/22 n/4

n/23 Time complexity :


Best, Average, worst case = O(log2(n))
1k
n/2

04-09-2024 Dr. Mili Dhar 10


Iterative Binary Search
class BinarySearch { public static void main(String args[])
int binarySearch(int arr[], int x) {
BinarySearch ob = new BinarySearch();
{ int low = 0, high = [Link] - 1; int arr[] = { 2, 3, 4, 10, 40 };
while (low <= high) { int n = [Link];
int x = 10;
int mid = (low + high) / 2; int result = [Link](arr, x);
if (result == -1)
if (arr[mid] == x)
[Link](
return mid; "Element is not present in array");
else
if (arr[mid] < x) [Link]("Element is present at "
low = mid + 1; + "index " + result);
}
else }
high = mid - 1; }
04-09-2024 return -1; } Dr. Mili Dhar 11
Applications
1. Search in databases and dictionaries.
2. Used to find first or last occurrence of an element in an array
3. Used to find the square root of a number

04-09-2024 Dr. Mili Dhar 12


Conclusion
We have explored several key aspects of binary search:
1. Necessity of Binary Search over Linear Search
2. Mechanics of Binary Search
3. Time Complexity
4. Applications

04-09-2024 Dr. Mili Dhar 13


Example1: Given a sorted array of positive integers (elements
may be repeated) and a number x. The task is to find the
leftmost index of x in the given array.

Input 1: arr[] = [1, 10, 10 ,10, 20, 20, 40]


x = 20
Output 1: 4

Input 2: arr[] = [10, 20, 30]


x = 15
Output 2: -1

Input 3: arr[] = [15, 15, 15]


x = 15
Output 3: 0
04-09-2024 Dr. Mili Dhar 14
Example1: Given a sorted array of positive integers (elements may be
repeated) and a number x. The task is to find the leftmost index of x in
the given array.
Int first_occ(arr, low, up, x)
{
if(low>up)
arr[] = [5, 10, 10 ,15, 20, 20, 20] return -1
x = 20 int mid = (low+up)/2
Output 1: 4 if(x>arr[mid])
return first_occ(arr, mid+1, up, x)
else if (x<arr[mid])
arr[] = [15, 15, 15] return first_occ(arr, low, mid-1,x)
x = 15 else
Output 2: 0 if(mid==0 || arr[mid-1]!=arr[mid])
return mid
else
return first_occ(arr, low, mid-1,x)
04-09-2024 }Dr. Mili Dhar 15
Example 2: Given a sorted array of positive integers (elements may be
repeated) and a number x. The task is to find the rightmost index of x in
the given array. [Given, N= length of Array]
Int last_occ(arr, low, up, x)
{
Input 1: arr[] = [1, 10, 20, 20, 40] if(low>up)
x = 20 return -1
Output 1: 3 int mid = (low+up)/2
if ( x>arr[mid] )
Input 2: arr[] = [10, 20, 30] return last_occ(arr, mid+1, up, x)
x = 15 else if ( x<arr[mid] )
Output 2: -1 return last_occ(arr, low, mid-1, x)
else
Input 3: arr[] = [15, 15, 15] if ( mid== (N-1) || arr[mid+1]!=arr[mid] )
x = 15 return mid
Output 3: 2 else
return last_occ(arr, mid+1, up, x) }
04-09-2024 Dr. Mili Dhar 16
Example 3: Given a binary sorted non-increasing array
of 1s and 0s. You need to print the count of 1s in the
binary array.

Input 1: arr[] = [1, 1, 1, 1, 1, 0, 0, 0]


Output 1: 5

Input 2: arr[] = [1,1, 0, 0, 0, 0, 0]


Output 2: 2

04-09-2024 Dr. Mili Dhar 17


Wooclap Activity

04-09-2024 Dr. Mili Dhar 18

You might also like