验证中...
私信发送成功
语言: Java
分类: 算法分析
最后更新于 2017-12-07 20:47
折半查找
原始数据 复制代码
package 实验四;
import java.util.Scanner;
/*
* author:黄良运
* time:2017.12.6
* 折半查找
*/
public class Find<AnyType extends Comparable<? super AnyType>> {
public int search(AnyType Arr[], AnyType x) {
int mid, low, high;
low = 0;
high = Arr.length - 1;
while (low <= high) {
mid = (low + high) / 2;
System.out.println("mid: "+mid+" low: "+low+" high: "+high);
if (Arr[mid].compareTo(x) == 0)
return mid;
else if (Arr[mid].compareTo(x) > 0)
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
public static void main(String []y){
Scanner in = new Scanner(System.in);
Integer Arr[] = {5,14,18,21,23,29,31,35};
Find<Integer> find = new Find<Integer>();
System.out.println(find.search(Arr, 23)+": "+Arr[find.search(Arr, 23)]);
}
}

评论列表( 0 )

你可以在登录后,对此项目发表评论

4_float_left_people 4_float_left_close