- public static int splitHalf(int[] arrayData,int searchData,int start,int end){
- int index = (start + end)/2;
- int data = arrayData[index];
- if(start > end ){
- return -1;
- }
- if(data == searchData){
- return index;
- }else{
- if(data < searchData){
- return splitHalf(arrayData,searchData,index+1,end);
- }else{
- return splitHalf(arrayData,searchData,start,index-1);
- }
- }
- }
-
- 当然我们也可以直接调用方法。Arrays.binarySearch();
- public static void main(String[] args) {
- int[] array = { 3,5,11,17,21,23,28,30,32,50};
- System.out.println(array.length);
- int isExist = splitHalf(array,(int)50,0,array.length - 1);
- System.out.println("isExist : "+isExist);
- }
复制代码
|