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-coding-problems/src/BinarySearch.java at main · jfullstackdev/java-coding-problems Expand file tree/ Copy pathBinarySearch.javaMore file actions 42 lines (31 loc) · 985 Bytes / Copy pathBinarySearch.javaFile metadata and controls42 lines (31 loc) · 985 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 39 40 41 42 public class BinarySearch { public static int binarySearch(int arr[], int low, int high, int key) { int mid = (low + high) / 2; while (low <= high) { if (arr[mid] < key) { low = mid + 1; } else if (arr[mid] == key) { return mid; } else { high = mid - 1; } mid = (low + high) / 2; } if (low > high) { return -1; } return -1; } public static void main(String[] args) { int intArray[] = {7, 8, 9}; int key = 8; if (binarySearch(intArray, 0, 5, key) != -1) { System.out.print("searching `"); System.out.print(key); System.out.print("` in the array... it's at index "); System.out.println(binarySearch(intArray, 0, 5, key)); } else { System.out.println("index not found"); } } } |
| You are visitor 01402503 — 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. |