MCPcopy Create free account
hub / github.com/careercup/ctci / binarySearch

Method binarySearch

java/Chapter 11/Introduction/BinarySearch.java:5–21  ·  view source on GitHub ↗
(int[] a, int x)

Source from the content-addressed store, hash-verified

3public class BinarySearch {
4
5 public static int binarySearch(int[] a, int x) {
6 int low = 0;
7 int high = a.length - 1;
8 int mid;
9
10 while (low <= high) {
11 mid = (low + high) / 2;
12 if (a[mid] < x) {
13 low = mid + 1;
14 } else if (a[mid] > x) {
15 high = mid - 1;
16 } else {
17 return mid;
18 }
19 }
20 return -1;
21 }
22
23 public static int binarySearchRecursive(int[] a, int x, int low, int high) {
24 if (low > high) return -1; // Error

Callers 1

mainMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected