forked from zfman/AlgorithmCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShenCeInterview1.java
More file actions
28 lines (26 loc) · 714 Bytes
/
ShenCeInterview1.java
File metadata and controls
28 lines (26 loc) · 714 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
package other;
/**
* 神策数据 面试手写算法题1 - 折半查找的实现
*
* @author 刘壮飞
* https://github.com/zfman.
* https://blog.csdn.net/lzhuangfei.
*/
public class ShenCeInterview1 {
public int find(int[] arr,int l,int r,int val){
if(l>r) return -1;
int middle=(l+r)/2;
if(val>arr[middle]) return find(arr,middle+1,r,val);
else if(val<arr[middle]) return find(arr,l,middle-1,val);
else{
return middle;
}
}
public static void main(String[] args){
int[] a={
1,3,5,9,10,20,33
};
int index=new ShenCeInterview1().find(a,0,a.length,33);
System.out.println(index);
}
}