Complete Binary Search Algorithm

Binary Search Algorithm:

ASSUMPTION:

• Let a base i 1 ≤ i ≤ n be a list of elements which are sorted in non-decreasing order.


PROBLEM:

• Consider the problem of determining whether a given element x is present in the list.

• In case x is present, we are to determine a value j such that a base j = x.

• If x is not in the list then j is to be set to zero.


Process of Binary Search Using Divide and Conquer:

• Divide-and conquer suggests breaking up any instance

• I = (n, a 1, ... , a n,  x) of this search problem into sub instances.

 One possibility is to pick an index k and obtain three instances:

 I1 = (k - 1, a 1, ••• , ak-1, x),

 I2 = (1, a k, x), and

  I3 = (n - k, a k+1 ,••••, an, x).


Algorithm of Binary Search:


Algorithm Binary Search(a,i,l,x)

       if(l=i) then // small(p) only one element

       { 

if(x==a[i]) then

return i;

else

return 0;

       }

       else

mid= (i+l)/2

If(x=a[mid]) then return mid;

Elseif (x<a[mid]) then

      return BinarySearch(a,i,mid-1,x)

Else

      return BinarySearch(a,mid+1,l,x)

       }

}


Example:


Example -2:


Let us select the nine entries,

-15, -6, 0, 7, 9, 23, 54, 82, 101

place them in A(1:9), and simulate the steps that BINARYSEARCH

goes through as it searches for different values of x.

Only the variables low, high and mid need to be traced as we simulate

the algorithm.

We shall try the following values for x: 101, -14, and 82 for two

successful searches and one unsuccessful search.





Complexity of Binary Search

• Theorem 1:


• If n is in the range [2k-1, 2k) then BINARYSEARCH makes at most k element comparisons for a successful search and either k - 1 or k comparisons for an unsuccessful search. (In other words the time for a successful search is O(log n) and for an unsuccessful search it is Θ (log n)).


• Proof for Theorem 1:


Proof: Consider the binary decision tree describing the action of BINARYSEARCH on n elements.

• All successful searches end at a circular node while all unsuccessful searches end at a square node. If 2^k-1 ≤ n < 2^k then all circular nodes are at levels 1, 2, ... , k while all square nodes are at levels k and k+ 1 (note that the root is at level 1).

• The number of element comparisons needed to terminate at a circular

node on level i is i while the number of element comparisons needed

to terminate at a square node at level i is only i - 1.





Time complexity of Binary Search:

• Let T(n) be the number of comparisons in worst-case in an array of n elements.

• Recurrence relation for binary search is





Complexity of Binary search O(logn)

T(n) = [T(n/2)+1]

= [T(n/4)+1]+1

= [T(n/8)+1]+2

*

*


T(n)=T(n/2^i)+i., for any (log n) ≥i ≥ 1.

Assume n/2^i=1 Therefore n=2^i and i=(log n)

Thus, T(n) = T(1) + log n

= 0+log n

= O(log n) 



Approach:

We basically ignore half of the elements just after one comparison.

• Compare x with the middle element. If x matches with middle element, we return the mid index.

• Else If x is greater than the mid element, then x can only lie in right half subarray after the mid element. So we recur for right half.

• Else (x is smaller) recur for the left half.

Program:

Recursive implementation of Binary Search:

// Java implementation of recursive Binary Search 

class BinarySearch { 

// Returns index of x if it is present in arr[l.. 

// r], else return -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 

if (arr[mid] > x) 

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; 


// Driver method to test above 

public static void main(String args[]) 

BinarySearch ob = new BinarySearch(); 

int arr[] = { 2, 3, 4, 10, 40 }; 

int n = arr.length; 

int x = 10; 

int result = ob.binarySearch(arr, 0, n - 1, x); 

if (result == -1) 

System.out.println("Element not present"); 

else

System.out.println("Element found at index " + result); 

}


Iterative implementation of Binary Search:


// Java implementation of iterative Binary Search 

class BinarySearch { 

// Returns index of x if it is present in arr[], 

// else return -1 

int binarySearch(int arr[], int x) 

int l = 0, r = arr.length - 1; 

while (l <= r) { 

int m = l + (r - l) / 2; 


// Check if x is present at mid 

if (arr[m] == x) 

return m; 


// If x greater, ignore left half 

if (arr[m] < x) 

l = m + 1; 


// If x is smaller, ignore right half 

else

r = m - 1; 


// if we reach here, then element was 

// not present 

return -1; 


// Driver method to test above 

public static void main(String args[]) 

BinarySearch ob = new BinarySearch(); 

int arr[] = { 2, 3, 4, 10, 40 }; 

int n = arr.length; 

int x = 10; 

int result = ob.binarySearch(arr, x); 

if (result == -1) 

System.out.println("Element not present"); 

else

System.out.println("Element found at " + "index " + result); 

Time Complexity:

• The time complexity of Binary Search can be written as

                                              T(n) = T(n/2) + c 

• The above recurrence can be solved either using Recurrence T ree method or Master method. It falls in case II of Master Method and solution of the recurrence is Theta(Logn).


Auxiliary Space: O(1) in case of iterative implementation. In case of recursive implementation, O(log n) recursion call stack space.


I think from my explanation  you got a basic idea about Binary Search 🙂.


Time complexity Graph:







Happy Coding💻:-)










Comments

Popular posts from this blog

Valid Username Regular Expression

Rotate String in Right Direction in java

Java MCQ's