当前位置:编程学习 > VB >>

用递归实现二分查找的vb代码

很急呀.....
答案: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]=a[j];
a[j]=temp;

}
}
}

}

public static void main(String[] args) {

System.out.println(halfSeacrh(a,8));

}

}
424077

上一个:vb里的split()函数说明
下一个:VC和VC++和VB和C++的差别

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,