-1

我写了一个二分搜索算法,但它似乎与我见过的其他人有点不同。我希望社区能给我一些反馈,告诉我我是否遗漏了什么,或者做错了什么。

二进制搜索:

import java.util.Arrays; 

public class BinarySearch {

    public int[] numbers = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; 

    /**
     * @param num
     * @param key
     * 
     * Function recursively searches sorted array of integers, finding the specific number (key).
     * Search looks at the midpoint of array, checking to see if midpoint is number being sought,
     * if not, depending of whether the sought number is greater than, or less than, the midpoint
     * the function copies the upper, or lower, half of the array and passes it into a recursive 
     * function call.
     * 
     */
    public int performSearch(int[] num, int key){
        if(num.length == 0){
            System.out.println("Array empty"); 
            return 0; 
        }else{
            int mid; 
            int number=0; 
            mid = (num.length)/2; 
            if(key == num[mid]){
                number =  num[mid]; 
                System.out.println("Found the number " + number); 
                return number; 
            }else if((key < num[mid]) && num.length > 1){
                num = Arrays.copyOfRange(num, 0, mid); 
                System.out.println("Low Range: " + Arrays.toString(num)); 
                return performSearch(num, key); 
            }else if((key > num[mid]) && num.length > 1){
                num = Arrays.copyOfRange(num, mid, num.length); 
                System.out.println("High Range: " + Arrays.toString(num)); 
                return performSearch(num, key); 
            }else{
                System.out.println("Number does not exist in array."); 
                return 0; 
            }
            //return number; 
        }

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        int key = 22; 
        BinarySearch bs = new BinarySearch(); 
        int index = bs.performSearch(bs.numbers, key); 
        System.out.println("Number " + index);
    }

}
4

1 回答 1

1

这是避免数组复制的程序。

导入 java.util.Arrays;

公共类 BinarySearch {

public int[] numbers = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; 

public int performSearch(int[] num, int key, int startIdx, int endIdx ){
    if(num.length == 0){
        System.out.println("Array empty"); 
        return 0; 
    }else{
        int mid; 
        int number=0; 
        mid = (startIdx+endIdx)/2; 
        int currentLength = endIdx - startIdx;
        if(key == num[mid]){
            number =  num[mid]; 
            System.out.println("Found the number " + number); 
            return number; 
        }else if((key < num[mid]) && currentLength > 1){
            System.out.println("Low Range: " + Arrays.toString(num)); 
            return performSearch(num, key, startIdx, mid); 
        }else if((key > num[mid]) && currentLength > 1){ 
            System.out.println("High Range: " + Arrays.toString(num)); 
            return performSearch(num, key, mid,endIdx); 
        }else{
            System.out.println("Number does not exist in array."); 
            return 0; 
        }
        //return number; 
    }

}

/**
 * @param args
 */
public static void main(String[] args) {
    int key = 10; 
    BinarySearch bs = new BinarySearch(); 
    int index = bs.performSearch(bs.numbers, key,0,bs.numbers.length-1); 
    System.out.println("Number " + index);
}

}

于 2013-05-05T02:07:22.517 回答