public class BinarySerch {
/*
* 利用了java中的范式结构,继承Compareable类用到其中的comparaTO方法
*/
public static <AnyType extends Comparable<? super AnyType>> int binarySech(AnyType arr[],AnyType x){
int low= 0,high=arr.length-1; //初始化low为数组开始位置,height为末尾位置
{
low = mid+1;
}else if(arr[mid].compareTo(x)>0)//判断出x是否在中间节点的左边
{
high = mid-1;
}else {
return mid;//返回在数组中的位置
}
}
return 0;
}
public static void main(String[] args) {
Integer a[] = {1,3,8,10,19,21,28,40};
int x = 40;
new BinarySerch();
System.out.println(BinarySerch.binarySech(a, x));
}
}
/*
* 利用了java中的范式结构,继承Compareable类用到其中的comparaTO方法
*/
public static <AnyType extends Comparable<? super AnyType>> int binarySech(AnyType arr[],AnyType x){
int low= 0,high=arr.length-1; //初始化low为数组开始位置,height为末尾位置
while (low<=high){
int mid = (low+high)/2; //初始化中间位置
if(arr[mid].compareTo(x)<0)//判断出x是否在中间节点的右边{
low = mid+1;
}else if(arr[mid].compareTo(x)>0)//判断出x是否在中间节点的左边
{
high = mid-1;
}else {
return mid;//返回在数组中的位置
}
}
return 0;
}
public static void main(String[] args) {
Integer a[] = {1,3,8,10,19,21,28,40};
int x = 40;
new BinarySerch();
System.out.println(BinarySerch.binarySech(a, x));
}
}