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

Lecture 1.2.5. Binary Search

binary search

Uploaded by

anshsaini1603
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)
12 views

Lecture 1.2.5. Binary Search

binary search

Uploaded by

anshsaini1603
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/ 16

UNIVERSITY INSTITUTE OF COMPUTING

BCA/BSC CS

DATA STRUCTURES
(22CAT-211/22SCT-211/22CAT231)

1
Content
2

Chapter 2 (Unit I)
• Lecture1.2.5. Binary search

 Binary search procedure


Complexity of Binary search
Objectives/Outcomes
3

• CO2: Implement and perform time complexity analysis of various sorting


and searching algorithms using C++.

14/06/2024
Searching operation in array
4

• One of the basic operations to be performed on an array is searching.


Searching an array means to find a particular element in the array.
• The search can be used to return the position of the element or check if it
exists in the array.
Searching can be done in two ways:
1. Linear search
2. Binary search
Binary search
5

• Binary search looks for a particular item by comparing the middle most item
of the collection. If a match occurs, then the index of item is returned. If the
middle item is greater than the item, then the item is searched in the sub-
array to the left of the middle item.
• Otherwise, the item is searched for in the sub-array to the right of the middle
item. This process continues on the sub-array as well until the size of the
subarray reduces to zero.

[1]
Binary Search Algorithm

Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
while x not found
if upperBound < lowerBound
EXIT: x does not exists
7

set midPoint = lowerBound + ( upperBound - lowerBound ) / 2


if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
EXIT: x found at location midPoint
end while
end procedure
Searching procedure
8

[4]
Implementation of linear search
9

• #include <bits/stdc++.h>
• using namespace std;
• // A recursive binary search function. It returns location of x in given array arr[l..r] is present, otherwise -1
• int binarySearch(int arr[], int l, int r, int x)
• {
• if (r >= l) {
• int mid = l + (r - l) / 2;
• // If the element is present at the middle itself
• if (arr[mid] == x)
• return mid;
• // If element is smaller than mid, then it can only be present in left subarray

Code (contd..)
• if (arr[mid] > x)
10
• return binarySearch(arr, l, mid - 1, x);

• // Else the element can only be present
• // in right subarray
• return binarySearch(arr, mid + 1, r, x);
• }

• // We reach here when element is not
• // present in array
• return -1;
• }

Code (contd..)
11

• int main(void)
•{
• int arr[] = { 2, 3, 4, 10, 40 };
• int x = 10;
• int n = sizeof(arr) / sizeof(arr[0]);
• int result = binarySearch(arr, 0, n - 1, x);
• (result == -1)
• ? cout << "Element is not present in array"
• : cout << "Element is present at index " << result;
• return 0;
Complexity of binary search
12

• Time and Space complexity


• The time complexity of the binary search algorithm is O(log n). The best-
case time complexity would be O(1) when the central index would directly
match the desired value. The worst-case scenario could be the values at
either extremity of the list or values not in the list.
• The space complexity of the binary search algorithm depends on the
implementation of the algorithm.
Complexity of binary search (Contd..)
13

There are two ways of implementing it:


• Iterative method
• Recursive method
Both methods are quite the same, with two differences in implementation.
First, there is no loop in the recursive method. Second, rather than passing the
new values to the next iteration of the loop, it passes them to the next
recursion. In the iterative method, the iterations can be controlled through the
looping conditions, while in the recursive method, the maximum and
minimum are used as the boundary condition.

[2]
Frequently asked questions
14

• Array is a homogenous data structure which is used for performing various


operations like binary search. Explain.
• Explain procedure of binary search using array.

14/06/2024
REFERENCES
15

[1] https://www.atnyla.com/tutorial/operations-on-array/3/521
[2] https://www.tutorialspoint.com/data_structures_algorithms/binary_search_algorithm.htm
[3] https://www.thedshandbook.com/searching-in-arrays/
[4] https://medium.com/techie-delight/binary-search-practice-problems-4c856cd9f26c

14/06/2024
THANK YOU

You might also like