-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMySearch.java
More file actions
39 lines (35 loc) · 937 Bytes
/
Copy pathMySearch.java
File metadata and controls
39 lines (35 loc) · 937 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
package algorithm.note;
/**
* @author: mayuan
* @desc:
* @date: 2018/09/01
*/
public class MySearch {
public static void main(String[] args) {
int[] numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println(binarySearch(numbers, 8));
}
/**
* 二分查找:数组需要有序
* @param array
* @param searchKey
* @return
*/
public static int binarySearch(int[] array, int searchKey) {
if (null == array || 0 >= array.length) {
return -1;
}
int left = 0, right = array.length - 1;
while (left <= right) {
int middle = left + (right - left) / 2;
if (searchKey == array[middle]) {
return middle;
} else if (searchKey < array[middle]) {
right = middle - 1;
} else {
left = middle + 1;
}
}
return -1;
}
}