{"id":315,"date":"2021-08-23T15:39:59","date_gmt":"2021-08-23T07:39:59","guid":{"rendered":"http:\/\/blog.nonot.cn\/?p=315"},"modified":"2021-08-23T15:39:59","modified_gmt":"2021-08-23T07:39:59","slug":"%e5%90%84%e7%a7%8d%e6%8e%92%e5%ba%8f%e7%ae%97%e6%b3%95%e7%9a%84%e6%97%b6%e9%97%b4%e5%a4%8d%e6%9d%82%e5%ba%a6%e5%92%8c%e7%a9%ba%e9%97%b4%e5%a4%8d%e6%9d%82%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/blog.nonot.cn\/index.php\/2021\/08\/23\/%e5%90%84%e7%a7%8d%e6%8e%92%e5%ba%8f%e7%ae%97%e6%b3%95%e7%9a%84%e6%97%b6%e9%97%b4%e5%a4%8d%e6%9d%82%e5%ba%a6%e5%92%8c%e7%a9%ba%e9%97%b4%e5%a4%8d%e6%9d%82%e5%ba%a6\/","title":{"rendered":"\u5404\u79cd\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/blog.nonot.cn\/wp-content\/uploads\/2021\/08\/20210823154017-d9165.png\" alt=\"\" \/><\/p>\n<p>\u00a0<\/p>\n<p>\u5176\u4e2d\u5192\u6ce1\u6392\u5e8f\u52a0\u4e2a\u6807\u5fd7\uff0c\u6240\u4ee5\u6700\u597d\u60c5\u51b5\u4e0b\u662fo(n)<\/p>\n<p>\u00a0\u76f4\u63a5\u9009\u62e9\u6392\u5e8f\uff1a<\/p>\n<p align=\"left\"><strong>\u6392\u5e8f\u8fc7\u7a0b\uff1a<\/strong><\/p>\n<p align=\"left\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a01\u00a0\u3001\u9996\u5148\u5728\u6240\u6709\u6570\u636e\u4e2d\u7ecf\u8fc7\u00a0n-1\u6b21\u6bd4\u8f83\u9009\u51fa\u6700\u5c0f\u7684\u6570\uff0c\u628a\u5b83\u4e0e\u7b2c\u00a01\u4e2a\u6570\u636e\u4ea4\u6362\uff0c<\/span><\/p>\n<p align=\"left\"><span lang=\"EN-US\">\u00a0 \u00a0 \u00a0 \u00a02\u3001\u7136\u540e\u5728\u5176\u4f59\u7684\u6570\u636e\u5185\u9009\u51fa\u6392\u5e8f\u7801\u6700\u5c0f\u7684\u6570\uff0c\u4e0e\u7b2c\u00a02\u4e2a\u6570\u636e\u4ea4\u6362&#8230;&#8230;\u00a0\u4f9d\u6b21\u7c7b\u63a8\uff0c\u76f4\u5230\u6240\u6709\u6570\u636e\u6392\u5b8c\u4e3a\u6b62\u3002<\/span><\/p>\n<p align=\"left\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5728\u7b2ci\u00a0\u8d9f\u6392\u5e8f\u4e2d\u9009\u51fa\u6700\u5c0f\u5173\u952e\u5b57\u7684\u6570\u636e\uff0c\u9700\u8981\u505a\u00a0n-i\u6b21\u6bd4\u8f83\u3002\u00a0\u00a0<\/span><\/p>\n<div class=\"likecs_code\">\n<pre>\/\/\u5192\u6ce1\u6392\u5e8f\uff0c\u5927\u7684\u6570\u4e0d\u65ad\u5411\u540e\u5192\u6ce1\nvoid buddle(vector&lt;int&gt;&amp; nums)\n{\n    int len=nums.size();\n    for(int i=0;i&lt;len-1;i++)\n    {\n        for(int j=0;j&lt;len-1-i;j++)\n        {\n            if(nums[j]&gt;nums[j+1])\n                swap(nums[j],nums[j+1]);\n        }\n    }\n     \n}<\/pre>\n<\/div>\n<div id=\"likecs_post_body\">\n<h1>\u7ebf\u6027\u6392\u5e8f\u7b97\u6cd5<\/h1>\n<h2>\u8ba1\u6570\u6392\u5e8f<\/h2>\n<p>\u5047\u8bbe\uff1a\u6709n\u4e2a\u6570\u7684\u96c6\u5408\uff0c\u800c\u4e14n\u4e2a\u6570\u7684\u8303\u56f4\u90fd\u57280~k(k = O(n))\u4e4b\u95f4\u3002<\/p>\n<p>\u8fd0\u884c\u65f6\u95f4\uff1a\u0398(n+k)<\/p>\n<p><a href=\"https:\/\/www.likecs.com\/default\/index\/url?u=aHR0cDovL2ltYWdlcy5jbmJsb2dzLmNvbS9jbmJsb2dzX2NvbS96YWJlcnkvMjAxMTA3LzIwMTEwNzMwMTg0MzI3Nzg2OC5wbmc%3D\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" src=\"http:\/\/blog.nonot.cn\/wp-content\/uploads\/2021\/08\/20210823154019-dbadf.png\" alt=\"image\" width=\"482\" height=\"234\" border=\"0\" \/><\/a><\/p>\n<p>\u5f85\u6392\u5e8f\u6570\u7ec4A\u5982\u56fe2.1\u6240\u793a\uff0c\u9700\u8981\u8f85\u52a9\u6570\u7ec4B(\u5b58\u50a8\u6700\u540e\u6392\u5e8f\u7ed3\u679c)\uff0c\u6570\u7ec4C(\u5b58\u50a8\u5143\u7d20\u7684\u4e2a\u6570)\u3002\u57fa\u4e8e\u4e0a\u8ff0\u7684\u5047\u8bbe\uff0c\u6570\u7ec4C\u7684\u5927\u5c0f\u4e3ak\uff0cC[i]\u8868\u793a\u6570\u7ec4A\u4e2d\u5143\u7d20i(0 &lt;= i &lt; k)\u7684\u4e2a\u6570(\u5982\u56fe2.2\u6240\u793a)\uff0c\u4e3a\u4e86\u4fdd\u8bc1\u8ba1\u6570\u6392\u5e8f\u7684\u7a33\u5b9a\u6027\uff0c\u6570\u7ec4C\u53d8\u5316\u4e3a\u56fe2.3\uff0cC[i]\u8868\u793a\u5c0f\u4e8e\u6216\u8005\u7b49\u4e8ei\u7684\u4e2a\u6570\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<div id=\"codeSnippetWrapper\">\n<div id=\"codeSnippet\">\n<div class=\"likecs_code\">\n<pre class=\"\">  1: \/*\n   2:     \u8f93\u5165\uff1a\u5f85\u6392\u5e8f\u6570\u7ec4A,\u5b58\u50a8\u6392\u5e8f\u540e\u7684\u6570\u7ec4B\uff0c\u6570\u7ec4A\u7684\u5927\u5c0f\uff0c\u6570\u7ec4C\u7684\u5927\u5c0f\n   3:     \u529f\u80fd\uff1a\u8ba1\u6570\u6392\u5e8f\n   4: *\/\n   5: void CountingSort(int A[], int B[], int len, int k)\n   6: {\n   7:     int *CountArr = new int[k];\n   8:     int i;\n   9:     for (i = 0; i &lt; k; i++)\n  10:     {\n  11:         CountArr[i] = 0;\n  12:     }\n  13:  \n  14:     for (i = 0; i &lt; len; i++)\n  15:     {\n  16:         CountArr[A[i]]++;                \n  17:     }\n  18:  \n  19:     for (i = 1; i &lt; k; i++)\n  20:     {\n  21:         CountArr[i] += CountArr[i-1];\n  22:     }\n  23:  \n  24:     \/\/ \u4ece\u53f3\u81f3\u5de6\u4fdd\u8bc1\u7b97\u6cd5\u7684\u7a33\u5b9a\u6027\n  25:     for (i = len-1; i &gt;=0; i--)\n  26:     {\n  27:         B[CountArr[A[i]]-1] = A[i];\n  28:         CountArr[A[i]]--;\n  29:     }\n  30: }<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p>9-12\u884c\u548c19-22\u884c\u7684\u8fd0\u884c\u65f6\u95f4\u0398(k)\uff0c14-17\u884c\u548c25-29\u884c\u7684\u8fd0\u884c\u65f6\u95f4\u4e3a\u0398(n)\uff0c\u6240\u4ee5\u603b\u7684\u8fd0\u884c\u65f6\u95f4\u4e3a\u0398(2(n+k)) = \u0398(n+k)\u3002<\/p>\n<h2>\u57fa\u6570\u6392\u5e8f<\/h2>\n<p>\u57fa\u6570\u6392\u5e8f\uff1a\u5c06\u6240\u6709\u5f85\u6bd4\u8f83\u6570\u503c(\u6b63\u6574\u6570)\u7edf\u4e00\u4e3a\u540c\u6837\u7684\u6570\u4f4d\u957f\u5ea6\uff0c\u6570\u4f4d\u8f83\u77ed\u7684\u6570\u524d\u9762\u8865\u96f6\u3002\u7136\u540e\uff0c\u4ece\u6700\u4f4e\u4f4d\u5f00\u59cb\uff0c\u4f9d\u6b21\u8fdb\u884c\u4e00\u6b21\u6392\u5e8f\u3002\u8fd9\u6837\u4ece\u6700\u4f4e\u4f4d\u6392\u5e8f\u4e00\u76f4\u5230\u6700\u9ad8\u4f4d\u6392\u5e8f\u5b8c\u6210\u4ee5\u540e\uff0c \u6570\u5217\u5c31\u53d8\u6210\u4e00\u4e2a\u6709\u5e8f\u5e8f\u5217\u3002<\/p>\n<p>\u57fa\u6570\u6392\u5e8f\u5206\u4e3a\u4e24\u79cdLSD\u548cMSD\u3002<\/p>\n<p>LSD(Least significant digital)\uff1a\u6700\u4f4e\u6709\u6548\u4f4d\u4f18\u5148\uff0c\u5373\u4ece\u53f3\u5411\u5de6\u5f00\u59cb\u6392\u5e8f\u3002<\/p>\n<p>MSD(Most significant digital)\uff1a\u6700\u9ad8\u6709\u6548\u4f4d\u4f18\u5148\uff0c\u5373\u4ece\u5de6\u5f80\u53f3\u5f00\u59cb\u6392\u5e8f\u3002<\/p>\n<p>\u4ee5\u4e0b\u662fLSD\u65b9\u5f0f\u7684\u57fa\u6570\u6392\u5e8f\u7684\u4f2a\u4ee3\u7801<\/p>\n<p><span id=\"lnum1\"> 1: RadixSort(A,d)<\/span><\/p>\n<p><span id=\"lnum2\"> 2: for i &lt;- 1 to d<\/span><\/p>\n<p><span id=\"lnum3\"> 3: \u7528<strong>\u7a33\u5b9a\u7684\u6392\u5e8f\u7b97\u6cd5<\/strong>\u6392\u5217\u6570\u7ec4A\u4e2d\u5143\u7d20\u7684\u7b2ci\u4f4d<\/span><\/p>\n<p><a href=\"https:\/\/www.likecs.com\/default\/index\/url?u=aHR0cDovL2ltYWdlcy5jbmJsb2dzLmNvbS9jbmJsb2dzX2NvbS96YWJlcnkvMjAxMTA3LzIwMTEwNzMwMTg0MzMyMTgzMy5wbmc%3D\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" src=\"http:\/\/blog.nonot.cn\/wp-content\/uploads\/2021\/08\/20210823154019-b001a.png\" alt=\"image\" width=\"379\" height=\"186\" border=\"0\" \/><\/a><\/p>\n<p>\u5982\u56fe3\uff1a\u5148\u724c\u4e2a\u4f4d\uff0c\u7136\u540e\u5341\u4f4d\uff0c\u6700\u540e\u767e\u4f4d\u3002\u4e3a\u6570\u7ec4\u7684\u67d0\u4e00\u4f4d\u6392\u5e8f\u7684\u65f6\u5019\u4e00\u5b9a\u9700\u8981\u7a33\u5b9a\u7684\u7b97\u6cd5\u3002<\/p>\n<p>\u8fd0\u884c\u65f6\u95f4\u4e3a\u0398(d(n+k))\u3002\u5728\u57fa\u6570\u6392\u5e8f\u4e2d\u6392\u5217\u6570\u7ec4\u5404\u4f4d\u7684\u7b97\u6cd5\u662f\u8ba1\u6570\u6392\u5e8f\u6240\u4ee5\u8fd0\u884c\u65f6\u95f4\u4e3a\u0398(n+k)\uff0c\u53c8d\u662f\u6570\u7ec4\u4e2d\u6570\u7684\u6700\u5927\u4f4d\u6570\u3002<\/p>\n<h3>\u00a0<\/h3>\n<h2>\u6876\u6392\u5e8f<\/h2>\n<p>\u6876\u6392\u5e8f\uff1a\u5c06\u6570\u7ec4\u5206\u5230\u6709\u9650\u4e2a\u6876\u5b50\u5185\uff0c\u7136\u540e\u518d\u5bf9\u6876\u5b50\u91cc\u9762\u7684\u5e8f\u5217\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd0\u884c\u65f6\u95f4\u0398(n)\u3002\u6876\u6392\u5e8f\u57fa\u4e8e\u4e00\u4e2a\u5047\u8bbe\uff1a\u8f93\u5165\u7684\u6570\u636e\u7531\u968f\u673a\u8fc7\u7a0b\u6784\u6210\uff0c\u5426\u5219\u5728\u6700\u574f\u60c5\u51b5\u4e0b\u90fd\u5206\u914d\u5230\u4e00\u4e2a\u6876\u5b50\u91cc\u9762\uff0c\u5982\u679c\u53c8\u4e0d\u6ee1\u8db3\u8ba1\u6570\u6392\u5e8f\u7684\u5047\u8bbe\u8981\u6c42\uff0c\u90a3\u4e48\u53ea\u80fd\u4f7f\u7528\u57fa\u4e8e\u6bd4\u8f83\u7684\u6392\u5e8f\u7b97\u6cd5\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd0\u884c\u65f6\u95f4\u5c31\u9000\u5316\u5230\u03a9(nlogn)\u3002<\/p>\n<h1>\u6392\u5e8f\u7b97\u6cd5\u7a33\u5b9a\u6027<\/h1>\n<p>\u6392\u5e8f\u7b97\u6cd5\u7a33\u5b9a\u6027\uff1a\u5047\u8bbe\u5f85\u6392\u5e8f\u5e8f\u5217\u4e2d\u6709\u4e24\u4e2a\u5143\u7d20\u76f8\u7b49\uff0c\u800c\u4e14\u5728\u6392\u5e8f\u524d\u548c\u6392\u5e8f\u540e\u4e24\u4e2a\u76f8\u7b49\u7684\u5143\u7d20\u7684\u76f8\u5bf9\u4f4d\u7f6e\u4e0d\u53d8\uff0c\u5373\u6709 a = b\uff0c\u6392\u5e8f\u524da\u5728b\u524d\u9762\uff0c\u90a3\u4e48\u6392\u5e8f\u540e\uff0ca\u8fd8\u662f\u8981\u5728b\u524d\u9762\u3002\u6392\u5e8f\u7b97\u6cd5\u7684\u7a33\u5b9a\u6027\u662f\u8981\u770b\u5177\u4f53\u7684\u7b97\u6cd5\u5b9e\u73b0\uff0c\u6bd4\u5982\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u76f4\u63a5\u9009\u62e9\u6392\u5e8f\uff0c\u5feb\u901f\u6392\u5e8f\uff0c\u5e0c\u5c14\u6392\u5e8f\uff0c\u5806\u6392\u5e8f\u90fd\u4e0d\u662f\u7a33\u5b9a\u6392\u5e8f\u7b97\u6cd5\uff0c\u57fa\u6570\u6392\u5e8f\uff0c\u8ba1\u6570\u6392\u5e8f\uff0c\u5f52\u5e76\u6392\u5e8f\uff0c\u63d2\u5165\u6392\u5e8f\uff0c\u5192\u6ce1\u6392\u5e8f\u90fd\u662f\u7a33\u5b9a\u6392\u5e8f\u7b97\u6cd5\u3002<\/p>\n<p>\u5feb\u901f\u6392\u5e8f\uff1aA = {2\uff0c\u00a02\uff0c 1}\uff0c\u6392\u5e8f\u540eA = {1\uff0c2\uff0c2}\u3002<\/p>\n<p>\u5e0c\u5c14\u6392\u5e8f\uff1aA = {1\uff0c2\uff0c5\uff0c4\uff0c4\uff0c7}\uff0c\u6392\u5e8f\u540e\uff08k = 2\uff09\uff1bA = {1\uff0c 2\uff0c\u00a04\uff0c 4\uff0c 5\uff0c 7} \u3002<\/p>\n<p>\u5806\u6392\u5e8f\uff1aA = {2\uff0c2\uff0c1}\uff0c\u6392\u5e8f\u540eA = {1\uff0c2\uff0c 2}\u3002<\/p>\n<p>\u76f4\u63a5\u9009\u62e9\u6392\u5e8f\uff1a A = {4\uff0c\u00a04\uff0c 2\uff0c 5},\u6392\u5e8f\u540e A = {2\uff0c4\uff0c 4\uff0c 5}\u3002<\/p>\n<p>\u4ee5\u4e0a\u4e3e\u4f8b\u90fd\u4e0d\u6ee1\u8db3\u7a33\u5b9a\u6027\u3002<\/p>\n<\/div>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n\n\n<p class=\"wp-block-paragraph\">*\u4e8c\u5206\u67e5\u627e<br>&nbsp; \u4e8c\u5206\u67e5\u627e\u4e5f\u79f0\u4e3a\u6298\u534a\u67e5\u627e\uff0c\u5b83\u662f\u4e00\u79cd\u6548\u7387\u8f83\u9ad8\u7684\u67e5\u627e\u65b9\u6cd5\u3002\u4e8c\u5206\u67e5\u627e\u7684\u4f7f\u7528\u524d\u63d0\u662f\u7ebf\u6027\u8868\u5df2\u7ecf\u6309\u7167\u5927\u5c0f\u6392\u597d\u4e86\u5e8f\u3002\u8fd9\u79cd\u65b9\u6cd5\u5145\u5206\u5229\u7528\u4e86\u5143\u7d20\u95f4\u7684\u6b21\u5e8f\u5173\u7cfb\uff0c\u91c7\u7528\u5206\u6cbb\u7b56\u7565\u3002\u57fa\u672c\u539f\u7406\u662f\uff1a\u9996\u5148\u5728\u6709\u5e8f\u7684\u7ebf\u6027\u8868\u4e2d\u627e\u5230\u4e2d\u503c\uff0c\u5c06\u8981\u67e5\u627e\u7684\u76ee\u6807\u4e0e\u4e2d\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u5982\u679c\u76ee\u6807\u5c0f\u4e8e\u4e2d\u503c\uff0c\u5219\u5728\u524d\u534a\u90e8\u5206\u627e\uff0c\u5982\u679c\u76ee\u6807\u5c0f\u4e8e\u4e2d\u503c\uff0c\u5219\u5728\u540e\u534a\u90e8\u5206\u627e\uff1b\u5047\u8bbe\u5728\u524d\u534a\u90e8\u5206\u627e\uff0c\u5219\u518d\u4e0e\u524d\u534a\u90e8\u5206\u7684\u4e2d\u503c\u76f8\u6bd4\u8f83\uff0c\u5982\u679c\u5c0f\u4e8e\u4e2d\u503c\uff0c\u5219\u5728\u4e2d\u503c\u7684\u524d\u534a\u90e8\u5206\u627e\uff0c\u5982\u679c\u5927\u4e8e\u4e2d\u503c\uff0c\u5219\u5728\u540e\u534a\u90e8\u5206\u627e\u3002\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6807\u4e3a\u6b62\u3002&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; \u5047\u8bbe\u6211\u4eec\u8981\u5728 2\uff0c6\uff0c11\uff0c13\uff0c16\uff0c17\uff0c22\uff0c30\u4e2d\u67e5\u627e22\uff0c\u4e0a\u56fe\u6240\u793a\uff0c\u5219\u67e5\u627e\u6b65\u9aa4\u4e3a\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;\u9996\u5148\u627e\u5230\u4e2d\u503c\uff1a\u4e2d\u503c\u4e3a13\uff08\u4e0b\u6807\uff1aint middle = (0+7)\/2\uff09\uff0c\u5c0622\u4e0e13\u8fdb\u884c\u6bd4\u8f83\uff0c\u53d1\u73b022\u6bd413\u5927\uff0c\u5219\u572813\u7684\u540e\u534a\u90e8\u5206\u627e\uff1b<br>&nbsp;\u5728\u540e\u534a\u90e8\u5206 16\uff0c17\uff0c22\uff0c30\u4e2d\u67e5\u627e22\uff0c\u9996\u5148\u627e\u5230\u4e2d\u503c\uff0c\u4e2d\u503c\u4e3a17\uff08\u4e0b\u6807\uff1aint middle=(0+3)\/2\uff09\uff0c\u5c0622\u4e0e17\u8fdb\u884c\u6bd4\u8f83\uff0c\u53d1\u73b022\u6bd417\u5927\uff0c\u5219\u7ee7\u7eed\u572817\u7684\u540e\u534a\u90e8\u5206\u67e5\u627e\uff1b<br>&nbsp;\u572817\u7684\u540e\u534a\u90e8\u5206 22\uff0c30\u67e5\u627e22\uff0c\u9996\u5148\u627e\u5230\u4e2d\u503c\uff0c\u4e2d\u503c\u4e3a22\uff08\u4e0b\u6807\uff1aint middle=(0+1)\/2\uff09\uff0c\u5c0622\u4e0e22\u8fdb\u884c\u6bd4\u8f83\uff0c\u67e5\u627e\u5230\u7ed3\u679c\u3002<br>&nbsp; \u4e8c\u5206\u67e5\u627e\u5927\u5927\u964d\u4f4e\u4e86\u6bd4\u8f83\u6b21\u6570\uff0c\u4e8c\u5206\u67e5\u627e\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1aO(logn)\uff0c\u5373\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b\u4ee3\u7801\uff1a&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class BinarySearch {\n\npublic static void main(String[] args) {\nint arr[] = {2, 6, 11, 13, 16, 17, 22, 30};\nSystem.out.println(\"\u975e\u9012\u5f52\u7ed3\u679c,22\u7684\u4f4d\u7f6e\u4e3a:\" + binarySearch(arr, 22));\nSystem.out.println(\"\u9012\u5f52\u7ed3\u679c,22\u7684\u4f4d\u7f6e\u4e3a:\" + binarySearch(arr, 22, 0, 7));\n}\n\n\n\/\/\u975e\u9012\u5f52\nstatic int binarySearch(int[] arr, int res) {\n\nint low = 0; \nint high = arr.length-1; \nwhile(low &lt;= high) {\nint middle = (low + high)\/2;\nif(res == arr[middle]) {\nreturn middle; \n}else if(res &lt;arr[middle]) { \nhigh = middle - 1; \n}else { \nlow = middle + 1; \n}\n}\nreturn -1; \n}\n\n\/\/\u9012\u5f52\nstatic int binarySearch(int[] arr,int res,int low,int high){ \n\nif(res &lt; arr[low] || res &gt; arr[high] || low &gt; high){\nreturn -1;\n}\nint middle = (low+high)\/2;\nif(res &lt; arr[middle]){ \nreturn binarySearch(arr, res, low, middle-1); \n}else if(res &gt; arr[middle]){ \nreturn binarySearch(arr, res, middle+1, high); \n}else { \nreturn middle; \n} \n} \n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u5176\u4e2d\u5192\u6ce1\u6392\u5e8f\u52a0\u4e2a\u6807\u5fd7\uff0c\u6240\u4ee5\u6700\u597d\u60c5\u51b5\u4e0b\u662fo(n) \u00a0\u76f4\u63a5\u9009&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-315","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/comments?post=315"}],"version-history":[{"count":0,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/315\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/media?parent=315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/categories?post=315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/tags?post=315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}