就以这个递归折半查找做开篇吧,距离上次写数据结构已然过去了10年。
希望自己不要基础上耗太久,赶紧迎头赶上吧~
public class HalfSearch {
public static int halfSearch(int low,int hig,double[] list,double target){
int result_mid = -1;
int mid = (low + hig)/2;
if (low>hig) return result_mid;
if (list[mid] < target) result_mid=halfSearch(low,mid-1,list,target);
else if (list[mid] > target) result_mid=halfSearch(mid+1,hig,list,target);
else if (list[mid] == target) {
result_mid = mid;
}
return result_mid;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
double[] list = {65,43,23,22,18,16,14,12,9,7,5,3,2,1};
double target = 77.0;
int result = halfSearch(0,list.length-1,list,target);
System.out.println(result);
}
}