Algorithm - Binary Search(Java)

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net 

package chimomo.learning.java.algorithm.search;

/**
 * @author Chimomo
 *
 * <p>
 * Prerequisite:
 *
 * The prerequisite of binary search:
 * 1. The searched sequence must be sequentially stored.
 * 2. The searched sequence must be sorted by keyword.
 * </p>
 */
public class BinarySearch {

    /**
     * Recursively binary search x in sorted array a
     * with low as subscript lower bound and high as subscript upper bound.
     * Its time complexity is O(log2n).
     *
     * @param a The array to be searched
     * @param x The searched target
     * @param low The subscript lower bound of array a
     * @param high The subscript upper bound of array a
     * @return The subscript of x in the array a if found, -1 otherwise.
     */
    private static int searchRecursively(int[] a, int x, int low, int high) {

        // Not found
        if (low > high) {
            return -1;
        }

        int mid = (low + high) >> 1;

        if (x == a[mid]) {
            return mid;
        }

        return x < a[mid]
                ? searchRecursively(a, x, low, mid - 1)
                : searchRecursively(a, x, mid + 1, high);
    }

    /**
     * Circularly binary search x in sorted array a.
     * Its time complexity is O(log2n).
     *
     * @param a The array to be searched
     * @param x The searched target
     * @return The subscript of x in the array a if found, -1 otherwise.
     */
    private static int searchCircularly(int[] a, int x) {
        int low = 0;
        int high = a.length - 1;
        while (low <= high) {
            int mid = (low + high) >> 1;
            if (a[mid] == x) {
                return mid;
            }

            if (a[mid] < x) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }

        return -1;
    }

    /**
     * Test program
     *
     * @param args The arguments
     */
    public static void main(String[] args) {
        int[] arr = {-3, -1, 0, 6, 11, 16, 16, 22, 99, 99};
        System.out.println(String.format("Search %d recursively in array with result subscript: %d",
                16, searchRecursively(arr, 16, 0, arr.length - 1)));
        System.out.println(String.format("Search %d circularly in array with result subscript: %d",
                16, searchCircularly(arr, 16)));
    }

}

/* ------ Running Results ------
Search 16 recursively in array with result subscript: 5
Search 16 circularly in array with result subscript: 5

*/

 

已标记关键词 清除标记
import java.util.*; public class Sample14_25 { public static void main(String[] args) { ArrayList al = new ArrayList(); for(int i=0;i<50;i++){ al.add(Integer.valueOf((int)(Math.random()*100))); } System.out.println("输出排序前的数组:"); //Collections.sort(al); System.out.println(al); int index = Collections.binarySearch(al, Integer.valueOf(20)); if(index < 0){ System.out.print("不好意思,没有查找到相应的内容"); }else{ System.out.println("恭喜你,找到相应的内容,索引是:"+index+" 是该数组的第"+(index+1)+"个数字。"); System.out.print("{"); for(int i=0;i<al.size();i++){ if(i == index){ System.out.print("["+al.get(i)+"]"+" "); }else{ System.out.print(al.get(i)+" "); } } System.out.println("}"); } } } 程序大意:就是随机产生50个(或10个)100以内的整数,然后看看里面有没有20这个数字,要是有就用[]括起来。如18,22,45,[20],78..... 但是问题是,有时候这个程序并不能找到20,虽然生成的随机数里有20?还有就是随机生成的数字里有两个20,但是一会儿指出来第一个,一会儿指出来第二个。 注:随机生成的数字的个数有时是50个,有时是10个,我是为了方便找到20这个数字。 1. ![图片说明](https://img-ask.csdn.net/upload/201508/23/1440333749_822111.png) 2. ![图片说明](https://img-ask.csdn.net/upload/201508/23/1440333799_911979.png) 3. ![图片说明](https://img-ask.csdn.net/upload/201508/23/1440333772_544391.png) 4. ![图片说明](https://img-ask.csdn.net/upload/201508/23/1440333819_869477.png) 5. ![图片说明](https://img-ask.csdn.net/upload/201508/23/1440333849_171912.png) 我先谢谢各位java师傅们了,谢谢!
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页