{"id":206612,"date":"2020-12-13T10:20:18","date_gmt":"2020-12-13T02:20:18","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=206612"},"modified":"2020-12-07T10:20:56","modified_gmt":"2020-12-07T02:20:56","slug":"you-dichotomy-search","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/you-dichotomy-search.html","title":{"rendered":"\u6c42\u804c\u5fc5\u4f1a\u7b97\u6cd5\u624b\u628a\u624b\u6559\u4f60\u4e8c\u5206\u6cd5\u67e5\u627e"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5f53\u6570\u7ec4\u6216\u8005\u96c6\u5408\u4e2d\u5b58\u653e\u7684\u5143\u7d20\u6570\u91cf\u975e\u5e38\u591a\u7684\u65f6\u5019\uff0c\u60f3\u8981\u8ddf\u8e2a\u5177\u4f53\u67d0\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\u6216\u8005\u662f\u5426\u5b58\u5728\uff0c\u5e38\u89c4\u65b9\u5f0f\u662f\u5faa\u73af\u6bcf\u4e00\u4e2a\u5143\u7d20\u76f4\u5230\u627e\u5230\u8981\u67e5\u627e\u7684\u5143\u7d20\u4e3a\u6b62\u3002\u8fd9\u6837\u7684\u67e5\u627e\u65b9\u5f0f\u6548\u7387\u975e\u5e38\u4f4e\u4e0b\uff0c\u8fd9\u4e2a\u65f6\u5019\u9700\u8981\u4f7f\u7528\u4e8c\u5206\u6cd5\u6765\u5b9e\u73b0\uff0c\u63d0\u9ad8\u67e5\u627e\u6548\u7387\u3002<\/p>\n

<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

1\u3001\u4e8c\u5206\u6cd5\u67e5\u627e\u7684\u80cc\u666f<\/strong><\/div>\n

\u5f53\u6570\u7ec4\u6216\u8005\u96c6\u5408\u4e2d\u5b58\u653e\u7684\u5143\u7d20\u6570\u91cf\u975e\u5e38\u591a\u7684\u65f6\u5019\uff0c\u60f3\u8981\u8ddf\u8e2a\u5177\u4f53\u67d0\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\u6216\u8005\u662f\u5426\u5b58\u5728\uff0c\u5e38\u89c4\u65b9\u5f0f\u662f\u5faa\u73af\u6bcf\u4e00\u4e2a\u5143\u7d20\u76f4\u5230\u627e\u5230\u8981\u67e5\u627e\u7684\u5143\u7d20\u4e3a\u6b62\u3002\u8fd9\u6837\u7684\u67e5\u627e\u65b9\u5f0f\u6548\u7387\u975e\u5e38\u4f4e\u4e0b\uff0c\u8fd9\u4e2a\u65f6\u5019\u9700\u8981\u4f7f\u7528\u4e8c\u5206\u6cd5\u6765\u5b9e\u73b0\uff0c\u63d0\u9ad8\u67e5\u627e\u6548\u7387\u3002<\/p>\n

2\u3001\u4e8c\u5206\u6cd5\u67e5\u627e\u7684\u4ecb\u7ecd<\/strong><\/div>\n

\u4e8c\u5206\u6cd5\u67e5\u627e(\u6298\u534a\u67e5\u627e)\uff0c\u627e\u6307\u5b9a\u6570\u503c\u6240\u5728\u7684\u4f4d\u7f6e<\/p>\n

\u767e\u5ea6\u767e\u79d1\u662f\u8fd9\u6837\u4ecb\u7ecd\u4e8c\u5206\u6cd5\u67e5\u627e\u7684\uff1a<\/p>\n

\"\"<\/p>\n

3\u3001\u4e8c\u5206\u6cd5\u67e5\u627e\u7684\u7b97\u6cd5\u601d\u60f3<\/strong><\/div>\n

\u5047\u8bbe\u6570\u7ec4\u662f\u6309\u5347\u5e8f\u6392\u5e8f\u7684\uff0c\u5bf9\u4e8e\u7ed9\u5b9a\u7684\u76ee\u6807\u503caim\uff0c\u4ece\u6570\u7ec4\u7684\u4e2d\u95f4\u4f4d\u7f6e\u5f00\u59cb\u67e5\u627e\uff1a1.\u82e5\u67e5\u627e\u6570\u636e\u4e0e\u4e2d\u95f4\u5143\u7d20\u503c\u6b63\u597d\u76f8\u7b49\uff0c\u5219\u8fd4\u56de\u4e2d\u95f4\u5143\u7d20\u503c\u7684\u7d22\u5f15;2.\u82e5\u67e5\u627e\u6570\u503c\u6bd4\u4e2d\u95f4\u503c\u5c0f\uff0c\u5219\u4ee5\u6574\u4e2a\u67e5\u627e\u8303\u56f4\u7684\u524d\u534a\u90e8\u5206\u4f5c\u4e3a\u65b0\u7684\u67e5\u627e\u8303\u56f4;3.\u82e5\u67e5\u627e\u6570\u503c\u6bd4\u4e2d\u95f4\u503c\u5927\uff0c\u5219\u4ee5\u6574\u4e2a\u67e5\u627e\u8303\u56f4\u7684\u540e\u534a\u90e8\u5206\u4f5c\u4e3a\u65b0\u7684\u67e5\u627e\u8303\u56f4;\u6ce8\uff1a\u67e5\u627e\u6210\u529f\u8fd4\u56de\u7d22\u5f15\uff0c\u5931\u8d25\u8fd4\u56de-1<\/p>\n

4\u3001\u4ee3\u7801\u5b9e\u73b0<\/strong><\/div>\n
4.1 \u5229\u7528\u5faa\u73af\u7684\u65b9\u5f0f\u5b9e\u73b0\u4e8c\u5206\u6cd5\u67e5\u627e<\/strong><\/span><\/div>\n
\r\npublic class BinarySearch { \r\n    public static void main(String[] args) { \r\n        \/\/ \u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\u7ec4 \r\n        int[] array = suiji(); \r\n        \/\/ \u5bf9\u968f\u673a\u6570\u7ec4\u6392\u5e8f \r\n        Arrays.sort(array); \r\n        System.out.println(\"\u4ea7\u751f\u7684\u968f\u673a\u6570\u7ec4\u4e3a\uff1a \" + Arrays.toString(array)); \r\n \r\n        System.out.println(\"\u8981\u8fdb\u884c\u67e5\u627e\u7684\u503c\uff1a \"); \r\n        Scanner input = new Scanner(System.in); \r\n        \/\/ \u8fdb\u884c\u67e5\u627e\u7684\u76ee\u6807\u503c \r\n        int aim = input.nextInt(); \r\n \r\n        \/\/ \u4f7f\u7528\u4e8c\u5206\u6cd5\u67e5\u627e \r\n        int index = binarySearch(array, aim); \r\n        System.out.println(\"\u67e5\u627e\u7684\u503c\u7684\u7d22\u5f15\u4f4d\u7f6e\uff1a \" + index); \r\n \r\n    } \r\n \r\n    \/** \r\n     * \u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\u7ec4 \r\n     *  \r\n     * @return \u8fd4\u56de\u503c\uff0c\u8fd4\u56de\u4e00\u4e2a\u968f\u673a\u6570\u7ec4 \r\n     *\/ \r\n    private static int[] suiji() { \r\n        \/\/ random.nextInt(n)+m  \u8fd4\u56dem\u5230m+n-1\u4e4b\u95f4\u7684\u968f\u673a\u6570 \r\n        int n = new Random().nextInt(6) + 5; \r\n        int[] array = new int[n]; \r\n        \/\/ \u5faa\u73af\u904d\u5386\u4e3a\u6570\u7ec4\u8d4b\u503c \r\n        for (int i = 0; i < array.length; i++) { \r\n            array[i] = new Random().nextInt(100); \r\n        } \r\n        return array; \r\n    } \r\n \r\n    \/** \r\n     * \u4e8c\u5206\u6cd5\u67e5\u627e  ---\u5faa\u73af\u7684\u65b9\u5f0f\u5b9e\u73b0 \r\n     *  \r\n     * @param array \u8981\u67e5\u627e\u7684\u6570\u7ec4 \r\n     * @param aim \u8981\u67e5\u627e\u7684\u503c \r\n     * @return \u8fd4\u56de\u503c\uff0c\u6210\u529f\u8fd4\u56de\u7d22\u5f15\uff0c\u5931\u8d25\u8fd4\u56de-1 \r\n     *\/ \r\n    private static int binarySearch(int[] array, int aim) { \r\n        \/\/ \u6570\u7ec4\u6700\u5c0f\u7d22\u5f15\u503c \r\n        int left = 0; \r\n        \/\/ \u6570\u7ec4\u6700\u5927\u7d22\u5f15\u503c \r\n        int right = array.length - 1; \r\n        int mid; \r\n        while (left <= right) { \r\n            mid = (left + right) \/ 2; \r\n            \/\/ \u82e5\u67e5\u627e\u6570\u503c\u6bd4\u4e2d\u95f4\u503c\u5c0f\uff0c\u5219\u4ee5\u6574\u4e2a\u67e5\u627e\u8303\u56f4\u7684\u524d\u534a\u90e8\u5206\u4f5c\u4e3a\u65b0\u7684\u67e5\u627e\u8303\u56f4 \r\n            if (aim < array[mid]) { \r\n                right = mid - 1; \r\n                \/\/ \u82e5\u67e5\u627e\u6570\u503c\u6bd4\u4e2d\u95f4\u503c\u5927\uff0c\u5219\u4ee5\u6574\u4e2a\u67e5\u627e\u8303\u56f4\u7684\u540e\u534a\u90e8\u5206\u4f5c\u4e3a\u65b0\u7684\u67e5\u627e\u8303\u56f4 \r\n            } else if (aim > array[mid]) { \r\n                left = mid + 1; \r\n                \/\/ \u82e5\u67e5\u627e\u6570\u636e\u4e0e\u4e2d\u95f4\u5143\u7d20\u503c\u6b63\u597d\u76f8\u7b49\uff0c\u5219\u653e\u56de\u4e2d\u95f4\u5143\u7d20\u503c\u7684\u7d22\u5f15 \r\n            } else { \r\n                return mid; \r\n            } \r\n        } \r\n        return -1; \r\n    } \r\n} <\/pre>\n

\u4ee3\u7801\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n

\u4ea7\u751f\u7684\u968f\u673a\u6570\u7ec4\u4e3a\uff1a [16, 33, 40, 46, 57, 63, 65, 71, 85] \r\n\u8981\u8fdb\u884c\u67e5\u627e\u7684\u503c\uff1a  \r\n46 \r\n\u67e5\u627e\u7684\u503c\u7684\u7d22\u5f15\u4f4d\u7f6e\uff1a 3 \r\n<\/pre>\n

\u82e5\u8f93\u5165\u7684\u503c\u627e\u4e0d\u5230\uff0c\u5219\u8fd4\u56de-1<\/p>\n

\r\n\u4ea7\u751f\u7684\u968f\u673a\u6570\u7ec4\u4e3a\uff1a [28, 41, 47, 56, 70, 81, 85, 88, 95] \r\n\u8981\u8fdb\u884c\u67e5\u627e\u7684\u503c\uff1a  \r\n66 \r\n\u67e5\u627e\u7684\u503c\u7684\u7d22\u5f15\u4f4d\u7f6e\uff1a -1 \r\n<\/pre>\n
4.2 \u5229\u7528\u9012\u5f52\u7684\u65b9\u5f0f\u5b9e\u73b0\u4e8c\u5206\u6cd5\u67e5\u627e<\/strong><\/span><\/div>\n
\r\npublic class BinarySearch2 { \r\n    public static void main(String[] args) { \r\n        \/\/ \u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\u7ec4 \r\n        int[] array = suiji(); \r\n        \/\/ \u5bf9\u968f\u673a\u6570\u7ec4\u6392\u5e8f \r\n        Arrays.sort(array); \r\n        System.out.println(\"\u4ea7\u751f\u7684\u968f\u673a\u6570\u7ec4\u4e3a\uff1a \" + Arrays.toString(array)); \r\n \r\n        System.out.println(\"\u8981\u8fdb\u884c\u67e5\u627e\u7684\u503c\uff1a \"); \r\n        Scanner input = new Scanner(System.in); \r\n        \/\/ \u8fdb\u884c\u67e5\u627e\u7684\u76ee\u6807\u503c \r\n        int aim = input.nextInt(); \r\n \r\n        \/\/ \u4f7f\u7528\u4e8c\u5206\u6cd5\u67e5\u627e \r\n        int index = binarySearch(array, aim, 0, array.length - 1); \r\n        System.out.println(\"\u67e5\u627e\u7684\u503c\u7684\u7d22\u5f15\u4f4d\u7f6e\uff1a \" + index); \r\n    } \r\n \r\n    \/** \r\n     * \u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\u7ec4 \r\n     * \r\n     * @return \u8fd4\u56de\u503c\uff0c\u8fd4\u56de\u4e00\u4e2a\u968f\u673a\u6570\u7ec4 \r\n     *\/ \r\n    private static int[] suiji() { \r\n        \/\/ Random.nextInt(n)+m  \u8fd4\u56dem\u5230m+n-1\u4e4b\u95f4\u7684\u968f\u673a\u6570 \r\n        int n = new Random().nextInt(6) + 5; \r\n        int[] array = new int[n]; \r\n        \/\/ \u5faa\u73af\u904d\u5386\u4e3a\u6570\u7ec4\u8d4b\u503c \r\n        for (int i = 0; i < array.length; i++) { \r\n            array[i] = new Random().nextInt(100); \r\n        } \r\n        return array; \r\n    } \r\n \r\n    \/** \r\n     * \u4e8c\u5206\u6cd5\u67e5\u627e ---\u9012\u5f52\u7684\u65b9\u5f0f \r\n     * \r\n     * @param array \u8981\u67e5\u627e\u7684\u6570\u7ec4 \r\n     * @param aim   \u8981\u67e5\u627e\u7684\u503c \r\n     * @param left  \u5de6\u8fb9\u6700\u5c0f\u503c \r\n     * @param right \u53f3\u8fb9\u6700\u5927\u503c \r\n     * @return \u8fd4\u56de\u503c\uff0c\u6210\u529f\u8fd4\u56de\u7d22\u5f15\uff0c\u5931\u8d25\u8fd4\u56de-1 \r\n     *\/ \r\n    private static int binarySearch(int[] array, int aim, int left, int right) { \r\n        if (aim < array[left] || aim > array[right]) { \r\n            return -1; \r\n        } \r\n        \/\/ \u627e\u4e2d\u95f4\u503c \r\n        int mid = (left + right) \/ 2; \r\n        if (array[mid] == aim) { \r\n            return mid; \r\n        } else if (array[mid] > aim) { \r\n            \/\/\u5982\u679c\u4e2d\u95f4\u503c\u5927\u4e8e\u8981\u627e\u7684\u503c\u5219\u4ece\u5de6\u8fb9\u4e00\u534a\u7ee7\u7eed\u9012\u5f52 \r\n            return binarySearch(array, aim, left, mid - 1); \r\n        } else { \r\n            \/\/\u5982\u679c\u4e2d\u95f4\u503c\u5c0f\u4e8e\u8981\u627e\u7684\u503c\u5219\u4ece\u53f3\u8fb9\u4e00\u534a\u7ee7\u7eed\u9012\u5f52 \r\n            return binarySearch(array, aim, mid + 1, array.length-1); \r\n        } \r\n    } \r\n} <\/pre>\n

\u9012\u5f52\u76f8\u8f83\u4e8e\u5faa\u73af\uff0c\u4ee3\u7801\u6bd4\u8f83\u7b80\u6d01\uff0c\u4f46\u662f\u65f6\u95f4\u548c\u7a7a\u95f4\u6d88\u8017\u6bd4\u8f83\u5927\uff0c\u6548\u7387\u4f4e\u3002\u5728\u5b9e\u9645\u7684\u5b66\u4e60\u4e0e\u5de5\u4f5c\u4e2d\uff0c\u6839\u636e\u60c5\u51b5\u9009\u62e9\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5f53\u6570\u7ec4\u6216\u8005\u96c6\u5408\u4e2d\u5b58\u653e\u7684\u5143\u7d20\u6570\u91cf\u975e\u5e38\u591a\u7684\u65f6\u5019\uff0c\u60f3\u8981\u8ddf\u8e2a\u5177\u4f53\u67d0\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\u6216\u8005\u662f\u5426\u5b58\u5728\uff0c\u5e38\u89c4\u65b9\u5f0f\u662f\u5faa\u73af\u6bcf\u4e00\u4e2a\u5143\u7d20\u76f4 […]<\/p>\n","protected":false},"author":317,"featured_media":11719,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-206612","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/206612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/317"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=206612"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/206612\/revisions"}],"predecessor-version":[{"id":206614,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/206612\/revisions\/206614"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/11719"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=206612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=206612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=206612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}