Binary Search in Data Structure
Prof. Neeraj Bhargava
Kapil Chauhan
Department of Computer Science
School of Engineering & Systems Sciences
MDS University, Ajmer
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.
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).
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.
Example
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.
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)
Assignment
 Explain Binary search in DS with suitable example and
also discuss algorithm.

Binary search in ds

  • 1.
    Binary Search inData Structure Prof. Neeraj Bhargava Kapil Chauhan Department of Computer Science School of Engineering & Systems Sciences MDS University, Ajmer
  • 2.
    Introduction  A binarysearch 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 searchalgorithm 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 searchworks 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.
  • 5.
  • 6.
    Implementation  Binary searchingstarts 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)
  • 8.
    Assignment  Explain Binarysearch in DS with suitable example and also discuss algorithm.