{"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":"
\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 |