

import java.util.Scanner;
 
class BinarySearch 
{
  public static void main(String args[])
  {
    int c, first, last, middle, n, search, array[];
 
    Scanner in = new Scanner(System.in);
    System.out.println("Enter number of elements");
    n = in.nextInt(); 
    array = new int[n];
 
    System.out.println("Enter " + n + " integers");
 
 
    for (c = 0; c < n; c++)
      array[c] = in.nextInt();
 
    System.out.println("Enter value to find");
    search = in.nextInt();
 
    first  = 0;
    last   = n - 1;
    middle = (first + last)/2;
 
    while( first <= last )
    {
      if ( array[middle] < search )
        first = middle + 1;    
      else if ( array[middle] == search ) 
      {
        System.out.println(search + " found at location " + (middle + 1) + ".");
        break;
      }
      else
         last = middle - 1;
 
      middle = (first + last)/2;
   }
   if ( first > last )
      System.out.println(search + " is not present in the list.\n");
  }
}

@@ -21,17 +21,795 @@
 canner;%0A
- 
+%0A/**%0A * Implementation of binary search algorithm in java%0A * %0A * %3Ccode%3E%0A * BinarySearch(A%5B0..N-1%5D, value) %7B%0A *    low = 0%0A *    high = N - 1%0A *     while (low %3C= high) %7B%0A *        // invariants: value %3E A%5Bi%5D for all i %3C low%0A *                       value %3C A%5Bi%5D for all i %3E high%0A *        mid = (low + high) / 2%0A *        if (A%5Bmid%5D %3E value)%0A *            high = mid - 1%0A *        else if (A%5Bmid%5D %3C value)%0A *            low = mid + 1%0A *        else%0A *            return mid%0A *    %7D%0A *    return not_found // value would be inserted at index %22low%22%0A * %7D%0A * %3C%5Ccode%3E%0A * %0A * This searching technique will only work for sorted array.%0A * %0A * For more information on Binary Search use below link%0A * https://en.wikipedia.org/wiki/Binary_search_algorithm%0A * %0A * @author Subham Mishra%0A * */
 %0Aclass B
@@ -1297,39 +1297,8 @@
  1;%0A
-    middle = (first + last)/2;%0A
  %0A  
@@ -1338,81 +1338,41 @@
     
-if ( array%5Bmiddle%5D %3C search )%0A        first = middle + 1;    
+middle = (first + last)/2;
 %0A      
-else 
 if (
@@ -1525,99 +1525,259 @@
 else
-%0A         last = middle - 1;%0A %0A      middle = (first + last)/2;%0A   %7D%0A   if ( first %3E last )
+ if ( array%5Bmiddle%5D %3C search ) //true then element must be in right side of middle%0A        first = middle + 1;    %0A      else%0A         last = middle - 1;%0A   %7D%0A   if ( first %3E last ) //true if all possiblties are already checked still element not found
 %0A   

Update BinarySearch.java