1. Binary Search in Data Structure
Prof. Neeraj Bhargava
Kapil Chauhan
Department of Computer Science
School of Engineering & Systems Sciences
MDS University, Ajmer
2. Introduction
A binary search is an advanced type of search
algorithm that finds and fetches data from a sorted list
of items.
Its core working principle involves dividing the data in
the list to half until the required value is located and
displayed to the user in the search result.
Binary search is commonly known as a half-interval
search or a logarithmic search.
3. Cont..
This search algorithm works on the principle of divide
and conquer.
Binary Search is used for searching an element in a
sorted array.
It is a fast search algorithm with run-time complexity
of O(log n).
4. Cont..
Binary search works on the principle of divide and
conquer.
This searching technique looks for a particular
element by comparing the middle most element of the
collection.
It is useful when there are large number of elements in
an array.
6. Implementation
Binary searching starts with middle element. If the
element is equal to the element that we are searching
then return true.
If the element is less than then move to the right of
the list or if the element is greater than then move to
the left of the list.
Repeat this, till you find an element.
7. Algorithm
binarySearch(arr, x, low, high)
if low > high
return False
else
mid = (low + high) / 2
if x == arr[mid]
return mid
else if x > data[mid] // x is on the right side
return binarySearch(arr, x, mid + 1, high)
else // x is on the right side
return binarySearch(arr, x, low, mid - 1)