File: A_test145_in.java

package info (click to toggle)
eclipse-jdt-ui 4.29-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 93,280 kB
  • sloc: java: 831,977; xml: 14,578; jsp: 33; makefile: 5
file content (30 lines) | stat: -rw-r--r-- 778 bytes parent folder | download
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
package p; // 14, 17, 14, 38

public class A {
	public static int commonBinarySearch(int[] arr, int key) {
		int low = 0;
		int high = arr.length - 1;
		if (key < arr[low] || key > arr[high] || low > high) {
			return -1;
		}
		if (arr[(low + high) / 2] == key) {
			return (low + high) / 2;
		}
		while (low <= high && arr[(low + high) / 2] != key) {
			if (arr[(low + high) / 2] > key) {
				high = (low + high) / 2 - 1;
			} else if (arr[(low + high) / 2] < key) {
				for (; isNoOutOfBound(arr, (low + high) / 2) && arr[(low + high) / 2] < key; ++low)
					;
			}
		}
		if (isNoOutOfBound(arr, (low + high) / 2))
			return arr[(low + high) / 2];
		else
			return -1;
	}

	static boolean isNoOutOfBound(int[] arr, int index) {
		return index >= 0 && index < arr.length;
	}
}