Java implementation of binary search (binary search)

First, what binary search that?

        There is a binary search in order array to find a particular element of the search algorithm. Its design idea is this: Find a set range is the first element of the array (subscript minIndex) to the last element of the array (subscript maxIndex), first look in the middle of the range of elements (subscript as midIndex) whether the element is to be searched, and if so, return it directly to an index; if not, is divided into two cases, one is smaller than the middle of this element, this time to find a range of modified first elements minIndex, the last indexing modify elements of midIndex-1; if the element you want to find larger than the middle element, then, in turn, modify the range for the mid + 1 to the max, and so on, until you want to find looking elements.

        It is noteworthy that, there will be an element you are looking for does not exist in the array of circumstances, so it is necessary to make a judgment, when the Look minIndex has been equal to or even greater than the maximum range maxIndex, certainly at this time I could not find this element, the return -1 can be .

 

Second, the use of round-robin fashion to achieve binary search

        The following is the Java code.

public class BinarySearch {

    public static void main(String[] args) {
        int[] numArray = {1, 5, 12, 25, 68, 102, 222, 233, 256, 500, 1000};
        int targetNum = 102;
        int index = new BinarySearch().binarySearch(numArray, targetNum);
        System.out.println(index);
    }

    /**
     * 折半(二分)查找
     * @param numArray 目标数组
     * @param targetNum 需要查找的元素
     * @return 元素下标
     */
    private int binarySearch(int[] numArray, int targetNum) {
        int minIndex = 0;
        int maxIndex = numArray.length-1;
        int midIndex = (maxIndex + minIndex)/2;
        while(minIndex < maxIndex) {
            if(targetNum == numArray[midIndex]) {
                return midIndex;
            }
            if(targetNum < numArray[midIndex]) {
                maxIndex = midIndex - 1;
                midIndex = (maxIndex + minIndex)/2;
            } else if(targetNum > numArray[midIndex]) {
                minIndex = midIndex + 1;
                midIndex = (maxIndex + minIndex)/2;
            }
        }
        return -1;
    }
}

Third, recursive way to achieve binary search

    /**
     * 递归实现折半(二分)查找
     * @param numArray 目标数组
     * @param minIndex 查找范围的最小索引值
     * @param maxIndex 查找范围的最大索引值
     * @param targetNum 需要查找的元素
     * @return 元素下标
     */
    private int binarySearch(int[] numArray, int minIndex, int maxIndex, int targetNum) {
        int middleIndex = (minIndex + maxIndex)/2;
        if(numArray[middleIndex] == targetNum) {
            return middleIndex;
        } else if(targetNum < numArray[middleIndex]){
            maxIndex = middleIndex -1;
            this.binarySearch(numArray, minIndex, maxIndex, targetNum);
        } else if(targetNum > numArray[middleIndex]){
            minIndex = middleIndex + 1;
            this.binarySearch(numArray, minIndex, maxIndex, targetNum);
        }
        return -1;
    }

 

Published 48 original articles · won praise 52 · views 20000 +

Guess you like

Origin blog.csdn.net/y506798278/article/details/103525104