{"id":18,"date":"2020-03-15T04:10:48","date_gmt":"2020-03-15T04:10:48","guid":{"rendered":"http:\/\/54.153.35.165\/feellikelearning\/?p=18"},"modified":"2020-03-15T09:26:33","modified_gmt":"2020-03-15T09:26:33","slug":"binarysearch","status":"publish","type":"post","link":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/","title":{"rendered":"\u7ecf\u5178\u7b97\u6cd5 binary search \u4e8c\u5206\u67e5\u627e\u600e\u4e48\u5b9e\u73b0\uff1f"},"content":{"rendered":"\n<p>\u4e8c\u5206\u67e5\u627e\u5f88\u7ecf\u5178\uff0c\u5f53\u5e74\u770b\u54c8\u4f5bCS50\u662f\u8fd9\u4e48\u8bb2\u89e3\u7684\uff1a\u600e\u4e48\u5728\u5b57\u5178\u91cc\u627e\u67d0\u4e2a\u5355\u8bcd\uff1f\u6211\u4eec\u77e5\u9053\u5b57\u5178\u662f\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u597d\u5e8f\u7684\u3002\u6bd4\u5982\u76ee\u6807\u5355\u8bcd\u662fcat\uff0c\u7ffb\u5f00\u5b57\u5178\u4e2d\u95f4\uff0c\u73b0\u5728\u662fg\u5f00\u5934\u7684\u8bcd\uff0c\u90a3\u4e48\u6211\u4eec\u77e5\u9053cat\u80af\u5b9a\u4e0d\u5728\u540e\u534a\u90e8\u5206\uff0c\u90a3\u4e48\u53ef\u4ee5\u628a\u5b57\u5178\u6495\u5f00\uff0c\u6254\u4e86\u540e\u534a\u90e8\u5206\u3002\u7136\u540e\u91cd\u590d\u4ee5\u4e0a\u8fc7\u7a0b\uff0c\u4e2d\u95f4\u7ffb\u5f00\uff0c\u8fd9\u56de\u662fb\u5f00\u5934\u7684\uff0c\u5c31\u53ef\u4ee5\u628a\u524d\u534a\u90e8\u5206\u4ecd\u4e86\u3002\u5982\u6b64\u4e0b\u53bb\uff0c\u6bcf\u6b21\u90fd\u628a\u8981\u641c\u7d22\u7684\u7a7a\u95f4\u51cf\u5c11\u4e00\u534a\uff0c\u8fd9\u6837\u53ea\u7528O(lgN)\u65f6\u95f4\u5c31\u80fd\u627e\u5230\u76ee\u6807\u3002\u4e0d\u7136\u7684\u8bdd\u4e00\u9875\u9875\u627e\u7684\u8bdd\u5c31\u9700\u8981O(N)\u3002\u867d\u7136\u7b97\u6cd5\u4e0d\u590d\u6742\uff0c\u4f46\u662f\u8981\u5199\u5bf9\u8fd8\u662f\u4e0d\u5bb9\u6613\u7684\uff0c\u4ee5\u4e0b\u7ecf\u5178\u5b9e\u73b0\u6700\u597d\u80cc\u4e0b\u6765\uff0c\u5185\u5316\u5230\u81ea\u5df1\u7684\u77e5\u8bc6\u5e93\uff0c\u5c31\u4e0d\u7528\u62c5\u5fc3\u7528\u5230\u7684\u65f6\u5019\u8fb9\u754c\u6761\u4ef6\u4e0d\u5bf9\u3002\u4e0d\u7136\u5f88\u5bb9\u6613\u8fdb\u5165\u6b7b\u5faa\u73af\u4e0d\u8fd4\u56de\uff0c\u4eb2\u8eab\u7ecf\u5386\u3002<\/p>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>Java<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Arrays;\nimport java.util.List;\n\npublic class Binarysearch {\n    public static boolean binarySearch(int target, List&lt;Integer> sortedlist) {\n        if (sortedlist == null || sortedlist.isEmpty()) {\n            return false;\n        }\n        int lower = 0;\n        int upper = sortedlist.size()-1;\n        while (lower &lt;= upper) {\n            int m = lower + (upper - lower) \/ 2;\n            if (target == sortedlist.get(m)) {\n                return true;\n            } else if (target &lt; sortedlist.get(m)) {\n                upper = m - 1;\n            } else {\n                lower = m + 1;\n            }\n        }\n        return false;\n    }\n\n    public static void main(String[] args) {\n        List&lt;Integer> sortedList = Arrays.asList(1, 3, 3, 7, 20, 48, 64, 72, 1024);\n        int [] targets = {20, 32, 55, 72, 100};\n        for (int target : targets) {\n            System.out.println(String.format(\"%d in list? %b\", target, binarySearch(target, sortedList)));\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u4e3b\u8981\u4ee5\u4e0b\u8981\u70b9<\/p>\n\n\n\n<ul><li>while\u91cc\u9762\u6761\u4ef6\u8981\u662f <code>lower &lt;= upper<\/code><\/li><li>update lower bound \u8981\u662f <code>m + 1<\/code><\/li><li>update upper bound \u8981\u662f <code>m - 1<\/code><\/li><li>\u8ba1\u7b97\u4e2d\u6307\u7528 <code>m = lower + (upper - lower) \/ 2<\/code>\uff0c\u800c\u4e0d\u662f <code>(upper + lower) \/ 2<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u53bb\u9664\u6574\u6570\u76f8\u52a0overflow\u95ee\u9898\u3002\u5f53\u7136\u5982\u679c\u7c7b\u578b\u662fbigint\u4e4b\u7c7b\u5c31\u65e0\u6240\u8c13\u4e86\u3002<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4e8c\u5206\u67e5\u627e\u5f88\u7ecf\u5178\uff0c\u5f53\u5e74\u770b\u54c8\u4f5bCS50\u662f\u8fd9\u4e48\u8bb2\u89e3\u7684\uff1a\u600e\u4e48\u5728\u5b57\u5178\u91cc\u627e\u67d0\u4e2a\u5355\u8bcd\uff1f\u6211\u4eec\u77e5\u9053\u5b57\u5178\u662f\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u597d\u5e8f\u7684\u3002\u6bd4\u5982\u76ee\u6807\u5355\u8bcd\u662fcat\uff0c\u7ffb\u5f00\u5b57\u5178\u4e2d\u95f4\uff0c\u73b0\u5728\u662fg\u5f00\u5934\u7684\u8bcd\uff0c\u90a3\u4e48\u6211\u4eec\u77e5\u9053cat\u80af\u5b9a\u4e0d\u5728\u540e\u534a\u90e8\u5206\uff0c\u90a3\u4e48\u53ef\u4ee5\u628a\u5b57\u5178\u6495\u5f00\uff0c\u6254\u4e86\u540e\u534a\u90e8\u5206\u3002\u7136\u540e\u91cd\u590d\u4ee5\u4e0a\u8fc7\u7a0b\uff0c\u4e2d\u95f4\u7ffb\u5f00\uff0c\u8fd9\u56de\u662fb\u5f00\u5934\u7684\uff0c\u5c31\u53ef\u4ee5\u628a\u524d\u534a\u90e8\u5206\u4ecd\u4e86\u3002\u5982\u6b64\u4e0b\u53bb\uff0c\u6bcf\u6b21\u90fd\u628a\u8981\u641c\u7d22\u7684\u7a7a\u95f4\u51cf\u5c11\u4e00\u534a\uff0c\u8fd9\u6837\u53ea\u7528O(lgN)\u65f6\u95f4\u5c31\u80fd\u627e\u5230\u76ee\u6807\u3002\u4e0d\u7136\u7684\u8bdd\u4e00\u9875\u9875\u627e\u7684\u8bdd\u5c31\u9700\u8981O(N)\u3002\u867d\u7136\u7b97\u6cd5\u4e0d\u590d\u6742\uff0c\u4f46\u662f\u8981\u5199\u5bf9\u8fd8\u662f\u4e0d\u5bb9\u6613\u7684\uff0c\u4ee5\u4e0b\u7ecf\u5178\u5b9e\u73b0\u6700\u597d\u80cc\u4e0b\u6765\uff0c\u5185\u5316\u5230\u81ea\u5df1\u7684\u77e5\u8bc6\u5e93\uff0c\u5c31\u4e0d\u7528\u62c5\u5fc3\u7528\u5230\u7684\u65f6\u5019\u8fb9\u754c\u6761\u4ef6\u4e0d\u5bf9\u3002\u4e0d\u7136\u5f88\u5bb9\u6613\u8fdb\u5165\u6b7b\u5faa\u73af\u4e0d\u8fd4\u56de\uff0c\u4eb2\u8eab\u7ecf\u5386\u3002 \u4ee3\u7801\u5982\u4e0b\uff1a Java \u4e3b\u8981\u4ee5\u4e0b\u8981\u70b9 while\u91cc\u9762\u6761\u4ef6\u8981\u662f lower &lt;= upper update lower bound \u8981\u662f m + 1 update upper bound \u8981\u662f m &#8211; 1 \u8ba1\u7b97\u4e2d\u6307\u7528 m = lower + (upper &#8211; lower) \/ 2\uff0c\u800c\u4e0d\u662f (upper + lower) \/ 2\uff0c\u8fd9\u6837\u53ef\u4ee5\u53bb\u9664\u6574\u6570\u76f8\u52a0overflow\u95ee\u9898\u3002\u5f53\u7136\u5982\u679c\u7c7b\u578b\u662fbigint\u4e4b\u7c7b\u5c31\u65e0\u6240\u8c13\u4e86\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.10 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u7ecf\u5178\u7b97\u6cd5 binary search \u4e8c\u5206\u67e5\u627e\u600e\u4e48\u5b9e\u73b0\uff1f | Feel Like Learning<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u7ecf\u5178\u7b97\u6cd5 binary search \u4e8c\u5206\u67e5\u627e\u600e\u4e48\u5b9e\u73b0\uff1f | Feel Like Learning\" \/>\n<meta property=\"og:description\" content=\"\u4e8c\u5206\u67e5\u627e\u5f88\u7ecf\u5178\uff0c\u5f53\u5e74\u770b\u54c8\u4f5bCS50\u662f\u8fd9\u4e48\u8bb2\u89e3\u7684\uff1a\u600e\u4e48\u5728\u5b57\u5178\u91cc\u627e\u67d0\u4e2a\u5355\u8bcd\uff1f\u6211\u4eec\u77e5\u9053\u5b57\u5178\u662f\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u597d\u5e8f\u7684\u3002\u6bd4\u5982\u76ee\u6807\u5355\u8bcd\u662fcat\uff0c\u7ffb\u5f00\u5b57\u5178\u4e2d\u95f4\uff0c\u73b0\u5728\u662fg\u5f00\u5934\u7684\u8bcd\uff0c\u90a3\u4e48\u6211\u4eec\u77e5\u9053cat\u80af\u5b9a\u4e0d\u5728\u540e\u534a\u90e8\u5206\uff0c\u90a3\u4e48\u53ef\u4ee5\u628a\u5b57\u5178\u6495\u5f00\uff0c\u6254\u4e86\u540e\u534a\u90e8\u5206\u3002\u7136\u540e\u91cd\u590d\u4ee5\u4e0a\u8fc7\u7a0b\uff0c\u4e2d\u95f4\u7ffb\u5f00\uff0c\u8fd9\u56de\u662fb\u5f00\u5934\u7684\uff0c\u5c31\u53ef\u4ee5\u628a\u524d\u534a\u90e8\u5206\u4ecd\u4e86\u3002\u5982\u6b64\u4e0b\u53bb\uff0c\u6bcf\u6b21\u90fd\u628a\u8981\u641c\u7d22\u7684\u7a7a\u95f4\u51cf\u5c11\u4e00\u534a\uff0c\u8fd9\u6837\u53ea\u7528O(lgN)\u65f6\u95f4\u5c31\u80fd\u627e\u5230\u76ee\u6807\u3002\u4e0d\u7136\u7684\u8bdd\u4e00\u9875\u9875\u627e\u7684\u8bdd\u5c31\u9700\u8981O(N)\u3002\u867d\u7136\u7b97\u6cd5\u4e0d\u590d\u6742\uff0c\u4f46\u662f\u8981\u5199\u5bf9\u8fd8\u662f\u4e0d\u5bb9\u6613\u7684\uff0c\u4ee5\u4e0b\u7ecf\u5178\u5b9e\u73b0\u6700\u597d\u80cc\u4e0b\u6765\uff0c\u5185\u5316\u5230\u81ea\u5df1\u7684\u77e5\u8bc6\u5e93\uff0c\u5c31\u4e0d\u7528\u62c5\u5fc3\u7528\u5230\u7684\u65f6\u5019\u8fb9\u754c\u6761\u4ef6\u4e0d\u5bf9\u3002\u4e0d\u7136\u5f88\u5bb9\u6613\u8fdb\u5165\u6b7b\u5faa\u73af\u4e0d\u8fd4\u56de\uff0c\u4eb2\u8eab\u7ecf\u5386\u3002 \u4ee3\u7801\u5982\u4e0b\uff1a Java \u4e3b\u8981\u4ee5\u4e0b\u8981\u70b9 while\u91cc\u9762\u6761\u4ef6\u8981\u662f lower &lt;= upper update lower bound \u8981\u662f m + 1 update upper bound \u8981\u662f m - 1 \u8ba1\u7b97\u4e2d\u6307\u7528 m = lower + (upper - lower) \/ 2\uff0c\u800c\u4e0d\u662f (upper + lower) \/ 2\uff0c\u8fd9\u6837\u53ef\u4ee5\u53bb\u9664\u6574\u6570\u76f8\u52a0overflow\u95ee\u9898\u3002\u5f53\u7136\u5982\u679c\u7c7b\u578b\u662fbigint\u4e4b\u7c7b\u5c31\u65e0\u6240\u8c13\u4e86\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant\" \/>\n<meta property=\"og:site_name\" content=\"Feel Like Learning\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-15T04:10:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-03-15T09:26:33+00:00\" \/>\n<meta name=\"author\" content=\"feellikelearning\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"feellikelearning\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant#article\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant\"},\"author\":{\"name\":\"feellikelearning\",\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"headline\":\"\u7ecf\u5178\u7b97\u6cd5 binary search \u4e8c\u5206\u67e5\u627e\u600e\u4e48\u5b9e\u73b0\uff1f\",\"datePublished\":\"2020-03-15T04:10:48+00:00\",\"dateModified\":\"2020-03-15T09:26:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant\"},\"wordCount\":21,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant\",\"url\":\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant\",\"name\":\"\u7ecf\u5178\u7b97\u6cd5 binary search \u4e8c\u5206\u67e5\u627e\u600e\u4e48\u5b9e\u73b0\uff1f | Feel Like Learning\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/#website\"},\"datePublished\":\"2020-03-15T04:10:48+00:00\",\"dateModified\":\"2020-03-15T09:26:33+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/feellikelearning.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u7ecf\u5178\u7b97\u6cd5 binary search \u4e8c\u5206\u67e5\u627e\u600e\u4e48\u5b9e\u73b0\uff1f\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/feellikelearning.com\/#website\",\"url\":\"https:\/\/feellikelearning.com\/\",\"name\":\"Feel Like Learning\",\"description\":\"\u7a0b\u5e8f\uff5c\u751f\u6d3b\uff5c\u5b66\u5230\u5c31\u662f\u8d5a\u5230\",\"publisher\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/feellikelearning.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\",\"name\":\"feellikelearning\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g\",\"caption\":\"feellikelearning\"},\"logo\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/feellikelearning.com\/index.php\/author\/feellikelearning\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u7ecf\u5178\u7b97\u6cd5 binary search \u4e8c\u5206\u67e5\u627e\u600e\u4e48\u5b9e\u73b0\uff1f | Feel Like Learning","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant","og_locale":"en_US","og_type":"article","og_title":"\u7ecf\u5178\u7b97\u6cd5 binary search \u4e8c\u5206\u67e5\u627e\u600e\u4e48\u5b9e\u73b0\uff1f | Feel Like Learning","og_description":"\u4e8c\u5206\u67e5\u627e\u5f88\u7ecf\u5178\uff0c\u5f53\u5e74\u770b\u54c8\u4f5bCS50\u662f\u8fd9\u4e48\u8bb2\u89e3\u7684\uff1a\u600e\u4e48\u5728\u5b57\u5178\u91cc\u627e\u67d0\u4e2a\u5355\u8bcd\uff1f\u6211\u4eec\u77e5\u9053\u5b57\u5178\u662f\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u597d\u5e8f\u7684\u3002\u6bd4\u5982\u76ee\u6807\u5355\u8bcd\u662fcat\uff0c\u7ffb\u5f00\u5b57\u5178\u4e2d\u95f4\uff0c\u73b0\u5728\u662fg\u5f00\u5934\u7684\u8bcd\uff0c\u90a3\u4e48\u6211\u4eec\u77e5\u9053cat\u80af\u5b9a\u4e0d\u5728\u540e\u534a\u90e8\u5206\uff0c\u90a3\u4e48\u53ef\u4ee5\u628a\u5b57\u5178\u6495\u5f00\uff0c\u6254\u4e86\u540e\u534a\u90e8\u5206\u3002\u7136\u540e\u91cd\u590d\u4ee5\u4e0a\u8fc7\u7a0b\uff0c\u4e2d\u95f4\u7ffb\u5f00\uff0c\u8fd9\u56de\u662fb\u5f00\u5934\u7684\uff0c\u5c31\u53ef\u4ee5\u628a\u524d\u534a\u90e8\u5206\u4ecd\u4e86\u3002\u5982\u6b64\u4e0b\u53bb\uff0c\u6bcf\u6b21\u90fd\u628a\u8981\u641c\u7d22\u7684\u7a7a\u95f4\u51cf\u5c11\u4e00\u534a\uff0c\u8fd9\u6837\u53ea\u7528O(lgN)\u65f6\u95f4\u5c31\u80fd\u627e\u5230\u76ee\u6807\u3002\u4e0d\u7136\u7684\u8bdd\u4e00\u9875\u9875\u627e\u7684\u8bdd\u5c31\u9700\u8981O(N)\u3002\u867d\u7136\u7b97\u6cd5\u4e0d\u590d\u6742\uff0c\u4f46\u662f\u8981\u5199\u5bf9\u8fd8\u662f\u4e0d\u5bb9\u6613\u7684\uff0c\u4ee5\u4e0b\u7ecf\u5178\u5b9e\u73b0\u6700\u597d\u80cc\u4e0b\u6765\uff0c\u5185\u5316\u5230\u81ea\u5df1\u7684\u77e5\u8bc6\u5e93\uff0c\u5c31\u4e0d\u7528\u62c5\u5fc3\u7528\u5230\u7684\u65f6\u5019\u8fb9\u754c\u6761\u4ef6\u4e0d\u5bf9\u3002\u4e0d\u7136\u5f88\u5bb9\u6613\u8fdb\u5165\u6b7b\u5faa\u73af\u4e0d\u8fd4\u56de\uff0c\u4eb2\u8eab\u7ecf\u5386\u3002 \u4ee3\u7801\u5982\u4e0b\uff1a Java \u4e3b\u8981\u4ee5\u4e0b\u8981\u70b9 while\u91cc\u9762\u6761\u4ef6\u8981\u662f lower &lt;= upper update lower bound \u8981\u662f m + 1 update upper bound \u8981\u662f m - 1 \u8ba1\u7b97\u4e2d\u6307\u7528 m = lower + (upper - lower) \/ 2\uff0c\u800c\u4e0d\u662f (upper + lower) \/ 2\uff0c\u8fd9\u6837\u53ef\u4ee5\u53bb\u9664\u6574\u6570\u76f8\u52a0overflow\u95ee\u9898\u3002\u5f53\u7136\u5982\u679c\u7c7b\u578b\u662fbigint\u4e4b\u7c7b\u5c31\u65e0\u6240\u8c13\u4e86\u3002","og_url":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant","og_site_name":"Feel Like Learning","article_published_time":"2020-03-15T04:10:48+00:00","article_modified_time":"2020-03-15T09:26:33+00:00","author":"feellikelearning","twitter_card":"summary_large_image","twitter_misc":{"Written by":"feellikelearning","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant#article","isPartOf":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant"},"author":{"name":"feellikelearning","@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"headline":"\u7ecf\u5178\u7b97\u6cd5 binary search \u4e8c\u5206\u67e5\u627e\u600e\u4e48\u5b9e\u73b0\uff1f","datePublished":"2020-03-15T04:10:48+00:00","dateModified":"2020-03-15T09:26:33+00:00","mainEntityOfPage":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant"},"wordCount":21,"commentCount":0,"publisher":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant#respond"]}]},{"@type":"WebPage","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant","url":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant","name":"\u7ecf\u5178\u7b97\u6cd5 binary search \u4e8c\u5206\u67e5\u627e\u600e\u4e48\u5b9e\u73b0\uff1f | Feel Like Learning","isPartOf":{"@id":"https:\/\/feellikelearning.com\/#website"},"datePublished":"2020-03-15T04:10:48+00:00","dateModified":"2020-03-15T09:26:33+00:00","breadcrumb":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/15\/binarysearch\/?variant=zh-hant#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/feellikelearning.com\/"},{"@type":"ListItem","position":2,"name":"\u7ecf\u5178\u7b97\u6cd5 binary search \u4e8c\u5206\u67e5\u627e\u600e\u4e48\u5b9e\u73b0\uff1f"}]},{"@type":"WebSite","@id":"https:\/\/feellikelearning.com\/#website","url":"https:\/\/feellikelearning.com\/","name":"Feel Like Learning","description":"\u7a0b\u5e8f\uff5c\u751f\u6d3b\uff5c\u5b66\u5230\u5c31\u662f\u8d5a\u5230","publisher":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/feellikelearning.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a","name":"feellikelearning","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g","caption":"feellikelearning"},"logo":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/"},"url":"https:\/\/feellikelearning.com\/index.php\/author\/feellikelearning\/"}]}},"_links":{"self":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/18"}],"collection":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":5,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":42,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions\/42"}],"wp:attachment":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}