DESIGNANALYSIS AND ALGORITHM
Binary Search
Introduction
 Binary Search is a search algorithm that
finds the position of a target value within a
sorted array.
 Binary Search is also known as half-
interval search or logarithmic search.
 Binary Search algorithm works on the
principle of Divide And Conquer.
 Binary Search can be implemented on only
sorted list of elements.
Divide And Conquer
 Divide and Conquer is the most-well known
algorithm strategy.
 A Divide and Conquer algorithm works by
recursively breaking down a problem into
two or more sub-problems of the same or
related type, until these become simple
enough to be solved directly.
 The solutions to the sub-problems are then
combined to give a solution to the original
problem.
Example
 Consider an Array:
A
0 1 2 3 4 5 6 7 8
min mid=(min + max)/2 max
If(37==a[mid]) return mid
Else if(37<a[mid]) max=mid-1
Else if(37>a[mid]) min=mid+1
20 35 37 40 45 50 51 55 67
Algorithm
Input :
A ← sorted array
n ← size of array
key← value to be searched
BinarySearch(a,key,n) :
Set min= 0
Set max= n-1
while (min < max)
Set mid=(min + max)/2
Algorithm
if (key ==A[mid])
Return mid
Else if(key<A[mid])
set max= mid - 1
Else
set min= mid + 1
End if
End while
Return (- 1)
Time Complexity
 The running time complexity for Binary Search
are of three types :-
 In Worst Case ,Time Complexity : O(log n)
 In Average Case ,Time Complexity : O(log n)
 In Best Case , Time Complexity : O(1)
Advantage
 Binary Search is an optimal searching
algorithm using which we can desired
element very efficiently.
Disadvantage
 Binary Search algorithm requires the list to
be sorted. Then only method is Applicable.
Application
 The Binary Search is an efficient searching
method and used to search desired record
from database.
 For solving non-linear equations with one
unknown this method is used.
THANK YOU-------------------------------------

Binary search

  • 1.
  • 2.
    Introduction  Binary Searchis a search algorithm that finds the position of a target value within a sorted array.  Binary Search is also known as half- interval search or logarithmic search.  Binary Search algorithm works on the principle of Divide And Conquer.  Binary Search can be implemented on only sorted list of elements.
  • 3.
    Divide And Conquer Divide and Conquer is the most-well known algorithm strategy.  A Divide and Conquer algorithm works by recursively breaking down a problem into two or more sub-problems of the same or related type, until these become simple enough to be solved directly.  The solutions to the sub-problems are then combined to give a solution to the original problem.
  • 4.
    Example  Consider anArray: A 0 1 2 3 4 5 6 7 8 min mid=(min + max)/2 max If(37==a[mid]) return mid Else if(37<a[mid]) max=mid-1 Else if(37>a[mid]) min=mid+1 20 35 37 40 45 50 51 55 67
  • 5.
    Algorithm Input : A ←sorted array n ← size of array key← value to be searched BinarySearch(a,key,n) : Set min= 0 Set max= n-1 while (min < max) Set mid=(min + max)/2
  • 6.
    Algorithm if (key ==A[mid]) Returnmid Else if(key<A[mid]) set max= mid - 1 Else set min= mid + 1 End if End while Return (- 1)
  • 7.
    Time Complexity  Therunning time complexity for Binary Search are of three types :-  In Worst Case ,Time Complexity : O(log n)  In Average Case ,Time Complexity : O(log n)  In Best Case , Time Complexity : O(1)
  • 8.
    Advantage  Binary Searchis an optimal searching algorithm using which we can desired element very efficiently.
  • 9.
    Disadvantage  Binary Searchalgorithm requires the list to be sorted. Then only method is Applicable.
  • 10.
    Application  The BinarySearch is an efficient searching method and used to search desired record from database.  For solving non-linear equations with one unknown this method is used.
  • 11.