二分查找算法
/**
* 二分查找算法
* @param args
*/
public static void main(String[] args) {
int []arr = {1,2,3,4,5,6,7,8,9,10,12,14,15};
int min = 0;
int max = arr.length-1;
int mid = (min + max) / 2;
int n = 15;
for (int i = 0; i < arr.length; i++) {
if(arr[mid] < n) {
min = mid + 1;
}else if(arr[mid] > n) {
max = mid - 1;
}else {
System.out.println("查找结果为:"+ arr[mid]+",下表为: "+mid);
break;
}
mid = (max + min) / 2;
}
}
相关推荐
faiculty 2020-08-20
wuxiaosi0 2020-06-28
数据与算法之美 2020-06-28
freedomfanye 2020-06-28
yaohustiAC 2020-06-11
只能做防骑 2020-06-01
Clairezz 2020-05-10
computermaths 2020-05-09
dushine00 2020-04-27
Codeeror 2020-04-20
数据与算法之美 2020-04-15
katyusha 2020-04-15
lickylin 2020-02-29
chenfei0 2020-02-26
wulaxiaohei 2020-02-15
baike 2020-02-03
ustbfym 2020-02-02
zangdaiyang 2020-01-29
Unimen 2020-01-11