class ArrayFindNum
{
public static void main(String[] args)
{
int[] arr = {1,2,3,6,7,9,10};
//int index = getIndex(arr,2);
int index = halfSearch_2(arr,2);
System.out.println("index=" + index);
}
/*
折半查找
数组必须是有序的
*/
public static int halfSearch(int[] arr,int key)
{
int min,max,mid;
min = 0;
max = arr.length - 1;
mid = (max + min)/2;
while (arr[mid] != key)
{
if (key > arr[mid])
{
min = mid + 1;
}
else if (key < arr[mid])
{
max = mid - 1;
}
if (min > max)
return -1;
mid = (max + min)/2;
}
return mid;
}
/*
折半的第二种方式
*/
public static int halfSearch_2(int[] arr,int key)
{
int min=0,max=arr.length -1,mid;
while(min <= max)
{
mid = (min + max)>>2;
if(key > arr[mid])
min = mid + 1;
else if(key < arr[mid])
max = mid - 1;
else
return mid;
}
return -1;
}
//获取key第一次出现在数组中的位置,如果是-1,那么代表该key在数组中不存在
public static int getIndex(int[] arr,int key)
{
for (int x=0; x<arr.length; x++)
{
if (arr[x] == key)
{
return x;
}
}
return -1;
}
}