class xrr
{
public static void pointarray(int[] arr)//打印方式
{
System.out.println("[");
for(int x=0;x<arr.length-1;x++)
{
if(x<arr.length-1)
{
System.out.println("arr["+x+"]="+x+"\n");
}
}
System.out.println("]");
}
public static void selectsout(int[] arr)//排列
{
for (int x=0;x<arr.length-1;x++ )
{
for (int y=x+1;x<arr.length;y++ )
{
if(arr[x]>arr[y])
{
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
}
public static int halfseaurch(int[] arr,int x)//折中查找
{
int indexmax = arr.length-1,indexmin = 0,indexmid = (indexmax+indexmin)/2;
while(arr[indexmid]!=x)
{
if(x<indexmid)
{
indexmax=indexmid-1;
}
else if(x>indexmid)
{
indexmin = indexmid+1;
}
else
{
return -1;
}
indexmid = (indexmax+indexmin)/2;
}
return indexmid;
}
public static void main(String[] args)//主函数输出
{
int[] arr = {1,4,2,7,5,8,3,9};
pointarray(arr);
System.out.println(arr);
selectsout(arr);
System.out.println(arr);
int indexmid=halfseaurch(arr,7);
System.out.println("indexmid="+indexmid);
}
} |
|