-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiSearch.java
More file actions
46 lines (39 loc) · 847 Bytes
/
Copy pathBiSearch.java
File metadata and controls
46 lines (39 loc) · 847 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
43
44
45
46
package com.algorithm.sort;
import org.junit.Assert;
import org.junit.Test;
/**
* @author chen_wj
* @Description:
* @date 2019/11/5
* @Description: 二分查找算法
* @modifier
*/
public class BiSearch {
@Test
public void testBiSearch() {
int[] arr = {1,3,4,5,7,9,11,15,20};
Assert.assertEquals(biSearch(arr,1), 0);
Assert.assertEquals(biSearch(arr,4), 2);
Assert.assertEquals(biSearch(arr,15), 7);
Assert.assertEquals(biSearch(arr,20), 8);
Assert.assertEquals(biSearch(arr,6), -1);
}
public static int biSearch(int[] arr, int target) {
int min = 0;
int max = arr.length - 1;
int mid;
while(min <= max) {
mid = (min + max) / 2;
if (arr[mid] == target) {
return mid;
} else {
if (arr[mid] > target) {
max = mid - 1;
} else {
min = mid + 1;
}
}
}
return -1;
}
}