int arr []={1,14,10,15};
int min=-1;
for(int i=0;i<arr.length-1;i++){
min=i;
for(int j=i+1;j<arr.length;j++){
if(arr[min]<arr[j]){
min=j;
}
}
if(min!=i){
int temp=arr[min];
arr[min]=arr[i];
arr[i]=temp;
}
}
for(int num:arr){
System.out.print(num+"\t");
}
int [] arr={1,2,3,4,5,6,9};
int startIndex=0;
int endIndex=arr.length-1;
int midIndex=-1;
int searchNum=5;
do{
midIndex=(startIndex+endIndex)/2;
if(arr[midIndex]==searchNum){
System.out.println("下标是:"+midIndex);
break;
}else if(arr[midIndex]>searchNum){
endIndex=midIndex-1;
}else{
startIndex=midIndex+1;
}
}while(startIndex<=endIndex);
public static void main(String[] args) {
int [] arr={1,2,3,4,6,5,9};
Arrays.sort(arr);
int startIndex=0;
int endIndex=arr.length-1;
int searchNum=5;
int result=getNum(arr,searchNum,startIndex,endIndex);
System.out.println(result==-1?"要查找的值不在数组范围内":"下标是"+result);
}
public static int getNum(int arr[],int searchNum,int startIndex,int endIndex){
if(startIndex>endIndex){
return -1;
}
int midIndex=(startIndex+endIndex)/2;
if(arr[midIndex]==searchNum){
return midIndex;
}else if(arr[midIndex]>searchNum){
return getNum(arr,searchNum,startIndex,midIndex-1);
}else{
return getNum(arr,searchNum,midIndex+1,endIndex);
}
}