{"id":266,"date":"2026-03-22T09:57:05","date_gmt":"2026-03-22T01:57:05","guid":{"rendered":"http:\/\/47.110.38.12\/?p=266"},"modified":"2026-03-22T22:29:56","modified_gmt":"2026-03-22T14:29:56","slug":"day1","status":"publish","type":"post","link":"https:\/\/edryan.top\/index.php\/2026\/03\/22\/day1\/","title":{"rendered":"5\u5929\u901f\u6210\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5-Day1"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">5\u5929\u901f\u6210\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5-Day1<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u6392\u5e8f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5192\u6ce1\u6392\u5e8f<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u4ecb\u7ecd<\/h4>\n\n\n\n<p>\u5192\u6ce1\u6392\u5e8f\uff08Bubble Sort\uff09\u7684\u6838\u5fc3\u601d\u60f3\u975e\u5e38\u7b80\u5355\uff1a<strong>\u4ece\u5934\u5f00\u59cb\uff0c\u4e0d\u65ad\u6bd4\u8f83\u76f8\u90bb\u7684\u4e24\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u524d\u8005\u6bd4\u540e\u8005\u5927\uff0c\u5c31\u4ea4\u6362\u4f4d\u7f6e\u3002<\/strong> \u8fd9\u6837\u4ece\u5934\u5230\u5c3e\u5b8c\u6574\u5730\u8d70\u4e00\u8d9f\uff0c\u6700\u5927\u7684\u90a3\u4e2a\u5143\u7d20\u5c31\u5fc5\u7136\u4f1a\u201c\u6c89\u201d\u5230\u961f\u5c3e\u7684\u6700\u7ec8\u4f4d\u7f6e\u3002\u53ea\u9700\u4e0d\u65ad\u91cd\u590d\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u6bcf\u4e00\u8f6e\u90fd\u5c06\u5f53\u524d\u672a\u6392\u5e8f\u90e8\u5206\u7684\u6700\u5927\u503c\u627e\u51fa\u6765\u9001\u5230\u672b\u7aef\uff0c\u76f4\u5230\u6240\u6709\u5143\u7d20\u90fd\u6392\u5217\u6574\u9f50\u3002\u56e0\u4e3a\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u8f83\u5c0f\u7684\u5143\u7d20\u4f1a\u50cf\u6c14\u6ce1\u4e00\u6837\u9010\u6e10\u201c\u5192\u201d\u5230\u6570\u7ec4\u7684\u524d\u65b9\uff0c\u6240\u4ee5\u53eb\u5192\u6ce1\u6392\u5e8f\u6545\u5f97\u6b64\u540d\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b97\u6cd5\u6b65\u9aa4<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6bd4\u8f83\u76f8\u90bb\u7684\u5143\u7d20\u3002\u5982\u679c\u7b2c\u4e00\u4e2a\u6bd4\u7b2c\u4e8c\u4e2a\u5927\uff0c\u5c31\u4ea4\u6362\u5b83\u4eec<\/li>\n\n\n\n<li>\u5bf9\u6bcf\u4e00\u5bf9\u76f8\u90bb\u5143\u7d20\u505a\u540c\u6837\u7684\u5de5\u4f5c\uff0c\u4ece\u5f00\u59cb\u7b2c\u4e00\u5bf9\u5230\u7ed3\u5c3e\u7684\u6700\u540e\u4e00\u5bf9<\/li>\n\n\n\n<li>\u8fd9\u6b65\u505a\u5b8c\u540e\uff0c\u6700\u540e\u7684\u5143\u7d20\u4f1a\u662f\u6700\u5927\u7684\u6570<\/li>\n\n\n\n<li>\u9488\u5bf9\u6240\u6709\u7684\u5143\u7d20\u91cd\u590d\u4ee5\u4e0a\u7684\u6b65\u9aa4\uff0c\u9664\u4e86\u5df2\u7ecf\u662f\u6700\u5927\u6570\u7684\u6700\u540e\u4e00\u4e2a<\/li>\n\n\n\n<li>\u6301\u7eed\u6bcf\u6b21\u5bf9\u8d8a\u6765\u8d8a\u5c11(\u6bcf\u6b21\u91cd\u590d\u90fd\u4f1a\u5c11\u4e00\u4e2a\u6700\u5927\u6570)\u7684\u5143\u7d20\u91cd\u590d\u4e0a\u9762\u7684\u6b65\u9aa4\uff0c\u76f4\u5230\u6ca1\u6709\u4efb\u4f55\u4e00\u5bf9\u6570\u5b57\u9700\u8981\u6bd4\u8f83<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u57fa\u7840\u5b9e\u73b0<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">#include &lt;iostream&gt;<br>#include &lt;vector&gt;<br>\u200b<br>void bubbleSort(std::vector&lt;int&gt;&amp; arr) {<br> &nbsp; &nbsp;int n = arr.size();<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;for (int i = 0; i &lt; n - 1; i++) {<br> &nbsp; &nbsp; &nbsp; &nbsp;for (int j = 0; j &lt; n - i - 1; j++) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u5982\u679c\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u4e0b\u4e00\u4e2a\u5143\u7d20\uff0c\u5219\u4ea4\u6362<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (arr[j] &gt; arr[j + 1]) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int temp = arr[j];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr[j] = arr[j + 1];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr[j + 1] = temp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>}<br>\u200b<br>\/\/ \u6253\u5370\u6570\u7ec4<br>void printArray(const std::vector&lt;int&gt;&amp; arr) {<br> &nbsp; &nbsp;for (int num : arr) {<br> &nbsp; &nbsp; &nbsp; &nbsp;std::cout &lt;&lt; num &lt;&lt; \" \";<br> &nbsp;  }<br> &nbsp; &nbsp;std::cout &lt;&lt; std::endl;<br>}<br>\u200b<br>int main() {<br> &nbsp; &nbsp;std::vector&lt;int&gt; arr = {64, 34, 25, 12, 22, 11, 90};<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;std::cout &lt;&lt; \"\u6392\u5e8f\u524d\uff1a\";<br> &nbsp; &nbsp;printArray(arr);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;bubbleSort(arr);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;std::cout &lt;&lt; \"\u6392\u5e8f\u540e\uff1a\";<br> &nbsp; &nbsp;printArray(arr);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;return 0;<br>}<br>\u200b<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5feb\u901f\u6392\u5e8f<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u4ecb\u7ecd<\/h4>\n\n\n\n<p>\u5feb\u901f\u6392\u5e8f\uff08Quick Sort\uff09\u662f\u4e00\u79cd\u6781\u5176\u9ad8\u6548\u7684\u5206\u6cbb\u6392\u5e8f\u7b97\u6cd5\uff0c\u4e5f\u662f<strong>\u5b9e\u9645\u5e94\u7528\u4e2d\u6700\u5e38\u7528\u7684\u6392\u5e8f\u7b97\u6cd5\u4e4b\u4e00<\/strong>\u3002<\/p>\n\n\n\n<p>\u5b83\u7684\u6838\u5fc3\u601d\u60f3\u53ef\u4ee5\u6982\u62ec\u4e3a\u201c\u9009\u4e2a\u57fa\u51c6\uff0c\u7136\u540e\u5de6\u53f3\u7ad9\u961f\u201d\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u9009\u57fa\u51c6(pivot)\uff1a\u9996\u5148\uff0c\u4ece\u6570\u7ec4\u4e2d\u4efb\u610f\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u201c\u57fa\u51c6\u201d\u3002<\/li>\n\n\n\n<li>\u7ad9\u961f\uff1a\u63a5\u7740\uff0c\u91cd\u65b0\u6392\u5217\u6570\u7ec4\uff0c\u5c06\u6240\u6709\u5c0f\u4e8e\u57fa\u51c6\u7684\u5143\u7d20\u79fb\u52a8\u5230\u57fa\u51c6\u7684\u5de6\u8fb9\uff0c\u6240\u6709\u5927\u4e8e\u7b49\u4e8e\u57fa\u51c6\u7684\u5143\u7d20\u79fb\u52a8\u5230\u53f3\u8fb9\u3002\u8fd9\u4e00\u6b65\u5b8c\u6210\u540e\uff0c\u8be5\u57fa\u51c6\u5143\u7d20\u5c31\u627e\u5230\u4e86\u5b83\u5728\u6700\u7ec8\u6709\u5e8f\u5e8f\u5217\u4e2d\u7684\u201c\u6700\u7ec8\u4f4d\u7f6e\u201d\u3002<\/li>\n\n\n\n<li>\u5206\u800c\u6cbb\u4e4b\uff1a\u6700\u540e\uff0c\u5bf9\u57fa\u51c6\u5de6\u53f3\u4e24\u8fb9\u7684\u5b50\u6570\u7ec4\uff08\u73b0\u5728\u5b83\u4eec\u662f\u4e24\u4e2a\u72ec\u7acb\u7684\u3001\u66f4\u5c0f\u7684\u95ee\u9898\uff09\uff0c\u9012\u5f52\u5730\u91cd\u590d\u4e0a\u8ff0\u8fc7\u7a0b\uff0c\u76f4\u5230\u6bcf\u4e2a\u5b50\u6570\u7ec4\u90fd\u6392\u5e8f\u5b8c\u6bd5\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u79cd\u5de7\u5999\u7684\u201c\u5206\u800c\u6cbb\u4e4b\u201d\u7b56\u7565\uff0c\u5feb\u901f\u6392\u5e8f\u80fd\u5c06\u4e00\u4e2a\u5927\u95ee\u9898\u4e0d\u65ad\u5206\u89e3\u6210\u5c0f\u95ee\u9898\u6765\u89e3\u51b3\uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u80fd\u8fbe\u5230\u5353\u8d8a\u7684 O(nlogn)\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b97\u6cd5\u6b65\u9aa4<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4ece\u6570\u5217\u4e2d\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a&#8221;\u57fa\u51c6&#8221;\uff0c\u672c\u6587\u91c7\u7528\u6700\u5de6\u4fa7\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6<\/li>\n\n\n\n<li>\u5c06\u6240\u6709\u6bd4\u57fa\u51c6\u503c\u5c0f\u7684\u5143\u7d20\u653e\u5230\u57fa\u51c6\u524d\u9762\uff0c\u6240\u6709\u6bd4\u57fa\u51c6\u503c\u5927\u7684\u5143\u7d20\u653e\u5230\u57fa\u51c6\u540e\u9762\uff08\u5206\u533a\u64cd\u4f5c\uff09<\/li>\n\n\n\n<li>\u5bf9\u57fa\u51c6\u5de6\u53f3\u4e24\u4e2a\u5b50\u5e8f\u5217\u5206\u522b\u91cd\u590d\u6b65\u9aa41\u548c2\uff0c\u76f4\u5230\u5b50\u5e8f\u5217\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\u6216\u4e3a\u7a7a<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u4ee3\u7801\u5b9e\u73b0<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">#include &lt;iostream&gt;<br>#include &lt;vector&gt;<br>\u200b<br>int partition(std::vector&lt;int&gt;&amp; arr, int low, int high) {<br> &nbsp; &nbsp;\/\/ \u9009\u62e9\u6700\u5de6\u4fa7\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6<br> &nbsp; &nbsp;int pivot = arr[low];<br> &nbsp; &nbsp;int i = low + 1;<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;for (int j = low + 1; j &lt;= high; j++) {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u5c06\u5c0f\u4e8e\u57fa\u51c6\u7684\u5143\u7d20\u79fb\u5230\u5de6\u4fa7<br> &nbsp; &nbsp; &nbsp; &nbsp;if (arr[j] &lt; pivot) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u4ea4\u6362\u5143\u7d20<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;std::swap(arr[i], arr[j]);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i++;<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;\/\/ \u5c06\u57fa\u51c6\u5143\u7d20\u653e\u5230\u6b63\u786e\u4f4d\u7f6e<br> &nbsp; &nbsp;std::swap(arr[low], arr[i - 1]);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;return i - 1;<br>}<br>\u200b<br>void quickSort(std::vector&lt;int&gt;&amp; arr, int low, int high) {<br> &nbsp; &nbsp;if (low &lt; high) {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u83b7\u53d6\u5206\u533a\u70b9\u4f4d\u7f6e<br> &nbsp; &nbsp; &nbsp; &nbsp;int pivotIndex = partition(arr, low, high);<br> &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u9012\u5f52\u6392\u5e8f\u5de6\u53f3\u5b50\u6570\u7ec4<br> &nbsp; &nbsp; &nbsp; &nbsp;quickSort(arr, low, pivotIndex - 1);<br> &nbsp; &nbsp; &nbsp; &nbsp;quickSort(arr, pivotIndex + 1, high);<br> &nbsp;  }<br>}<br>\u200b<br>\/\/ \u6253\u5370\u6570\u7ec4<br>void printArray(const std::vector&lt;int&gt;&amp; arr) {<br> &nbsp; &nbsp;for (int num : arr) {<br> &nbsp; &nbsp; &nbsp; &nbsp;std::cout &lt;&lt; num &lt;&lt; \" \";<br> &nbsp;  }<br> &nbsp; &nbsp;std::cout &lt;&lt; std::endl;<br>}<br>\u200b<br>int main() {<br> &nbsp; &nbsp;std::vector&lt;int&gt; arr = {10, 7, 8, 9, 1, 5};<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;std::cout &lt;&lt; \"\u6392\u5e8f\u524d\uff1a\";<br> &nbsp; &nbsp;printArray(arr);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;quickSort(arr, 0, arr.size() - 1);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;std::cout &lt;&lt; \"\u6392\u5e8f\u540e\uff1a\";<br> &nbsp; &nbsp;printArray(arr);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;return 0;<br>}<br>\u200b<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5f52\u5e76\u6392\u5e8f<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u4ecb\u7ecd<\/h4>\n\n\n\n<p>\u5f52\u5e76\u6392\u5e8f\uff08Merge Sort\uff09\u662f\u4e00\u79cd\u9ad8\u6548\u4e14\u7a33\u5b9a\u7684\u201c\u5206\u6cbb\u201d\u6392\u5e8f\u7b97\u6cd5\u3002\u5176\u6838\u5fc3\u7b56\u7565\u53ef\u4ee5\u6982\u62ec\u4e3a\u201c<strong>\u5148\u9012\u5f52\u62c6\u5206\uff0c\u518d\u6709\u5e8f\u5408\u5e76<\/strong>\u201d\u3002\u5b83\u4f1a\u6301\u7eed\u5730\u5c06\u4e00\u4e2a\u5927\u6570\u7ec4\u5bf9\u534a\u5207\u5206\uff0c\u76f4\u5230\u6bcf\u4e2a\u90e8\u5206\u90fd\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\uff08\u6b64\u65f6\u5929\u7136\u6709\u5e8f\uff09\u3002\u63a5\u7740\uff0c\u518d\u53cd\u5411\u5730\u5c06\u8fd9\u4e9b\u76f8\u90bb\u7684\u6709\u5e8f\u90e8\u5206\u4e24\u4e24\u914d\u5bf9\uff0c\u6309\u5927\u5c0f\u987a\u5e8f\u5408\u5e76\u6210\u4e00\u4e2a\u66f4\u957f\u7684\u6709\u5e8f\u6570\u7ec4\uff0c\u4e0d\u65ad\u91cd\u590d\u6b64\u8fc7\u7a0b\uff0c\u76f4\u5230\u6700\u7ec8\u8fd8\u539f\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u6709\u5e8f\u5e8f\u5217\u3002<\/p>\n\n\n\n<p>\u5f52\u5e76\u6392\u5e8f\u7684\u6027\u80fd\u6781\u5176\u7a33\u5b9a\uff0c\u65e0\u8bba\u539f\u59cb\u5e8f\u5217\u662f\u597d\u662f\u574f\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4fdd\u6301\u5728\u5353\u8d8a\u7684 O(nlogn)\u3002\u4e0e\u5feb\u901f\u6392\u5e8f\u7684\u201c\u5c31\u5730\u4ea4\u6362\u201d\u4e0d\u540c\uff0c\u5b83\u901a\u8fc7\u201c\u6709\u5e8f\u5408\u5e76\u201d\u5b9e\u73b0\u6392\u5e8f\uff0c\u8fd9\u4e00\u7279\u6027\u4e5f\u4fdd\u8bc1\u4e86\u5176\u6392\u5e8f\u7684\u7a33\u5b9a\u6027\uff08\u76f8\u540c\u5143\u7d20\u7684\u539f\u59cb\u76f8\u5bf9\u987a\u5e8f\u5728\u6392\u5e8f\u540e\u4e0d\u4f1a\u6539\u53d8\uff09\uff0c\u4f46\u901a\u5e38\u9700\u8981\u989d\u5916\u7684\u5b58\u50a8\u7a7a\u95f4\u6765\u8f85\u52a9\u5408\u5e76\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b97\u6cd5\u6b65\u9aa4<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5c06\u5f85\u6392\u5e8f\u6570\u7ec4\u9012\u5f52\u5730\u5206\u5272\u6210\u4e24\u534a\uff0c\u76f4\u5230\u6bcf\u4e2a\u5b50\u6570\u7ec4\u53ea\u5305\u542b\u4e00\u4e2a\u5143\u7d20\uff08\u6b64\u65f6\u8ba4\u4e3a\u5b50\u6570\u7ec4\u5df2\u6392\u5e8f\uff09<\/li>\n\n\n\n<li>\u9012\u5f52\u5730\u5408\u5e76\u76f8\u90bb\u7684\u5b50\u6570\u7ec4\uff0c<strong>\u5408\u5e76\u65f6\u6bd4\u8f83\u4e24\u4e2a\u5b50\u6570\u7ec4\u7684\u5143\u7d20\uff0c\u6309\u987a\u5e8f\u653e\u5165\u4e34\u65f6\u6570\u7ec4<\/strong>\uff08\u6838\u5fc3\uff09<\/li>\n\n\n\n<li>\u5c06\u4e34\u65f6\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u590d\u5236\u56de\u539f\u6570\u7ec4\u5bf9\u5e94\u7684\u4f4d\u7f6e<\/li>\n\n\n\n<li>\u91cd\u590d\u6b65\u9aa42\u548c3\uff0c\u76f4\u5230\u6240\u6709\u5b50\u6570\u7ec4\u5408\u5e76\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u6709\u5e8f\u6570\u7ec4<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b97\u6cd5\u5b9e\u73b0<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">#include &lt;iostream&gt;<br>#include &lt;vector&gt;<br>\u200b<br>void merge(std::vector&lt;int&gt;&amp; arr, int left, int mid, int right) {<br> &nbsp; &nbsp;\/\/ \u8ba1\u7b97\u4e24\u4e2a\u5b50\u6570\u7ec4\u7684\u5927\u5c0f<br> &nbsp; &nbsp;int n1 = mid - left + 1;<br> &nbsp; &nbsp;int n2 = right - mid;<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;\/\/ \u521b\u5efa\u4e34\u65f6\u6570\u7ec4<br> &nbsp; &nbsp;std::vector&lt;int&gt; L(n1);<br> &nbsp; &nbsp;std::vector&lt;int&gt; R(n2);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;\/\/ \u590d\u5236\u6570\u636e\u5230\u4e34\u65f6\u6570\u7ec4<br> &nbsp; &nbsp;for (int i = 0; i &lt; n1; i++)<br> &nbsp; &nbsp; &nbsp; &nbsp;L[i] = arr[left + i];<br> &nbsp; &nbsp;for (int j = 0; j &lt; n2; j++)<br> &nbsp; &nbsp; &nbsp; &nbsp;R[j] = arr[mid + 1 + j];<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;\/\/ \u5408\u5e76\u4e34\u65f6\u6570\u7ec4<br> &nbsp; &nbsp;int i = 0, j = 0;<br> &nbsp; &nbsp;int k = left;<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;while (i &lt; n1 &amp;&amp; j &lt; n2) {<br> &nbsp; &nbsp; &nbsp; &nbsp;if (L[i] &lt;= R[j]) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr[k] = L[i];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i++;<br> &nbsp; &nbsp; &nbsp;  } else {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr[k] = R[j];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j++;<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;k++;<br> &nbsp;  }<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;\/\/ \u590d\u5236L\u7684\u5269\u4f59\u5143\u7d20<br> &nbsp; &nbsp;while (i &lt; n1) {<br> &nbsp; &nbsp; &nbsp; &nbsp;arr[k] = L[i];<br> &nbsp; &nbsp; &nbsp; &nbsp;i++;<br> &nbsp; &nbsp; &nbsp; &nbsp;k++;<br> &nbsp;  }<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;\/\/ \u590d\u5236R\u7684\u5269\u4f59\u5143\u7d20<br> &nbsp; &nbsp;while (j &lt; n2) {<br> &nbsp; &nbsp; &nbsp; &nbsp;arr[k] = R[j];<br> &nbsp; &nbsp; &nbsp; &nbsp;j++;<br> &nbsp; &nbsp; &nbsp; &nbsp;k++;<br> &nbsp;  }<br>}<br>\u200b<br>void mergeSort(std::vector&lt;int&gt;&amp; arr, int left, int right) {<br> &nbsp; &nbsp;if (left &lt; right) {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u627e\u51fa\u4e2d\u95f4\u70b9<br> &nbsp; &nbsp; &nbsp; &nbsp;int mid = left + (right - left) \/ 2;<br> &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u9012\u5f52\u6392\u5e8f\u5de6\u53f3\u4e24\u534a<br> &nbsp; &nbsp; &nbsp; &nbsp;mergeSort(arr, left, mid);<br> &nbsp; &nbsp; &nbsp; &nbsp;mergeSort(arr, mid + 1, right);<br> &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u5408\u5e76\u5df2\u6392\u5e8f\u7684\u4e24\u534a<br> &nbsp; &nbsp; &nbsp; &nbsp;merge(arr, left, mid, right);<br> &nbsp;  }<br>}<br>\u200b<br>\/\/ \u6253\u5370\u6570\u7ec4<br>void printArray(const std::vector&lt;int&gt;&amp; arr) {<br> &nbsp; &nbsp;for (int num : arr) {<br> &nbsp; &nbsp; &nbsp; &nbsp;std::cout &lt;&lt; num &lt;&lt; \" \";<br> &nbsp;  }<br> &nbsp; &nbsp;std::cout &lt;&lt; std::endl;<br>}<br>\u200b<br>int main() {<br> &nbsp; &nbsp;std::vector&lt;int&gt; arr = {12, 11, 13, 5, 6, 7};<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;std::cout &lt;&lt; \"\u6392\u5e8f\u524d\uff1a\";<br> &nbsp; &nbsp;printArray(arr);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;mergeSort(arr, 0, arr.size() - 1);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;std::cout &lt;&lt; \"\u6392\u5e8f\u540e\uff1a\";<br> &nbsp; &nbsp;printArray(arr);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;return 0;<br>}<br>\u200b<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u63d2\u5165\u6392\u5e8f<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u4ecb\u7ecd<\/h4>\n\n\n\n<p>\u63d2\u5165\u6392\u5e8f\uff08Insertion Sort\uff09\u662f\u4e00\u79cd\u7b80\u5355\u76f4\u89c2\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u5b83\u7684\u5de5\u4f5c\u65b9\u5f0f\u7c7b\u4f3c\u4e8e\u6211\u4eec\u6253\u724c\u65f6\u7684\u6574\u7406\u724c\u5e8f\uff0c\u5b83\u5c06\u5f85\u6392\u5e8f\u5e8f\u5217\u5206\u4e3a\u4e24\u90e8\u5206\uff1a\u5df2\u6392\u5e8f\u90e8\u5206\u548c\u672a\u6392\u5e8f\u90e8\u5206\u3002\u7b97\u6cd5\u4e0d\u65ad\u5730\u4ece\u672a\u6392\u5e8f\u90e8\u5206\u53d6\u51fa\u5143\u7d20\uff0c\u7136\u540e\u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\uff0c\u76f4\u5230\u6240\u6709\u5143\u7d20\u90fd\u6392\u5e8f\u5b8c\u6bd5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b97\u6cd5\u6b65\u9aa4<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5c06\u7b2c\u4e00\u4e2a\u5143\u7d20\u89c6\u4e3a\u5df2\u6392\u5e8f\u5e8f\u5217\uff0c\u5176\u4f59\u5143\u7d20\u89c6\u4e3a\u672a\u6392\u5e8f\u5e8f\u5217<\/li>\n\n\n\n<li>\u4ece\u672a\u6392\u5e8f\u5e8f\u5217\u4e2d\u53d6\u51fa\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u79f0\u4e3a&#8221;\u5f85\u63d2\u5165\u5143\u7d20&#8221;<\/li>\n\n\n\n<li>\u4ece\u5df2\u6392\u5e8f\u5e8f\u5217\u7684\u672b\u5c3e\u5f00\u59cb\uff0c\u4f9d\u6b21\u4e0e\u5f85\u63d2\u5165\u5143\u7d20\u6bd4\u8f83<\/li>\n\n\n\n<li>\u5982\u679c\u5df2\u6392\u5e8f\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u5927\u4e8e\u5f85\u63d2\u5165\u5143\u7d20\uff0c\u5219\u5c06\u8be5\u5143\u7d20\u540e\u79fb\u4e00\u4f4d<\/li>\n\n\n\n<li>\u91cd\u590d\u6b65\u9aa43\u548c4\uff0c\u76f4\u5230\u627e\u5230\u5c0f\u4e8e\u6216\u7b49\u4e8e\u5f85\u63d2\u5165\u5143\u7d20\u7684\u4f4d\u7f6e<\/li>\n\n\n\n<li>\u5c06\u5f85\u63d2\u5165\u5143\u7d20\u63d2\u5165\u5230\u8be5\u4f4d\u7f6e<\/li>\n\n\n\n<li>\u91cd\u590d\u6b65\u9aa42\u81f36\uff0c\u76f4\u5230\u672a\u6392\u5e8f\u5e8f\u5217\u4e3a\u7a7a<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b97\u6cd5\u5b9e\u73b0<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">#include &lt;iostream&gt;<br>#include &lt;vector&gt;<br>\u200b<br>void insertionSort(std::vector&lt;int&gt;&amp; arr) {<br> &nbsp; &nbsp;int n = arr.size();<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;for (int i = 1; i &lt; n; i++) {<br> &nbsp; &nbsp; &nbsp; &nbsp;int key = arr[i]; &nbsp;\/\/ \u5f53\u524d\u5f85\u63d2\u5165\u5143\u7d20<br> &nbsp; &nbsp; &nbsp; &nbsp;int j = i - 1;<br> &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u5c06\u5927\u4e8ekey\u7684\u5143\u7d20\u5411\u540e\u79fb\u52a8<br> &nbsp; &nbsp; &nbsp; &nbsp;while (j &gt;= 0 &amp;&amp; arr[j] &gt; key) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr[j + 1] = arr[j];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j--;<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u63d2\u5165\u5143\u7d20<br> &nbsp; &nbsp; &nbsp; &nbsp;arr[j + 1] = key;<br> &nbsp;  }<br>}<br>\u200b<br>\/\/ \u6253\u5370\u6570\u7ec4<br>void printArray(const std::vector&lt;int&gt;&amp; arr) {<br> &nbsp; &nbsp;for (int num : arr) {<br> &nbsp; &nbsp; &nbsp; &nbsp;std::cout &lt;&lt; num &lt;&lt; \" \";<br> &nbsp;  }<br> &nbsp; &nbsp;std::cout &lt;&lt; std::endl;<br>}<br>\u200b<br>int main() {<br> &nbsp; &nbsp;std::vector&lt;int&gt; arr = {12, 11, 13, 5, 6};<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;std::cout &lt;&lt; \"\u6392\u5e8f\u524d\uff1a\";<br> &nbsp; &nbsp;printArray(arr);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;insertionSort(arr);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;std::cout &lt;&lt; \"\u6392\u5e8f\u540e\uff1a\";<br> &nbsp; &nbsp;printArray(arr);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;return 0;<br>}<br>\u200b<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5806\u6392\u5e8f<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u4ecb\u7ecd<\/h4>\n\n\n\n<p>\u5806\u6392\u5e8f\uff08Heap Sort\uff09\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u539f\u5730\u6392\u5e8f\u7b97\u6cd5\uff0c\u5b83\u5de7\u5999\u5730\u5229\u7528\u4e86\u201c\u5806\u201d\u6570\u636e\u7ed3\u6784\u3002\u5176\u6838\u5fc3\u601d\u60f3\u662f\u9996\u5148\u5c06\u5f85\u6392\u5e8f\u7684\u6570\u7ec4\u91cd\u6784\u6210\u4e00\u4e2a\u6700\u5927\u5806\uff08Max Heap\uff09\u3002\u5728\u6700\u5927\u5806\u4e2d\uff0c\u6839\u8282\u70b9\uff08\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff09\u59cb\u7ec8\u662f\u6240\u6709\u5143\u7d20\u4e2d\u7684\u6700\u5927\u503c\u3002<\/p>\n\n\n\n<p>\u6784\u5efa\u597d\u6700\u5927\u5806\u540e\uff0c\u7b97\u6cd5\u5c06\u5806\u9876\u7684\u6700\u5927\u5143\u7d20\u4e0e\u5806\u672b\u5c3e\u7684\u5143\u7d20\u4ea4\u6362\uff0c\u4ece\u800c\u5c06\u5f53\u524d\u7684\u6700\u5927\u503c\u653e\u7f6e\u5230\u6570\u7ec4\u7684\u6b63\u786e\u6700\u7ec8\u4f4d\u7f6e\u3002\u63a5\u7740\uff0c\u5c06\u5806\u7684\u5927\u5c0f\u51cf\u4e00\uff0c\u5e76\u5bf9\u5269\u4f59\u7684\u5143\u7d20\u8fdb\u884c\u201c\u5806\u5316\u201d\u8c03\u6574\uff0c\u786e\u4fdd\u65b0\u7684\u6839\u8282\u70b9\u4ecd\u7136\u662f\u5269\u4f59\u5143\u7d20\u4e2d\u7684\u6700\u5927\u503c\u3002\u6b64\u8fc7\u7a0b\u4e0d\u65ad\u91cd\u590d\u2014\u2014\u4ea4\u6362\u3001\u7f29\u5c0f\u5806\u3001\u91cd\u65b0\u5806\u5316\u2014\u2014\u76f4\u5230\u6240\u6709\u5143\u7d20\u90fd\u88ab\u653e\u7f6e\u5230\u5176\u6700\u7ec8\u7684\u6709\u5e8f\u4f4d\u7f6e\uff0c\u5b8c\u6210\u6574\u4e2a\u6392\u5e8f\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5806\uff08Heap\uff09\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5b8c\u5168\u4e8c\u53c9\u6811\u7ed3\u6784\uff0c\u5b83\u7684\u4e24\u4e2a\u91cd\u8981\u7279\u6027\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5806\u662f\u4e00\u4e2a\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u9664\u4e86\u6700\u5e95\u5c42\u5916\uff0c\u5176\u4ed6\u5c42\u7684\u8282\u70b9\u90fd\u662f\u6ee1\u7684\uff0c\u6700\u5e95\u5c42\u7684\u8282\u70b9\u4ece\u5de6\u5230\u53f3\u586b\u5145\u3002<\/li>\n\n\n\n<li>\u5728\u6700\u5927\u5806\u4e2d\uff0c\u6bcf\u4e2a\u8282\u70b9\u7684\u503c\u90fd\u5927\u4e8e\u6216\u7b49\u4e8e\u5176\u5b50\u8282\u70b9\u7684\u503c\uff1b\u5728\u6700\u5c0f\u5806\u4e2d\uff0c\u6bcf\u4e2a\u8282\u70b9\u7684\u503c\u90fd\u5c0f\u4e8e\u6216\u7b49\u4e8e\u5176\u5b50\u8282\u70b9\u7684\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5806\u7684\u6570\u7ec4\u8868\u793a\uff1a<\/p>\n\n\n\n<p>\u867d\u7136\u5806\u662f\u4e00\u79cd\u6811\u7ed3\u6784\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u7528\u6570\u7ec4\u9ad8\u6548\u5730\u8868\u793a\uff0c\u8fd9\u662f\u5806\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u6027\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u6570\u7ec4\u4e2d\u7d22\u5f15\u4e3a i \u7684\u8282\u70b9\uff1a<\/p>\n\n\n\n<p>\u5176\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\u4e3a 2*i + 1<\/p>\n\n\n\n<p>\u5176\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\u4e3a 2*i + 2<\/p>\n\n\n\n<p>\u8fd9\u79cd\u8868\u793a\u65b9\u6cd5\u975e\u5e38\u7d27\u51d1\uff0c\u4e0d\u9700\u8981\u4f7f\u7528\u989d\u5916\u7684\u6307\u9488\uff0c\u5145\u5206\u5229\u7528\u4e86\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u6027\u8d28\u3002<\/p>\n<\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b97\u6cd5\u6b65\u9aa4<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5c06\u65e0\u5e8f\u5e8f\u5217\u6784\u5efa\u6210\u4e00\u4e2a\u6700\u5927\u5806<\/li>\n\n\n\n<li>\u5c06\u5806\u9876\u5143\u7d20\uff08\u6700\u5927\u503c\uff09\u4e0e\u5806\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u4ea4\u6362<\/li>\n\n\n\n<li>\u5254\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff08\u5df2\u6392\u5e8f\uff09\uff0c\u5c06\u5269\u4f59\u5143\u7d20\u91cd\u65b0\u6784\u5efa\u4e3a\u6700\u5927\u5806<\/li>\n\n\n\n<li>\u91cd\u590d\u6b65\u9aa42\u548c3\uff0c\u76f4\u5230\u5806\u4e2d\u53ea\u5269\u4e0b\u4e00\u4e2a\u5143\u7d20<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b97\u6cd5\u5b9e\u73b0<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">#include &lt;iostream&gt;<br>#include &lt;vector&gt;<br>\u200b<br>void heapify(std::vector&lt;int&gt;&amp; arr, int n, int root) {<br> &nbsp; &nbsp;int largest = root; &nbsp; &nbsp; &nbsp;\/\/ \u521d\u59cb\u5316\u6700\u5927\u503c\u4e3a\u6839\u8282\u70b9<br> &nbsp; &nbsp;int left = 2 * root + 1; \/\/ \u5de6\u5b50\u8282\u70b9<br> &nbsp; &nbsp;int right = 2 * root + 2; \/\/ \u53f3\u5b50\u8282\u70b9<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;\/\/ \u5982\u679c\u5de6\u5b50\u8282\u70b9\u5927\u4e8e\u6839\u8282\u70b9<br> &nbsp; &nbsp;if (left &lt; n &amp;&amp; arr[left] &gt; arr[largest])<br> &nbsp; &nbsp; &nbsp; &nbsp;largest = left;<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;\/\/ \u5982\u679c\u53f3\u5b50\u8282\u70b9\u5927\u4e8e\u5f53\u524d\u6700\u5927\u503c<br> &nbsp; &nbsp;if (right &lt; n &amp;&amp; arr[right] &gt; arr[largest])<br> &nbsp; &nbsp; &nbsp; &nbsp;largest = right;<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;\/\/ \u5982\u679c\u6700\u5927\u503c\u4e0d\u662f\u6839\u8282\u70b9<br> &nbsp; &nbsp;if (largest != root) {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u4ea4\u6362\u6839\u8282\u70b9\u548c\u6700\u5927\u503c<br> &nbsp; &nbsp; &nbsp; &nbsp;std::swap(arr[root], arr[largest]);<br> &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u9012\u5f52\u8c03\u6574\u88ab\u5f71\u54cd\u7684\u5b50\u6811<br> &nbsp; &nbsp; &nbsp; &nbsp;heapify(arr, n, largest);<br> &nbsp;  }<br>}<br>\u200b<br>void heapSort(std::vector&lt;int&gt;&amp; arr) {<br> &nbsp; &nbsp;int n = arr.size();<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;\/\/ \u6784\u5efa\u6700\u5927\u5806<br> &nbsp; &nbsp;for (int i = n \/ 2 - 1; i &gt;= 0; i--)<br> &nbsp; &nbsp; &nbsp; &nbsp;heapify(arr, n, i);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;\/\/ \u9010\u4e2a\u4ece\u5806\u9876\u53d6\u51fa\u5143\u7d20<br> &nbsp; &nbsp;for (int i = n - 1; i &gt; 0; i--) {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u5c06\u5f53\u524d\u5806\u9876\uff08\u6700\u5927\u503c\uff09\u79fb\u5230\u672b\u5c3e<br> &nbsp; &nbsp; &nbsp; &nbsp;std::swap(arr[0], arr[i]);<br> &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u5bf9\u5269\u4f59\u5143\u7d20\u91cd\u65b0\u6784\u5efa\u6700\u5927\u5806<br> &nbsp; &nbsp; &nbsp; &nbsp;heapify(arr, i, 0);<br> &nbsp;  }<br>}<br>\u200b<br>\/\/ \u6253\u5370\u6570\u7ec4<br>void printArray(const std::vector&lt;int&gt;&amp; arr) {<br> &nbsp; &nbsp;for (int num : arr) {<br> &nbsp; &nbsp; &nbsp; &nbsp;std::cout &lt;&lt; num &lt;&lt; \" \";<br> &nbsp;  }<br> &nbsp; &nbsp;std::cout &lt;&lt; std::endl;<br>}<br>\u200b<br>int main() {<br> &nbsp; &nbsp;std::vector&lt;int&gt; arr = {12, 11, 13, 5, 6, 7};<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;std::cout &lt;&lt; \"\u6392\u5e8f\u524d\uff1a\";<br> &nbsp; &nbsp;printArray(arr);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;heapSort(arr);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;std::cout &lt;&lt; \"\u6392\u5e8f\u540e\uff1a\";<br> &nbsp; &nbsp;printArray(arr);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;return 0;<br>}<br>\u200b<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u67e5\u627e<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u5206\u67e5\u627e<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u4ecb\u7ecd<\/h4>\n\n\n\n<p>\u4e8c\u5206\u67e5\u627e\uff08Binary Search\uff09\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u67e5\u627e\u7b97\u6cd5\uff0c\u4e5f\u53eb\u6298\u534a\u67e5\u627e\u3002\u6838\u5fc3\u601d\u60f3\uff1a\u5bf9\u4e8e\u4e00\u4e2a<strong>\u6709\u5e8f<\/strong>\u7684\u6570\u636e\u96c6\u5408\uff0c\u6bcf\u6b21\u67e5\u627e\u90fd\u5c06\u67e5\u627e\u8303\u56f4\u7f29\u5c0f\u4e3a\u539f\u6765\u7684\u4e00\u534a\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6807\u503c\u6216\u786e\u5b9a\u76ee\u6807\u503c\u4e0d\u5b58\u5728\u3002\u4e8c\u5206\u67e5\u627e\u8981\u6c42\u6570\u636e\u5fc5\u987b\u662f\u6709\u5e8f\u7684\uff0c\u7ecf\u5e38\u5e94\u7528\u4e8e\u6570\u7ec4\u7b49\u652f\u6301\u968f\u673a\u8bbf\u95ee\u7684\u6570\u636e\u7ed3\u6784\u91cc\u3002\u8ddf\u7ebf\u6027\u67e5\u627e\u76f8\u6bd4\uff0c\u4e8c\u5206\u67e5\u627e\u7684\u6548\u7387\u8981\u9ad8\u5f97\u591a\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u96c6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b97\u6cd5\u6b65\u9aa4<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u786e\u5b9a\u67e5\u627e\u8303\u56f4\u7684\u5de6\u8fb9\u754c left \u548c\u53f3\u8fb9\u754c right<\/li>\n\n\n\n<li>\u8ba1\u7b97\u4e2d\u95f4\u4f4d\u7f6e mid = (left + right) \/ 2\uff08\u6ce8\u610f\u6574\u6570\u6ea2\u51fa\u95ee\u9898\uff0c\u66f4\u5b89\u5168\u7684\u505a\u6cd5\u662f mid = left + (right &#8211; left) \/ 2\uff09<\/li>\n\n\n\n<li>\u5c06\u4e2d\u95f4\u4f4d\u7f6e\u7684\u5143\u7d20\u4e0e\u76ee\u6807\u503c\u6bd4\u8f83\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u4e2d\u95f4\u5143\u7d20\u7b49\u4e8e\u76ee\u6807\u503c\uff0c\u67e5\u627e\u6210\u529f\uff0c\u8fd4\u56de\u4e2d\u95f4\u5143\u7d20\u7684\u4f4d\u7f6e<\/li>\n\n\n\n<li>\u5982\u679c\u4e2d\u95f4\u5143\u7d20\u5927\u4e8e\u76ee\u6807\u503c\uff0c\u76ee\u6807\u503c\u53ef\u80fd\u5728\u5de6\u534a\u90e8\u5206\uff0c\u5c06\u53f3\u8fb9\u754c\u8c03\u6574\u4e3a mid &#8211; 1<\/li>\n\n\n\n<li>\u5982\u679c\u4e2d\u95f4\u5143\u7d20\u5c0f\u4e8e\u76ee\u6807\u503c\uff0c\u76ee\u6807\u503c\u53ef\u80fd\u5728\u53f3\u534a\u90e8\u5206\uff0c\u5c06\u5de6\u8fb9\u754c\u8c03\u6574\u4e3a mid + 1<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u91cd\u590d\u6b65\u9aa42-3\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6807\u503c\u6216\u8005\u5de6\u8fb9\u754c\u5927\u4e8e\u53f3\u8fb9\u754c\uff08\u6b64\u65f6\u8868\u793a\u76ee\u6807\u503c\u4e0d\u5b58\u5728\uff09<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b97\u6cd5\u5b9e\u73b0<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">#include &lt;iostream&gt;<br>#include &lt;vector&gt;<br>\u200b<br>\/\/ \u8fed\u4ee3\u5b9e\u73b0<br>int binarySearch(const std::vector&lt;int&gt;&amp; arr, int target) {<br> &nbsp; &nbsp;int left = 0;<br> &nbsp; &nbsp;int right = arr.size() - 1;<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;while (left &lt;= right) {<br> &nbsp; &nbsp; &nbsp; &nbsp;int mid = left + (right - left) \/ 2;<br> &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;if (arr[mid] == target) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return mid;<br> &nbsp; &nbsp; &nbsp;  } else if (arr[mid] &gt; target) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;right = mid - 1;<br> &nbsp; &nbsp; &nbsp;  } else {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;left = mid + 1;<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;return -1;<br>}<br>\u200b<br>\/\/ \u9012\u5f52\u5b9e\u73b0<br>int binarySearchRecursive(const std::vector&lt;int&gt;&amp; arr, int target, int left, int right) {<br> &nbsp; &nbsp;if (left &gt; right) {<br> &nbsp; &nbsp; &nbsp; &nbsp;return -1;<br> &nbsp;  }<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;int mid = left + (right - left) \/ 2;<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;if (arr[mid] == target) {<br> &nbsp; &nbsp; &nbsp; &nbsp;return mid;<br> &nbsp;  } else if (arr[mid] &gt; target) {<br> &nbsp; &nbsp; &nbsp; &nbsp;return binarySearchRecursive(arr, target, left, mid - 1);<br> &nbsp;  } else {<br> &nbsp; &nbsp; &nbsp; &nbsp;return binarySearchRecursive(arr, target, mid + 1, right);<br> &nbsp;  }<br>}<br>\u200b<br>int main() {<br> &nbsp; &nbsp;std::vector&lt;int&gt; arr = {2, 3, 4, 10, 40, 50, 70, 80};<br> &nbsp; &nbsp;int target = 10;<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;\/\/ \u8fed\u4ee3\u65b9\u6cd5<br> &nbsp; &nbsp;int result = binarySearch(arr, target);<br> &nbsp; &nbsp;if (result == -1) {<br> &nbsp; &nbsp; &nbsp; &nbsp;std::cout &lt;&lt; \"\u5143\u7d20 \" &lt;&lt; target &lt;&lt; \" \u4e0d\u5b58\u5728\u4e8e\u6570\u7ec4\u4e2d\" &lt;&lt; std::endl;<br> &nbsp;  } else {<br> &nbsp; &nbsp; &nbsp; &nbsp;std::cout &lt;&lt; \"\u5143\u7d20 \" &lt;&lt; target &lt;&lt; \" \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u4e3a \" &lt;&lt; result &lt;&lt; std::endl;<br> &nbsp;  }<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;\/\/ \u9012\u5f52\u65b9\u6cd5<br> &nbsp; &nbsp;result = binarySearchRecursive(arr, target, 0, arr.size() - 1);<br> &nbsp; &nbsp;if (result == -1) {<br> &nbsp; &nbsp; &nbsp; &nbsp;std::cout &lt;&lt; \"\u5143\u7d20 \" &lt;&lt; target &lt;&lt; \" \u4e0d\u5b58\u5728\u4e8e\u6570\u7ec4\u4e2d\" &lt;&lt; std::endl;<br> &nbsp;  } else {<br> &nbsp; &nbsp; &nbsp; &nbsp;std::cout &lt;&lt; \"\u5143\u7d20 \" &lt;&lt; target &lt;&lt; \" \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u4e3a \" &lt;&lt; result &lt;&lt; std::endl;<br> &nbsp;  }<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;return 0;<br>}<br>\u200b<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b57\u7b26\u4e32\u5339\u914d<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Rabin-Karp\u7b97\u6cd5<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u4ecb\u7ecd<\/h4>\n\n\n\n<p>Rabin-Karp\u7b97\u6cd5\u662f\u4e00\u79cd<strong>\u57fa\u4e8e\u54c8\u5e0c\u51fd\u6570\u7684\u5b57\u7b26\u4e32\u5339\u914d\u7b97\u6cd5<\/strong>\uff0c\u7531 Michael O. Rabin \u548c Richard M. Karp \u4e8e1987\u5e74\u63d0\u51fa\uff0c\u6838\u5fc3\u601d\u60f3\u662f\u7528<strong>\u54c8\u5e0c\u51fd\u6570<\/strong>\u5c06\u6a21\u5f0f\u4e32\u548c\u6587\u672c\u4e32\u4e2d\u7684\u5b50\u4e32\u8f6c\u6362\u4e3a\u6570\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u907f\u514d\u5927\u91cf\u4e0d\u5fc5\u8981\u7684\u5b57\u7b26\u6bd4\u8f83\u3002\u8fd9\u4e2a\u7b97\u6cd5\u7279\u522b\u9002\u5408<strong>\u591a\u6a21\u5f0f\u4e32\u5339\u914d\u573a\u666f<\/strong>\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5e73\u5747\u4e3aO(n+m)\uff0cn\u662f\u6587\u672c\u4e32\u957f\u5ea6\uff0cm\u662f\u6a21\u5f0f\u4e32\u957f\u5ea6\u3002<\/p>\n\n\n\n<p>Rabin-Karp\u7b97\u6cd5\u7684\u5173\u952e\u5728\u4e8e\u4f7f\u7528<strong>\u6eda\u52a8\u54c8\u5e0c\u51fd\u6570<\/strong>\uff08Rolling Hash\uff09\uff0c\u5b83\u53ef\u4ee5\u5728\u5e38\u6570\u65f6\u95f4\u5185\u8ba1\u7b97\u51fa\u6ed1\u52a8\u7a97\u53e3\u7684\u65b0\u54c8\u5e0c\u503c\uff0c\u4fdd\u8bc1\u7b97\u6cd5\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u7684\u9ad8\u6548\u6027\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b97\u6cd5\u6b65\u9aa4<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u8ba1\u7b97\u6a21\u5f0f\u4e32\u7684\u54c8\u5e0c\u503c<\/li>\n\n\n\n<li>\u8ba1\u7b97\u6587\u672c\u4e32\u4e2d\u957f\u5ea6\u4e3am\u7684\u7b2c\u4e00\u4e2a\u5b50\u4e32\u7684\u54c8\u5e0c\u503c\uff08m\u4e3a\u6a21\u5f0f\u4e32\u957f\u5ea6\uff09<\/li>\n\n\n\n<li>\u5728\u6587\u672c\u4e32\u4e0a\u6ed1\u52a8\u7a97\u53e3\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u4f4d\u7f6e\uff1a\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u6eda\u52a8\u54c8\u5e0c\u6280\u672f\u9ad8\u6548\u8ba1\u7b97\u5f53\u524d\u7a97\u53e3\u7684\u54c8\u5e0c\u503c<\/li>\n\n\n\n<li>\u5982\u679c\u54c8\u5e0c\u503c\u4e0e\u6a21\u5f0f\u4e32\u76f8\u7b49\uff0c\u5219\u8fdb\u884c\u5b57\u7b26\u9010\u4e00\u6bd4\u8f83\u4ee5\u907f\u514d\u54c8\u5e0c\u51b2\u7a81<\/li>\n\n\n\n<li>\u5982\u679c\u5b8c\u5168\u5339\u914d\uff0c\u5219\u627e\u5230\u4e00\u4e2a\u5339\u914d\u4f4d\u7f6e<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u91cd\u590d\u6b65\u9aa43\uff0c\u76f4\u5230\u5904\u7406\u5b8c\u6574\u4e2a\u6587\u672c\u4e32<\/li>\n<\/ol>\n\n\n\n<p>####<\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\u5929\u901f\u6210\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5-Day1 \u6392\u5e8f \u5192\u6ce1\u6392\u5e8f \u4ecb\u7ecd \u5192\u6ce1\u6392\u5e8f\uff08Bubble Sort\uff09\u7684\u6838\u5fc3\u601d\u60f3\u975e\u5e38\u7b80\u5355\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[14,15],"tags":[],"class_list":["post-266","post","type-post","status-publish","format-standard","hentry","category-14","category-15"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/edryan.top\/index.php\/wp-json\/wp\/v2\/posts\/266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/edryan.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/edryan.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/edryan.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/edryan.top\/index.php\/wp-json\/wp\/v2\/comments?post=266"}],"version-history":[{"count":3,"href":"https:\/\/edryan.top\/index.php\/wp-json\/wp\/v2\/posts\/266\/revisions"}],"predecessor-version":[{"id":269,"href":"https:\/\/edryan.top\/index.php\/wp-json\/wp\/v2\/posts\/266\/revisions\/269"}],"wp:attachment":[{"href":"https:\/\/edryan.top\/index.php\/wp-json\/wp\/v2\/media?parent=266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/edryan.top\/index.php\/wp-json\/wp\/v2\/categories?post=266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/edryan.top\/index.php\/wp-json\/wp\/v2\/tags?post=266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}