package 二分递归查找;
public class 顺序二分递归 {
public static int a1(int[] arr,int find)
{
for(int i = 0;i < arr.length;i ++)
{
if(arr[i] == find)
{
return i + 1;
}
}
return -1;
}
//二分查找迭代模式
public static int a2(int[] arr,int fine)
{
int begin = 0;
int last = arr.length - 1;
//int mid = (begin + last) >> 1;
int mid = last + ((begin - last) >> 1);
while(begin < last)
{
if(fine < arr[mid])
{
last = mid - 1;
//mid = (begin + last) >> 1;
mid = last + ((begin - last) >> 1);
}
else if(fine > arr[mid])
{
begin = mid + 1;
//mid = (begin + last) >> 1;
mid = last + ((begin - last) >> 1);
}
else
{
return mid + 1;
}
}
return -1;
}
//二分查找递归模式
public static int a3(int[] arr,int begin,int last,int find)
{
if(begin >= last)
{
return -1;
}
int mid = last + ((begin - last) >> 1);
if(find < arr[mid])
{
return a3(arr,begin,mid - 1,find);
}
else if(find > arr[mid])
{
return a3(arr,mid + 1,last,find);
}
else
{
return mid;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//int arr[10000*10000] = {1,23,54,56,57,58,59,60,65,69};
int[] arr = new int[10000*10000];
long now1 = System.currentTimeMillis();
System.out.println(a1(arr,10000*10000));
System.out.print("顺序查找一共花费" );
System.out.println(System.currentTimeMillis() - now1 + "ms");
long now2 = System.currentTimeMillis();
System.out.println(a2(arr,10000*10000));
System.out.print("二分迭代查找一共花费" );
System.out.println(System.currentTimeMillis() - now2 + "ms");
long now3 = System.currentTimeMillis();
System.out.println(a3(arr,0,arr.length - 1,10000*10000));
System.out.print("二分递归查找一共花费" );
System.out.println(System.currentTimeMillis() - now3 + "ms");
}
}