SHARE
TWEET

Untitled

a guest Oct 21st, 2019 87 in 3 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Arrays;
  2.  
  3. public class BinarySearch {
  4.     public static boolean search(int[] array, int searchedValue) {
  5.         int beginning = 0;
  6.         int end = array.length - 1;
  7. //        boolean[] flag = {false};
  8.         boolean correct = true;
  9.  
  10.         while (beginning <= end) {
  11.             int middleIndex = (beginning + end) / 2;
  12.             int middleValue = array[middleIndex];
  13.             if (middleValue == searchedValue) {
  14.                 return true;
  15.             }
  16.             // restrict the search area
  17.             if ( searchedValue > middleValue) {
  18.                 beginning = middleIndex;
  19.                 return search(Arrays.copyOfRange(array, beginning+1, end+1), searchedValue);
  20.  
  21.             } else if (searchedValue < middleValue) {
  22.                 end = middleIndex;
  23.                 return search(Arrays.copyOfRange(array, beginning, end), searchedValue);
  24.             }
  25.  
  26.         }
  27.         return false;
  28.     }
  29. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top