import java.util.*;
class Halfsearch
{
public static void main(String[] args)
{
char[] arr={'a','b','s','d','e','q','m'};
sop(arr);
System.out.println("");
//排序
for(int x=0;x<arr.length;x++)
{
for(int y=0;y<arr.length-1-x;y++)
{
int num=new Character(arr[y]).compareTo(new Character(arr[y+1]));
if(num>0)
swap(arr,y,y+1);
}
}
sop(arr);
System.out.println(getmyIndex(arr,'l'));
}
//位置转换
public static void swap(char[] arr,int s1,int s2)
{
char num=arr[s1];
arr[s1]=arr[s2];
arr[s2]=num;
}
// 打印方法
public static void sop(char[] arr)
{
for(int x=0;x<arr.length;x++)
{
System.out.print(arr[x]+" ");
}
}
// 获取角标方法
public static int getmyIndex(char[] arr,char key)
{
int min=0;
int max=arr.length-1;
while(min<max)
{
int mid=(min+max)>>1;
int numb=new Character(arr[mid]).compareTo(new Character(key));
if(numb>0)
{
max=mid;
}
if(numb<0)
{
min=mid;
}
return mid;
}
return -1-min;
}
} |