GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java/BinarySearch.java at master · shivgy/Java Expand file tree/ Copy pathBinarySearch.javaMore file actions 38 lines (33 loc) · 984 Bytes / Copy pathBinarySearch.javaFile metadata and controls38 lines (33 loc) · 984 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 public class BinarySearch{ public static void main(String[] args){ int[] arr = {2,5,67,88,93,101,652}; Boolean result = binarySearchIT(arr,45); System.out.printl(result.toString()); } /*This method is iterative and works for sorted array only*/ public static Boolean binarySearchIT(int[] arr, int num){ int low = 0; int high = arr.length - 1; while(high - low >= 0){ int mid = low + (high - low)/2; if(arr[mid] == num) return true; else if(arr[mid] > num) high = mid - 1; else low = mid + 1; } return false; } /*Recursive method*/ public static Boolean binarySearchRec(int[] arr, int low, int high, int num){ int mid = low + (high - low)/2; if(low <= high){ if(arr[mid] == num) return true; else if(arr[mid] < num) return binarySearchRec(arr,low,mid-1,num); else return binarySearchRec(arr,mid+1,high,num); } return false; } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |