データ構造復習(二)二分検索アルゴリズム
package algorithm.search;
public class Search {
public int BinSearch(int [] a,int k){
int low = 0;
int high = a.length-1;
while(low<=high){
int mid = (low+high+1)/2;
if(k==a[mid]){
return mid;
}else if(k<a[mid]){
high = mid-1;
}else{
low = mid+1;
}
}
return 0;
}
public static void main(String[] args) {
Search search = new Search();
int a[] = {2,5,6,9,12,45,68,71,75,79,85,96,102,200,205,300,999};
System.out.println(search.BinSearch(a, 9));
}
}