fork download
  1. class Ideone
  2. {
  3. public static void main (String[] args) throws java.lang.Exception
  4. {
  5. int[] numbers = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
  6. boolean found = binarySearch(numbers, 0, numbers.length-1, 5);
  7. System.out.print("Search for 5 " + found );
  8. }
  9.  
  10. public static boolean binarySearch(int[] data, int min, int max, int target){
  11. if (max > min) {
  12. int midpoint = (min + max) / 2; // determine the midpoint
  13.  
  14. if (data[midpoint] == target) {
  15. return true;
  16. }
  17.  
  18. if (data[midpoint] > target) { // use lower half
  19. return binarySearch(data, min, midpoint-1, target);
  20. }
  21. else { // use upper half
  22. return binarySearch(data, midpoint+1, max, target);
  23. }
  24. }
  25. return false;
  26. }
  27. }
Success #stdin #stdout 0.1s 321600KB
stdin
Standard input is empty
stdout
Search for 5  true