SHARE
TWEET

Untitled

a guest Oct 21st, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #binary search pagerintas
  2.  
  3.  private static boolean binarySearch(int[] array, int beginning, int end, int searchedValue) {
  4.  
  5.  
  6.         while (beginning <= end) {
  7.             int middleIndex = (beginning + end) / 2;
  8.             int middleValue = array[middleIndex];
  9.             if (middleValue == searchedValue || array[end] == searchedValue) {
  10.                 return true;
  11.             }
  12.             if (end-beginning==1 && middleValue != searchedValue){
  13.                 return false;
  14.             }
  15.  
  16.             if (searchedValue > middleValue) {
  17.                 beginning = middleIndex;
  18.                 return binarySearch(array, beginning+1, end, searchedValue);
  19.             } else if (searchedValue < middleValue) {
  20.                 end = middleIndex;
  21.                 return binarySearch(array, beginning, end-1, searchedValue);
  22.             }
  23.         }
  24.         return false;
  25.     }
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