Advertisement
deflorator1980

Untitled

Dec 4th, 2017
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. public class Bs {
  2.     int binarySearch(double arr[], int l, int r) {
  3.         if (r >= l) {
  4.             int mid = l + (r - l) / 2;
  5.             if ((int) arr[mid] == mid)
  6.                 return mid;
  7.  
  8.             if ((int) arr[mid] < mid)
  9.                 return binarySearch(arr, l, mid - 1);
  10.  
  11.             return binarySearch(arr, mid + 1, r);
  12.         }
  13.  
  14.         return -1;
  15.     }
  16.  
  17.     public static void main(String args[]) {
  18.         Bs bs = new Bs();
  19.         double arr[] = {1.2, 2.4, 3.11, 3.14, 10.14, 20.14};
  20. //        double arr[] = {1, 2, 2.1, 4, 5, 6, 7, 8, 9, 10};
  21.         int result = bs.binarySearch(arr, 0, arr.length - 1);
  22.         if (result == -1)
  23.             System.out.println("Element not present");
  24.         else
  25.             System.out.println("Element found at index " + result);
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement