用递归方法写出有序数组的二分查找算法

来源:百度知道 编辑:UC知道 时间:2024/05/31 14:49:42
用递归方法写出有序数组的二分查找算法 .
用JAVA做,要完整的答案,谢谢!

package stack;

public class HalfSearch {
static int a[]={1,3,5,98,8,9,4,38,12};
public static int halfSeacrh(int[] a,int number){//二分查找
HalfSearch hs=new HalfSearch();
hs.bubbleSort(a);
int startPostion=0;
int endPostion=a.length-1;
int postion=(startPostion+endPostion)/2;
while(startPostion<endPostion){
if(a[postion]==number)
return postion;
else if(a[postion]<number){
startPostion=postion+1;
}
else if(a[postion]>number){
endPostion=postion-1;
}
postion=startPostion+endPostion;
}
return -1;

}
public static void bubbleSort(int a[]){
int temp=0;
for(int i=0;i<a.length;i++){
for(int j=i+1;j<a.length;j++){
if(a[i]>a[j]){
temp=a[i];
a[i