{"id":311,"date":"2021-08-23T15:24:07","date_gmt":"2021-08-23T07:24:07","guid":{"rendered":"http:\/\/blog.nonot.cn\/?p=311"},"modified":"2024-07-08T13:05:56","modified_gmt":"2024-07-08T05:05:56","slug":"%e6%8e%92%e5%ba%8f%e7%ae%97%e6%b3%95%e8%a7%a3%e6%9e%90_java","status":"publish","type":"post","link":"https:\/\/blog.nonot.cn\/index.php\/2021\/08\/23\/%e6%8e%92%e5%ba%8f%e7%ae%97%e6%b3%95%e8%a7%a3%e6%9e%90_java\/","title":{"rendered":"\u6392\u5e8f\u7b97\u6cd5\u89e3\u6790_JAVA"},"content":{"rendered":"<p>\u4ee5\u4e0b\u5982\u65e0\u7279\u6b8a\u8bf4\u660e\u90fd\u662f\u6309\u7167\u5347\u5e8f\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<h1 id=\"0.7565232011498904\">\u6bd4\u8f83\u7c7b\u6392\u5e8f<\/h1>\n<h2 id=\"0.48965747925912595\">\u4ea4\u6362\u6392\u5e8f<\/h2>\n<h3 id=\"0.5347028021126787\">\u5192\u6ce1\u6392\u5e8f<\/h3>\n<h4 id=\"0.35798128606377944\">\u5b9a\u4e49<\/h4>\n<p>\u662f\u4e00\u79cd\u7b80\u5355\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u5b83\u91cd\u590d\u5730\u8d70\u8bbf\u8fc7\u8981\u6392\u5e8f\u7684\u6570\u5217\uff0c\u4e00\u6b21\u6bd4\u8f83\u4e24\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u4ed6\u4eec\u7684\u987a\u5e8f\u9519\u8bef\u5c31\u628a\u4ed6\u4eec\u4ea4\u6362\u8fc7\u6765\u3002\u8d70\u8bbf\u6570\u5217\u7684\u5de5\u4f5c\u662f\u91cd\u590d\u5730\u8fdb\u884c\u76f4\u5230\u6ca1\u6709\u518d\u9700\u8981\u4ea4\u6362\uff0c\u4e5f\u5c31\u662f\u8bf4\u8be5\u6570\u5217\u5df2\u7ecf\u6392\u5e8f\u5b8c\u6210\u3002\u8fd9\u4e2a\u7b97\u6cd5\u7684\u540d\u5b57\u7531\u6765\u662f\u56e0\u4e3a\u8d8a\u5c0f\u7684\u5143\u7d20\u4f1a\u7ecf\u7531\u4ea4\u6362\u6162\u6162\u201c\u6d6e\u201d\u5230\u6570\u5217\u7684\u9876\u7aef&#8211;\u7ef4\u57fa\u767e\u79d1\u3002<\/p>\n<h4 id=\"0.25463015698546787\">\u601d\u60f3<\/h4>\n<p>\u5192\u6ce1\u6392\u5e8f\u5f88\u7b80\u5355\uff0c\u987e\u540d\u601d\u4e49\u6bcf\u8f6e\u5faa\u73af\u4e2d\u5c06\u4e00\u4e2a\u6700\u5927\/\u6700\u5c0f\u7684\u6570\u901a\u8fc7\u4ea4\u6362\u4e00\u8def<code>\u5192<\/code>\u5230\u6570\u7ec4\u9876\u90e8\u3002<\/p>\n<h4 id=\"0.6461355387636325\">\u4ee3\u7801<\/h4>\n<pre><code class=\"language-java hljs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BubbleSort<\/span> <\/span>{\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String[] args)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">int<\/span>[] arr = {<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">12<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">453<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">59<\/span>, <span class=\"hljs-number\">33<\/span>};\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> i = <span class=\"hljs-number\">0<\/span>, length = arr.length; i &lt; arr.length - <span class=\"hljs-number\">1<\/span>; i++) {\n            <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> j = <span class=\"hljs-number\">0<\/span>, tempLength = length - <span class=\"hljs-number\">1<\/span> - i; j &lt; tempLength; j++) {\n                <span class=\"hljs-comment\">\/\/\u5982\u679c\u5f53\u524d\u6570\u5927\u4e8e\u4e0b\u4e00\u4e2a\u6570\u90a3\u4e48\u548c\u4e0b\u4e00\u4e2a\u6570\u4ea4\u6362\u4f4d\u7f6e<\/span>\n                <span class=\"hljs-keyword\">if<\/span> (arr[j] &gt; arr[j + <span class=\"hljs-number\">1<\/span>]) {\n                    <span class=\"hljs-keyword\">int<\/span> temp = arr[j];\n                    arr[j] = arr[j + <span class=\"hljs-number\">1<\/span>];\n                    arr[j + <span class=\"hljs-number\">1<\/span>] = temp;\n                }\n            }\n            System.out.println(Arrays.toString(arr));\n        }\n    }\n}\n<\/code><\/pre>\n<h3 id=\"0.1696300279199916\">\u5feb\u901f\u6392\u5e8f<\/h3>\n<h4 id=\"0.4968616893600599\">\u5b9a\u4e49<\/h4>\n<p>\u5feb\u901f\u6392\u5e8f\uff08Quicksort\uff09\u662f\u5bf9\u5192\u6ce1\u6392\u5e8f\u7684\u4e00\u79cd\u6539\u8fdb\u3002\u7531 C. A. R. Hoare \u5728 1960 \u5e74\u63d0\u51fa\u3002\u5b83\u7684\u57fa\u672c\u601d\u60f3\u662f\uff1a\u901a\u8fc7\u4e00\u8d9f\u6392\u5e8f\u5c06\u8981\u6392\u5e8f\u7684\u6570\u636e\u5206\u5272\u6210\u72ec\u7acb\u7684\u4e24\u90e8\u5206\uff0c\u5176\u4e2d\u4e00\u90e8\u5206\u7684\u6240\u6709\u6570\u636e\u90fd\u6bd4\u53e6\u5916\u4e00\u90e8\u5206\u7684\u6240\u6709\u6570\u636e\u90fd\u8981\u5c0f\uff0c\u7136\u540e\u518d\u6309\u6b64\u65b9\u6cd5\u5bf9\u8fd9\u4e24\u90e8\u5206\u6570\u636e\u5206\u522b\u8fdb\u884c\u5feb\u901f\u6392\u5e8f\uff0c\u6574\u4e2a\u6392\u5e8f\u8fc7\u7a0b\u53ef\u4ee5\u9012\u5f52\u8fdb\u884c\uff0c\u4ee5\u6b64\u8fbe\u5230\u6574\u4e2a\u6570\u636e\u53d8\u6210\u6709\u5e8f\u5e8f\u5217&#8211;\u767e\u5ea6\u767e\u79d1\u3002<\/p>\n<h4 id=\"0.23000197799375544\">\u601d\u60f3<\/h4>\n<ol>\n<li>\u4f7f\u7528\u4e86\u5206\u6cbb\u7684\u601d\u60f3\uff0c\u5148\u53d6\u4e00\u4e2a\u6570\u4f5c\u4e3a\u57fa\u6570\uff08\u4e00\u822c\u9009\u7b2c\u4e00\u4e2a\u6570\uff09\uff0c\u7136\u540e\u5c06\u8fd9\u4e2a\u6570\u79fb\u52a8\u5230\u4e00\u4e2a\u5408\u9002\u7684\u4f4d\u7f6e\u4f7f\u5de6\u8fb9\u7684\u90fd\u6bd4\u5b83\u5c0f\uff0c\u53f3\u8fb9\u7684\u90fd\u6bd4\u4ed6\u5927<\/li>\n<li>\u9012\u5f52\u5904\u7406\u8fd9\u4e2a\u6570\u5de6\u8fb9\u7684\u6570\u548c\u53f3\u8fb9\u7684\u6570\uff0c\u76f4\u5230\u6240\u6709\u7684\u6570\u90fd\u6709\u5e8f\u3002\u76f4\u5230\u6240\u6709\u7684\u6570\u90fd\u6709\u5e8f<\/li>\n<\/ol>\n<h4 id=\"0.4281065651657452\">\u4ee3\u7801<\/h4>\n<pre><code class=\"language-java hljs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">QuickSort<\/span> <\/span>{\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">deal<\/span><span class=\"hljs-params\">(Integer[] arr, <span class=\"hljs-keyword\">int<\/span> start, <span class=\"hljs-keyword\">int<\/span> end)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">if<\/span> (start &gt;= end) {\n            <span class=\"hljs-keyword\">return<\/span>;\n        }\n        <span class=\"hljs-keyword\">int<\/span> base = arr[start], i = start, j = end;\n        <span class=\"hljs-keyword\">while<\/span> (i &lt; j) {\n            <span class=\"hljs-comment\">\/\/\u5728\u53f3\u8fb9\u627e\u4e00\u4e2a\u6bd4\u57fa\u6570\u5c0f\u7684\u6570,\u76f4\u5230i,j\u76f8\u7b49<\/span>\n            <span class=\"hljs-keyword\">while<\/span> (arr[j] &gt;= base &amp;&amp; j &gt; i) {\n                j--;\n            }\n\n            <span class=\"hljs-comment\">\/\/\u5728\u5de6\u8fb9\u627e\u4e00\u4e2a\u6bd4\u57fa\u6570\u5927\u7684\u6570,\u76f4\u5230i,j\u76f8\u7b49<\/span>\n            <span class=\"hljs-keyword\">while<\/span> (arr[i] &lt;= base &amp;&amp; j &gt; i) {\n                i++;\n            }\n            <span class=\"hljs-comment\">\/\/\u5982\u679cij\u4e0d\u76f8\u7b49\uff0c\u4ea4\u6362\u5176\u503c<\/span>\n            <span class=\"hljs-keyword\">if<\/span> (i &lt; j) {\n                ArrayUtil.swap(arr, i++, j--);\n            }\n        }\n        <span class=\"hljs-comment\">\/\/\u6b64\u65f6i\u7b49\u4e8ej\uff0c\u4ea4\u6362\u57fa\u6570\u548ci\/j,\u4f7f\u5de6\u8fb9\u7684\u6570\u5c0f\u4e8e\u7b49\u4e8e\u57fa\u6570\uff0c\u53f3\u8fb9\u7684\u6570\u5927\u4e8e\u7b49\u4e8e\u57fa\u6570<\/span>\n        <span class=\"hljs-keyword\">if<\/span> (start != i) {\n            ArrayUtil.swap(arr, start, i);\n        }\n        deal(arr, start, i - <span class=\"hljs-number\">1<\/span>);\n        deal(arr, j + <span class=\"hljs-number\">1<\/span>, end);\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String[] args)<\/span> <\/span>{\n        Integer[] arr = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">43<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">555<\/span>, <span class=\"hljs-number\">200<\/span>, <span class=\"hljs-number\">21<\/span>};\n        deal(arr, <span class=\"hljs-number\">0<\/span>, arr.length - <span class=\"hljs-number\">1<\/span>);\n        System.out.println(<span class=\"hljs-string\">\"\u7ed3\u679c\"<\/span> + Arrays.toString(arr));\n    }\n}\n<\/code><\/pre>\n<h2 id=\"0.2825824167493143\">\u63d2\u5165\u6392\u5e8f<\/h2>\n<h3 id=\"0.42291453925102496\">\u7b80\u5355\u63d2\u5165\u6392\u5e8f<\/h3>\n<p>\u662f\u4e00\u79cd\u7b80\u5355\u76f4\u89c2\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u662f\u901a\u8fc7\u6784\u5efa\u6709\u5e8f\u5e8f\u5217\uff0c\u5bf9\u4e8e\u672a\u6392\u5e8f\u6570\u636e\uff0c\u5728\u5df2\u6392\u5e8f\u5e8f\u5217\u4e2d\u4ece\u540e\u5411\u524d\u626b\u63cf\uff0c\u627e\u5230\u76f8\u5e94\u4f4d\u7f6e\u5e76\u63d2\u5165\u3002\u63d2\u5165\u6392\u5e8f\u5728\u5b9e\u73b0\u4e0a\uff0c\u901a\u5e38\u91c7\u7528 in-place \u6392\u5e8f\uff0c\u56e0\u800c\u5728\u4ece\u540e\u5411\u524d\u626b\u63cf\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u53cd\u590d\u628a\u5df2\u6392\u5e8f\u5143\u7d20\u9010\u6b65\u5411\u540e\u632a\u4f4d\uff0c\u4e3a\u6700\u65b0\u5143\u7d20\u63d0\u4f9b\u63d2\u5165\u7a7a\u95f4&#8211;\u7ef4\u57fa\u767e\u79d1\u3002<\/p>\n<h4 id=\"0.006541865761128474\">\u601d\u60f3<\/h4>\n<p>\u63d2\u5165\u6392\u5e8f\u7684\u601d\u60f3\u5f88\u7b80\u5355\u76f4\u63a5\uff1a<\/p>\n<ol>\n<li>\u4ece\u7b2c\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u8be5\u5143\u7d20\u53ef\u4ee5\u8ba4\u4e3a\u5df2\u7ecf\u88ab\u6392\u5e8f<\/li>\n<li>\u53d6\u51fa\u4e0b\u4e00\u4e2a\u5143\u7d20\uff0c\u5728\u5df2\u7ecf\u6392\u5e8f\u7684\u5143\u7d20\u5e8f\u5217\u4e2d\u4ece\u540e\u5411\u524d\u626b\u63cf<\/li>\n<li>\u5982\u679c\u8be5\u5143\u7d20\uff08\u5df2\u6392\u5e8f\uff09\u5927\u4e8e\u65b0\u5143\u7d20\uff0c\u5c06\u8be5\u5143\u7d20\u79fb\u5230\u4e0b\u4e00\u4f4d\u7f6e<\/li>\n<li>\u91cd\u590d\u6b65\u9aa4 3\uff0c\u76f4\u5230\u627e\u5230\u5df2\u6392\u5e8f\u7684\u5143\u7d20\u5c0f\u4e8e\u6216\u8005\u7b49\u4e8e\u65b0\u5143\u7d20\u7684\u4f4d\u7f6e<\/li>\n<li>\u5c06\u65b0\u5143\u7d20\u63d2\u5165\u5230\u8be5\u4f4d\u7f6e\u540e<\/li>\n<li>\u91cd\u590d\u6b65\u9aa4 2~5<\/li>\n<\/ol>\n<p>\u52a8\u56fe\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/blog.nonot.cn\/wp-content\/uploads\/2021\/08\/3cc63-Insertion-sort-example-300px.gif\" alt=\"\" \/><\/p>\n<h4 id=\"0.16995898187953618\">\u4ee3\u7801<\/h4>\n<pre><code class=\"language-java hljs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">InsertSort<\/span> <\/span>{\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">sort<\/span><span class=\"hljs-params\">(Integer[] arr)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> i = <span class=\"hljs-number\">0<\/span>, length = arr.length; i &lt; length; i++) {\n            <span class=\"hljs-comment\">\/\/\u6709\u5e8f\u90e8\u5206\u4ece\u540e\u5411\u524d\u6bd4\u8f83,\u76f4\u5230\u627e\u5230\u5408\u9002\u7684\u4f4d\u7f6e<\/span>\n            <span class=\"hljs-keyword\">int<\/span> j = i, temp = arr[i];\n            <span class=\"hljs-comment\">\/\/\u5982\u679carr[j-1]&lt;=temp,\u8bf4\u660earr[j]\u9700\u4e3atemp\uff0c\u5426\u5219\u5c06arr[j-1]\u5411\u540e\u79fb\u52a8\u4e00\u4f4d<\/span>\n            <span class=\"hljs-keyword\">for<\/span> (; j &gt; <span class=\"hljs-number\">0<\/span> &amp;&amp; temp &lt; arr[j - <span class=\"hljs-number\">1<\/span>]; j--) {\n                arr[j] = arr[j - <span class=\"hljs-number\">1<\/span>];\n            }\n            arr[j] = temp;\n            System.out.println(<span class=\"hljs-string\">\"\u5f53\u524d\u6570\u7ec4\u72b6\u6001\u4e3a\uff1a\"<\/span> + Arrays.toString(arr));\n        }\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String[] args)<\/span> <\/span>{\n        Integer[] arr = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">65<\/span>, <span class=\"hljs-number\">32<\/span>, <span class=\"hljs-number\">12<\/span>, <span class=\"hljs-number\">21<\/span>};\n        InsertSort.sort(arr);\n        System.out.println(Arrays.toString(arr));\n    }\n}\n<\/code><\/pre>\n<h3 id=\"0.252760102321439\">\u5e0c\u5c14\u6392\u5e8f<\/h3>\n<h4 id=\"0.1674833604311723\">\u5b9a\u4e49<\/h4>\n<p>\u5e0c\u5c14\u6392\u5e8f\uff0c\u4e5f\u79f0\u9012\u51cf\u589e\u91cf\u6392\u5e8f\u7b97\u6cd5\uff0c\u662f\u63d2\u5165\u6392\u5e8f\u7684\u4e00\u79cd\u66f4\u9ad8\u6548\u7684\u6539\u8fdb\u7248\u672c\u3002\u5e0c\u5c14\u6392\u5e8f\u662f\u975e\u7a33\u5b9a\u6392\u5e8f\u7b97\u6cd5\u3002<\/p>\n<p>\u5e0c\u5c14\u6392\u5e8f\u662f\u57fa\u4e8e\u63d2\u5165\u6392\u5e8f\u7684\u4ee5\u4e0b\u4e24\u70b9\u6027\u8d28\u800c\u63d0\u51fa\u6539\u8fdb\u65b9\u6cd5\u7684\uff1a<\/p>\n<ul>\n<li>\u63d2\u5165\u6392\u5e8f\u5728\u5bf9\u51e0\u4e4e\u5df2\u7ecf\u6392\u597d\u5e8f\u7684\u6570\u636e\u64cd\u4f5c\u65f6\uff0c\u6548\u7387\u9ad8\uff0c\u5373\u53ef\u4ee5\u8fbe\u5230\u7ebf\u6027\u6392\u5e8f\u7684\u6548\u7387<\/li>\n<li>\u4f46\u63d2\u5165\u6392\u5e8f\u4e00\u822c\u6765\u8bf4\u662f\u4f4e\u6548\u7684\uff0c\u56e0\u4e3a\u63d2\u5165\u6392\u5e8f\u6bcf\u6b21\u53ea\u80fd\u5c06\u6570\u636e\u79fb\u52a8\u4e00\u4f4d<\/li>\n<\/ul>\n<h4 id=\"0.23453656880600593\">\u601d\u60f3<\/h4>\n<ol>\n<li>\u53d6\u4e00\u4e2a\u5c0f\u4e8e\u6570\u7ec4\u957f\u5ea6 n \u7684\u6574\u6570 n1\uff0c\u5c06\u6240\u6709\u95f4\u9694\u4e3a n1 \u7684\u6570\u5206\u6210\u4e00\u7ec4\uff0c\u5bf9\u5404\u7ec4\u8fdb\u884c\u76f4\u63a5\u63d2\u5165\u6392\u5e8f.\u7136\u540e n=n1\uff1b<\/li>\n<li>\u91cd\u590d\u4e0a\u8ff0\u64cd\u7eb5\uff0c\u76f4\u5230 n1=1 \u8fdb\u884c\u4e00\u6b21\u5b8c\u6574\u7684\u63d2\u5165\u6392\u5e8f\u540e\u7ed3\u675f\u3002<\/li>\n<\/ol>\n<h4 id=\"0.2337314101159722\">\u4ee3\u7801<\/h4>\n<pre><code class=\"language-java hljs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ShellSort<\/span> <\/span>{\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">sort<\/span><span class=\"hljs-params\">(Integer[] arr)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">int<\/span> n1 = arr.length \/ <span class=\"hljs-number\">2<\/span>;\n        <span class=\"hljs-comment\">\/\/ \u4e5f\u53ef\u5c06do\/while\u66ff\u6362\u6210\u5c3e\u9012\u5f52<\/span>\n        <span class=\"hljs-keyword\">do<\/span> {\n            <span class=\"hljs-comment\">\/\/\u5171n1\u7ec4\u6570\u636e\u9700\u8981\u8fdb\u884c\u76f4\u63a5\u63d2\u5165\u6392\u5e8f<\/span>\n            <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> start = <span class=\"hljs-number\">0<\/span>; start &lt; n1; start++) {\n                <span class=\"hljs-comment\">\/\/\u5bf9\u4e00\u7ec4\u6267\u884c\u63d2\u5165\u6392\u5e8f\uff0c\u7b2c\u4e00\u4e2a\u6570\u4e3aarr[start],\u589e\u91cf\u4e3an1<\/span>\n                <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> i = start; i &lt; arr.length; i += n1) {\n                    <span class=\"hljs-keyword\">int<\/span> j = i, temp = arr[i];\n                    <span class=\"hljs-keyword\">for<\/span> (; j &gt; start &amp;&amp; temp &lt; arr[j - n1]; j -= n1) {\n                        arr[j] = arr[j - n1];\n                    }\n                    arr[j] = temp;\n                }\n            }\n            n1 \/= <span class=\"hljs-number\">2<\/span>;\n        } <span class=\"hljs-keyword\">while<\/span> (n1 &gt;= <span class=\"hljs-number\">1<\/span>);\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String[] args)<\/span> <\/span>{\n        Integer[] arr = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">65<\/span>, <span class=\"hljs-number\">32<\/span>, <span class=\"hljs-number\">12<\/span>, <span class=\"hljs-number\">21<\/span>};\n        ShellSort.sort(arr);\n        System.out.println(Arrays.toString(arr));\n    }\n}\n<\/code><\/pre>\n<h2 id=\"0.19440021650269634\">\u9009\u62e9\u6392\u5e8f<\/h2>\n<h3 id=\"0.5890486970784254\">\u7b80\u5355\u9009\u62e9\u6392\u5e8f<\/h3>\n<h4 id=\"0.785373023714244\">\u5b9a\u4e49<\/h4>\n<p>\u662f\u4e00\u79cd\u7b80\u5355\u76f4\u89c2\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\u3002\u9996\u5148\u5728\u672a\u6392\u5e8f\u5e8f\u5217\u4e2d\u627e\u5230\u6700\u5c0f\uff08\u5927\uff09\u5143\u7d20\uff0c\u5b58\u653e\u5230\u6392\u5e8f\u5e8f\u5217\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u7136\u540e\uff0c\u518d\u4ece\u5269\u4f59\u672a\u6392\u5e8f\u5143\u7d20\u4e2d\u7ee7\u7eed\u5bfb\u627e\u6700\u5c0f\uff08\u5927\uff09\u5143\u7d20\uff0c\u7136\u540e\u653e\u5230\u5df2\u6392\u5e8f\u5e8f\u5217\u7684\u672b\u5c3e\u3002\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u5230\u6240\u6709\u5143\u7d20\u5747\u6392\u5e8f\u5b8c\u6bd5\u3002<\/p>\n<h4 id=\"0.7254871011182045\">\u601d\u60f3<\/h4>\n<p>\u7b80\u5355\u9009\u62e9\u6392\u5e8f\u987e\u540d\u601d\u4e49,\u6bcf\u6b21\u4ece\u65e0\u5e8f\u90e8\u5206\u9009\u51fa\u4e00\u4e2a\u6700\u5927\u7684\u6570\uff0c\u548c\u65e0\u5e8f\u90e8\u5206\u7684\u6700\u540e\u4e00\u4e2a\u503c\u4ea4\u6362\uff0c\u91cd\u590d n-1 \u6b21\u540e\u6240\u6709\u7684\u503c\u90fd\u53d8\u6210\u6709\u5e8f\u72b6\u6001.<\/p>\n<p>\u52a8\u753b\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/blog.nonot.cn\/wp-content\/uploads\/2021\/08\/b853a-Selection-Sort-Animation.gif\" alt=\"\" \/><\/p>\n<h4 id=\"0.8670835109926871\">\u4ee3\u7801<\/h4>\n<pre><code class=\"language-java hljs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SimpleSelectSort<\/span> <\/span>{\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">sort<\/span><span class=\"hljs-params\">(Integer[] arr)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">int<\/span> length = arr.length;\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> i = <span class=\"hljs-number\">0<\/span>; i &lt; length - <span class=\"hljs-number\">1<\/span>; i++) {\n            <span class=\"hljs-keyword\">int<\/span> maxIndex = <span class=\"hljs-number\">0<\/span>;\n            <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> j = <span class=\"hljs-number\">1<\/span>; j &lt; length - i; j++) {\n                <span class=\"hljs-keyword\">if<\/span> (arr[j] &gt; arr[maxIndex]) {\n                    maxIndex = j;\n                }\n            }\n            ArrayUtil.swap(arr, maxIndex, length - i);\n        }\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String[] args)<\/span> <\/span>{\n        Integer[] arr = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">65<\/span>, <span class=\"hljs-number\">32<\/span>, <span class=\"hljs-number\">12<\/span>, <span class=\"hljs-number\">21<\/span>};\n        sort(arr);\n        System.out.println(Arrays.toString(arr));\n    }\n}\n<\/code><\/pre>\n<h3 id=\"0.09635931110023299\">\u5806\u6392\u5e8f<\/h3>\n<h4 id=\"0.9044962091709101\">\u5b9a\u4e49<\/h4>\n<p>\u5806\u6392\u5e8f\uff08\u82f1\u8bed\uff1aHeapsort\uff09\u662f\u6307\u5229\u7528\u5806\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u6240\u8bbe\u8ba1\u7684\u4e00\u79cd\u6392\u5e8f\u7b97\u6cd5\u3002\u5806\u662f\u4e00\u4e2a\u8fd1\u4f3c\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u7ed3\u6784\uff0c\u5e76\u540c\u65f6\u6ee1\u8db3\u5806\u79ef\u7684\u6027\u8d28\uff1a\u5373\u5b50\u8282\u70b9\u7684\u952e\u503c\u6216\u7d22\u5f15\u603b\u662f\u5c0f\u4e8e\uff08\u6216\u8005\u5927\u4e8e\uff09\u5b83\u7684\u7236\u8282\u70b9\u3002<\/p>\n<p>\u4e0d\u4e86\u89e3<code>\u5806<\/code>\u7684\u53ef\u4ee5\u770b\u770b<a href=\"https:\/\/www.jianshu.com\/p\/6b526aa481b1\" target=\"_blank\" rel=\"noopener noreferrer\">\u8fd9\u7bc7<\/a>\uff0c\u7ffb\u8bd1\u7684\u633a\u597d\u7684\u3002<\/p>\n<h4 id=\"0.24694141748820586\">\u601d\u60f3<\/h4>\n<ol>\n<li>\u6784\u5efa\u5927\u9876\u5806(\u5347\u5e8f\u7528\u5927\u9876\u5806\uff0c\u964d\u5e8f\u7528\u5c0f\u9876\u5806) ,i=arr.lengh-1,n=arr.lengh<\/li>\n<li>\u5c06 arr[0]\u548c arr[i]\u4e92\u6362,<\/li>\n<li>i&#8211;<\/li>\n<li>\u91cd\u65b0\u5c06 arr 0 \u5230 i \u6784\u5efa\u4e3a\u5927\u9876\u5806<\/li>\n<li>\u91cd\u590d 2\uff0c3\uff0c4 \u76f4\u5230 i=1<\/li>\n<\/ol>\n<p>\u6784\u5efa\u5927\u9876\u5806\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u4ece\u6700\u540e\u4e00\u4e2a\u975e\u53f6\u5b50\u8282\u70b9\u5f00\u59cb\u4ece\u4e0b\u5f80\u4e0a\u8fdb\u884c\u8c03\u6574\u3002<\/li>\n<li>\u5c06\u8be5\u8282\u70b9\u7684\u503c\u8c03\u6574\u4e3a max(\u8282\u70b9\u503c\uff0c\u76f4\u63a5\u5b50\u8282\u70b9\u503c\uff09\uff0c\u6ce8\u610f\u5982\u679c\u4ea7\u751f\u4e86\u4ea4\u6362\u64cd\u4f5c\u8fd8\u8981\u8c03\u6574\u88ab\u4ea4\u6362\u8282\u70b9,\u8ba9\u5176\u4e5f\u662f max\uff08\u8282\u70b9\u503c\uff0c\u76f4\u63a5\u5b50\u8282\u70b9\u503c\uff09,\u76f4\u5230\u88ab\u4ea4\u6362\u8282\u70b9\u65e0\u5b50\u8282\u70b9<\/li>\n<\/ul>\n<h4 id=\"0.7561813710197756\">\u4ee3\u7801<\/h4>\n<pre><code class=\"language-java hljs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">HeapSort<\/span> <\/span>{\n\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">sort<\/span><span class=\"hljs-params\">(Integer[] arr)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">int<\/span> n = arr.length;\n        <span class=\"hljs-comment\">\/\/\u6784\u5efa\u5927\u9876\u5806<\/span>\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> i = n \/ <span class=\"hljs-number\">2<\/span> - <span class=\"hljs-number\">1<\/span>; i &gt;= <span class=\"hljs-number\">0<\/span>; i--) {\n            adjustHeap(arr, i, n);\n        }\n        <span class=\"hljs-comment\">\/\/\u6392\u5e8f<\/span>\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> i = n - <span class=\"hljs-number\">1<\/span>; i &gt; <span class=\"hljs-number\">0<\/span>; i--) {\n            ArrayUtil.swap(arr, <span class=\"hljs-number\">0<\/span>, arr[i]);\n            adjustHeap(arr, <span class=\"hljs-number\">0<\/span>, i);\n        }\n    }\n\n    <span class=\"hljs-comment\">\/**\n     * Description: \u8c03\u6574\u5806\n     *\n     * <span class=\"hljs-doctag\">@param<\/span> arr    \u6570\u7ec4\n     * <span class=\"hljs-doctag\">@param<\/span> index  \u8c03\u6574index\u5904\u7684\u5bf9\u7ed3\u6784\n     * <span class=\"hljs-doctag\">@param<\/span> length \u5806\u5927\u5c0f\n     * <span class=\"hljs-doctag\">@author<\/span> fanxb\n     * <span class=\"hljs-doctag\">@date<\/span> 2019\/7\/31 19:50\n     *\/<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">adjustHeap<\/span><span class=\"hljs-params\">(Integer[] arr, <span class=\"hljs-keyword\">int<\/span> index, <span class=\"hljs-keyword\">int<\/span> length)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">if<\/span> (index &gt;= length) {\n            <span class=\"hljs-keyword\">return<\/span>;\n        }\n        <span class=\"hljs-keyword\">int<\/span> maxIndex = index;\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> i = <span class=\"hljs-number\">2<\/span> * index + <span class=\"hljs-number\">1<\/span>; i &lt; length - <span class=\"hljs-number\">1<\/span> &amp;&amp; i &lt;= <span class=\"hljs-number\">2<\/span> * index + <span class=\"hljs-number\">2<\/span>; i++) {\n            <span class=\"hljs-keyword\">if<\/span> (arr[maxIndex] &lt; arr[i]) {\n                maxIndex = i;\n            }\n        }\n        <span class=\"hljs-comment\">\/\/\u5982\u679c\u8fdb\u884c\u4e86\u4ea4\u6362\uff0c\u8fd8\u8981\u8c03\u6574\u88ab\u4ea4\u6362\u8282\u70b9<\/span>\n        <span class=\"hljs-keyword\">if<\/span> (maxIndex != index) {\n            ArrayUtil.swap(arr, maxIndex, index);\n            adjustHeap(arr, maxIndex, length);\n        }\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String[] args)<\/span> <\/span>{\n        Integer[] arr = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">65<\/span>, <span class=\"hljs-number\">32<\/span>, <span class=\"hljs-number\">334<\/span>, <span class=\"hljs-number\">12<\/span>, <span class=\"hljs-number\">21<\/span>, <span class=\"hljs-number\">65<\/span>, <span class=\"hljs-number\">112<\/span>, <span class=\"hljs-number\">444443<\/span>};\n        ShellSort.sort(arr);\n        System.out.println(Arrays.toString(arr));\n    }\n}\n<\/code><\/pre>\n<h2 id=\"0.7515062385943925\">\u5f52\u5e76\u6392\u5e8f<\/h2>\n<h3 id=\"0.14005936783589412\">\u4e8c\u8def\u5f52\u5e76<\/h3>\n<h4 id=\"0.37982672570156883\">\u5b9a\u4e49<\/h4>\n<p>\u5f52\u5e76\u6392\u5e8f\uff08\u82f1\u8bed\uff1aMerge sort\uff0c\u6216 mergesort\uff09\uff0c\u662f\u521b\u5efa\u5728\u5f52\u5e76\u64cd\u4f5c\u4e0a\u7684\u4e00\u79cd\u6709\u6548\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u6548\u7387\u4e3a<img decoding=\"async\" src=\"http:\/\/blog.nonot.cn\/wp-content\/uploads\/2021\/08\/4c556-20190805171211.png\" alt=\"\" \/>\u30021945 \u5e74\u7531\u7ea6\u7ff0\u00b7\u51af\u00b7\u8bfa\u4f0a\u66fc\u9996\u6b21\u63d0\u51fa\u3002\u8be5\u7b97\u6cd5\u662f\u91c7\u7528\u5206\u6cbb\u6cd5\uff08Divide and Conquer\uff09\u7684\u4e00\u4e2a\u975e\u5e38\u5178\u578b\u7684\u5e94\u7528\uff0c\u4e14\u5404\u5c42\u5206\u6cbb\u9012\u5f52\u53ef\u4ee5\u540c\u65f6\u8fdb\u884c\u3002&#8211;\u7ef4\u57fa\u767e\u79d1<\/p>\n<h4 id=\"0.22577506078773157\">\u601d\u60f3<\/h4>\n<p>\u5f52\u5e76\u6392\u5e8f\u7684\u6838\u5fc3\u601d\u60f3\u662f\u5c06\u4e24\u4e2a\u6709\u5e8f\u7684\u6570\u7ec4\u5408\u5e76\u6210\u4e00\u4e2a\u5927\u7684\u6570\u7ec4\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a 1 \u6b21\u5f52\u5e76\u3002<\/p>\n<p>\u4e00\u6b21\u5f52\u5e76\u8fc7\u7a0b\u5982\u4e0b(arr1,arr2 \u4e24\u4e2a\u6709\u5e8f\u6570\u7ec4\uff0carr3 \u5b58\u653e\u6392\u5e8f\u540e\u7684\u6570\u7ec4,i=0,j=0,k=0)\uff1a<\/p>\n<ol>\n<li>\u5982\u679c<code>arr1[i]&lt;=arr2[j]<\/code>,\u90a3\u4e48<code>arr3[k]=arr1[i]<\/code>\uff0c<code>i++,k++<\/code>;\u5426\u5219\u00a0<code>arr3[k]=arr2[j]<\/code>\uff0c<code>j++,k++<\/code>;<\/li>\n<li>\u91cd\u590d 1\uff0c\u76f4\u5230\u67d0\u4e2a\u6709\u5e8f\u6570\u7ec4\u5168\u90e8\u52a0\u5165\u5230 arr3 \u4e2d\uff0c\u7136\u540e\u5c06\u53e6\u5916\u4e00\u4e2a\u6570\u7ec4\u5269\u4f59\u7684\u90e8\u5206\u52a0\u5230 arr3 \u4e2d\u5373\u53ef\u3002<\/li>\n<\/ol>\n<p>\u4f46\u662f\u4e00\u4e2a\u65e0\u987b\u6570\u7ec4\u663e\u7136\u4e0d\u80fd\u76f4\u63a5\u62c6\u6210\u4e24\u4e2a\u6709\u5e8f\u6570\u7ec4\uff0c\u8fd9\u5c31\u9700\u8981\u7528\u5230<code>\u5206\u6cbb<\/code>\u7684\u601d\u60f3\u3002\u5c06\u6570\u7ec4\u4e00\u5c42\u4e00\u5c42\u7684\u62c6\u5206\uff0c\u76f4\u5230\u5355\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a 1\uff08\u957f\u5ea6\u4e3a 1 \u7684\u6570\u7ec4\u53ef\u4ee5\u8ba4\u4e3a\u662f\u6709\u5e8f\u7684\uff09\uff0c\u7136\u540e\u518d\u53cd\u8fc7\u6765\u4e00\u5c42\u5c42\u8fdb\u884c\u5f52\u5e76\u64cd\u4f5c\uff0c\u90a3\u4e48\u6700\u540e\u6570\u7ec4\u5c31\u53d8\u6210\u6709\u5e8f\u7684\u4e86\u3002<\/p>\n<h4 id=\"0.41950156706377495\">\u4ee3\u7801<\/h4>\n<pre><code class=\"language-java hljs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MergeSort<\/span> <\/span>{\n\n    <span class=\"hljs-comment\">\/**\n     * Description:\n     *\n     * <span class=\"hljs-doctag\">@param<\/span> arr   \u5f85\u6392\u5e8f\u6570\u7ec4\n     * <span class=\"hljs-doctag\">@param<\/span> start \u5f00\u59cb\u4e0b\u6807\n     * <span class=\"hljs-doctag\">@param<\/span> end   \u7ed3\u675f\u4e0b\u6807\n     * <span class=\"hljs-doctag\">@author<\/span> fanxb\n     * <span class=\"hljs-doctag\">@date<\/span> 2019\/8\/6 9:29\n     *\/<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">mergeSort<\/span><span class=\"hljs-params\">(Integer[] arr, <span class=\"hljs-keyword\">int<\/span> start, <span class=\"hljs-keyword\">int<\/span> end)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">if<\/span> (start &gt;= end) {\n            <span class=\"hljs-keyword\">return<\/span>;\n        }\n        <span class=\"hljs-keyword\">int<\/span> half = (start + end) \/ <span class=\"hljs-number\">2<\/span>;\n        <span class=\"hljs-comment\">\/\/\u5f52\u5e76\u5de6\u8fb9<\/span>\n        mergeSort(arr, start, half);\n        <span class=\"hljs-comment\">\/\/\u5f52\u5e76\u53f3\u8fb9<\/span>\n        mergeSort(arr, half + <span class=\"hljs-number\">1<\/span>, end);\n        <span class=\"hljs-comment\">\/\/\u5408\u5e76<\/span>\n        merge(arr, start, half, end);\n    }\n\n    <span class=\"hljs-comment\">\/**\n     * Description:\n     *\n     * <span class=\"hljs-doctag\">@param<\/span> arr arr\n     * <span class=\"hljs-doctag\">@author<\/span> fanxb\n     * <span class=\"hljs-doctag\">@date<\/span> 2019\/8\/5 17:36\n     *\/<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">merge<\/span><span class=\"hljs-params\">(Integer[] arr, <span class=\"hljs-keyword\">int<\/span> start, <span class=\"hljs-keyword\">int<\/span> half, <span class=\"hljs-keyword\">int<\/span> end)<\/span> <\/span>{\n        ArrayList&lt;Integer&gt; tempList = <span class=\"hljs-keyword\">new<\/span> ArrayList&lt;&gt;();\n        <span class=\"hljs-keyword\">int<\/span> i = start, j = half + <span class=\"hljs-number\">1<\/span>;\n        <span class=\"hljs-comment\">\/\/ \u5faa\u73af\u6bd4\u8f83\uff0c\u5c06\u8f83\u5c0f\u7684\u653e\u5230tempList\u4e2d<\/span>\n        <span class=\"hljs-keyword\">while<\/span> (i &lt;= half &amp;&amp; j &lt;= end) {\n            <span class=\"hljs-keyword\">if<\/span> (arr[i] &lt;= arr[j]) {\n                tempList.add(arr[i]);\n                i++;\n            } <span class=\"hljs-keyword\">else<\/span> {\n                tempList.add(arr[j]);\n                j++;\n            }\n        }\n        <span class=\"hljs-keyword\">if<\/span> (i &gt; half) {\n            <span class=\"hljs-comment\">\/\/\u8bf4\u660e\u7b2c\u4e00\u4e2a\u6570\u7ec4\u5df2\u7ecf\u5b8c\u4e86\uff0c\u5c06\u7b2c\u4e8c\u4e2a\u6570\u7ec4\u7684\u5269\u4f59\u90e8\u5206\u653e\u5230tempList\u4e2d<\/span>\n            <span class=\"hljs-keyword\">while<\/span> (j &lt;= end) {\n                tempList.add(arr[j]);\n                j++;\n            }\n        } <span class=\"hljs-keyword\">else<\/span> {\n            <span class=\"hljs-comment\">\/\/\u8bf4\u660e\u7b2c\u4e8c\u4e2a\u6570\u7ec4\u5df2\u7ecf\u5b8c\u4e86\uff0c\u5c06\u7b2c\u4e00\u4e2a\u6570\u7ec4\u5269\u4f59\u90e8\u5206\u653e\u5230tempList\u4e2d<\/span>\n            <span class=\"hljs-keyword\">while<\/span> (i &lt;= half) {\n                <span class=\"hljs-comment\">\/\/\u8bf4\u660e\u7b2c\u4e8c\u4e2a\u6570\u7ec4\u5904\u7406\u5b8c\u4e86<\/span>\n                tempList.add(arr[i]);\n                i++;\n            }\n        }\n        <span class=\"hljs-comment\">\/\/\u6700\u540e\u5c06tempList\u590d\u5236\u5230arr\u4e2d<\/span>\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> k = <span class=\"hljs-number\">0<\/span>, length = tempList.size(); k &lt; length; k++) {\n            arr[start + k] = tempList.get(k);\n        }\n\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String[] args)<\/span> <\/span>{\n        Integer[] arr = {<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">2<\/span>};\n        mergeSort(arr, <span class=\"hljs-number\">0<\/span>, arr.length - <span class=\"hljs-number\">1<\/span>);\n        System.out.println(Arrays.toString(arr));\n    }\n}\n<\/code><\/pre>\n<h1 id=\"0.5746397682008701\">\u975e\u6bd4\u8f83\u6392\u5e8f<\/h1>\n<h2 id=\"0.28252907850455666\">\u8ba1\u6570\u6392\u5e8f<\/h2>\n<h3 id=\"0.5068648283082515\">\u5b9a\u4e49<\/h3>\n<p>\u8ba1\u6570\u6392\u5e8f\u4e0d\u4ee5\u6bd4\u8f83\u4e3a\u57fa\u7840\uff0c\u6838\u5fc3\u5728\u4e8e\u5c06\u6570 a \u5b58\u653e\u5728 arr[a]\u4e0a,\u6392\u5e8f\u901f\u5ea6\u8d85\u7ea7\u5feb\uff0c\u4f46\u662f\u8981\u6c42\u8f93\u5165\u7684\u6570\u5fc5\u987b\u662f\u6709\u786e\u5b9a\u8303\u56f4\u7684\u6574\u6570\u3002<\/p>\n<h3 id=\"0.1291447768998275\">\u601d\u60f3<\/h3>\n<p>\u5047\u8bbe\u5bf9\u4e8e\u8303\u56f4 0-100 \u7684\u6574\u6570\u8fdb\u884c\u6392\u5e8f<\/p>\n<ol>\n<li>\u5b9a\u4e49\u957f\u5ea6\u4e3a 101 \u7684\u6570\u7ec4 arr,\u5e76\u5c06\u503c\u521d\u59cb\u5316\u4e3a 0<\/li>\n<li>\u8bfb\u53d6\u4e00\u4e2a\u6570 a\uff0c\u7136\u540e arr[a]++<\/li>\n<li>\u904d\u5386 arr\uff0c\u6570\u7ec4\u4e0a\u7684\u6bcf\u4e2a\u503c\u8868\u793a\u5bf9\u5e94\u4e0b\u6807\u7684\u6570\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/li>\n<\/ol>\n<h3 id=\"0.8234537178719414\">\u4ee3\u7801<\/h3>\n<pre><code class=\"language-java hljs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">CountSort<\/span> <\/span>{\n    <span class=\"hljs-comment\">\/**\n     * Description:\n     *\n     * <span class=\"hljs-doctag\">@param<\/span> arr \u5f85\u6392\u5e8f\u6570\u7ec4\n     * <span class=\"hljs-doctag\">@return<\/span> void\n     * <span class=\"hljs-doctag\">@author<\/span> fanxb\n     * <span class=\"hljs-doctag\">@date<\/span> 2019\/8\/6 17:36\n     *\/<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">sort<\/span><span class=\"hljs-params\">(Integer[] arr, Integer minValue, Integer maxValue)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">int<\/span> range = maxValue - minValue + <span class=\"hljs-number\">1<\/span>;\n        Integer[] numCount = <span class=\"hljs-keyword\">new<\/span> Integer[range];\n        Arrays.fill(numCount, <span class=\"hljs-number\">0<\/span>);\n        <span class=\"hljs-keyword\">for<\/span> (Integer item : arr) {\n            item = item - minValue;\n            numCount[item]++;\n        }\n        <span class=\"hljs-keyword\">int<\/span> count = <span class=\"hljs-number\">0<\/span>;\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> i = <span class=\"hljs-number\">0<\/span>; i &lt; range; i++) {\n            <span class=\"hljs-keyword\">if<\/span> (numCount[i] == <span class=\"hljs-number\">0<\/span>) {\n                <span class=\"hljs-keyword\">continue<\/span>;\n            }\n            <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> j = <span class=\"hljs-number\">0<\/span>; j &lt; numCount[i]; j++) {\n                arr[count] = minValue + i;\n                count++;\n            }\n        }\n    }\n\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String[] args)<\/span> <\/span>{\n        Integer[] arr = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">65<\/span>, <span class=\"hljs-number\">32<\/span>, <span class=\"hljs-number\">334<\/span>, <span class=\"hljs-number\">12<\/span>, <span class=\"hljs-number\">21<\/span>, <span class=\"hljs-number\">65<\/span>, <span class=\"hljs-number\">112<\/span>, <span class=\"hljs-number\">444443<\/span>};\n        sort(arr, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">444443<\/span>);\n        System.out.println(Arrays.toString(arr));\n    }\n}\n<\/code><\/pre>\n<p><strong>PS<\/strong><\/p>\n<p>\u8ba1\u6570\u6392\u5e8f\u6709\u5f88\u591a\u7684\u53d8\u79cd\uff0c\u4e0b\u9762\u5217\u4e3e\u51e0\u79cd\uff1a<\/p>\n<ol>\n<li>\u5b58\u5728\u8d1f\u6570\u600e\u4e48\u529e?<\/li>\n<\/ol>\n<p>\u5f88\u7b80\u5355\uff0c\u5148\u8fdb\u884c\u4e00\u6b21\u904d\u5386\u5c06\u6b63\u6570\u8d1f\u6570\u5206\u5f00\uff0c\u5728\u5206\u522b\u8fdb\u884c\u6392\u5e8f\uff0c\u8d1f\u6570\u53d6\u53cd\u540e\u518d\u6392\u3002<\/p>\n<ol start=\"2\">\n<li>\u6709\u7a7a\u95f4\u9650\u5236\u4e14\u6570\u7ec4\u975e\u5e38\u5927\u600e\u4e48\u529e\uff1f<\/li>\n<\/ol>\n<p>\u8fd9\u91cc\u53ef\u4ee5\u5229\u7528\u6587\u4ef6\u6765\u5b9e\u73b0\u3002\u5148\u5c06\u8d85\u5927\u7684\u6570\u7ec4\u6309\u7167\u89c4\u5219\u5206\u6210\u51e0\u4e2a\u90e8\u5206\uff0c\u5206\u522b\u5b58\u5230\u6587\u4ef6\u4e2d\uff08\u6bd4\u5982 1-1000000 \u653e\u5728\u6587\u4ef6 1 \u4e2d\uff0c1000001-2000000 \u653e\u5728\u6587\u4ef6 2 \u4e2d\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff09\u5c31\u5c06\u8d85\u5927\u7684\u6570\u7ec4\u5206\u6210\u4e86\u5c0f\u7684\u6570\u7ec4\uff0c\u7136\u540e\u518d\u5206\u522b\u8ba1\u6570\u6392\u5e8f\u5373\u53ef\u3002<\/p>\n<h2 id=\"0.8644597747174367\">\u57fa\u6570\u6392\u5e8f<\/h2>\n<h3 id=\"0.012056475779483211\">\u5b9a\u4e49<\/h3>\n<p>\u662f\u4e00\u79cd\u975e\u6bd4\u8f83\u578b\u6574\u6570\u6392\u5e8f\u7b97\u6cd5\uff0c\u5176\u539f\u7406\u662f\u5c06\u6574\u6570\u6309\u4f4d\u6570\u5207\u5272\u6210\u4e0d\u540c\u7684\u6570\u5b57\uff0c\u7136\u540e\u6309\u6bcf\u4e2a\u4f4d\u6570\u5206\u522b\u6bd4\u8f83\u3002\u7531\u4e8e\u6574\u6570\u4e5f\u53ef\u4ee5\u8868\u8fbe\u5b57\u7b26\u4e32\uff08\u6bd4\u5982\u540d\u5b57\u6216\u65e5\u671f\uff09\u548c\u7279\u5b9a\u683c\u5f0f\u7684\u6d6e\u70b9\u6570\uff0c\u6240\u4ee5\u57fa\u6570\u6392\u5e8f\u4e5f\u4e0d\u662f\u53ea\u80fd\u4f7f\u7528\u4e8e\u6574\u6570\u3002\u57fa\u6570\u6392\u5e8f\u7684\u53d1\u660e\u53ef\u4ee5\u8ffd\u6eaf\u5230 1887 \u5e74\u8d6b\u5c14\u66fc\u00b7\u4f55\u4e50\u793c\u5728\u6253\u5b54\u5361\u7247\u5236\u8868\u673a\uff08Tabulation Machine\uff09\u4e0a\u7684\u8d21\u732e\u3002&#8211;\u6765\u81ea\u7ef4\u57fa\u767e\u79d1<\/p>\n<p>\u57fa\u6570\u6392\u5e8f\u53ef\u4ee5\u91c7\u7528 LSD(\u4ece\u9ad8\u4f4d\u5f00\u59cb),MSD(\u4ece\u4f4e\u4f4d\u5f00\u59cb),\u8fd9\u91cc\u4ee5 MSD \u4e3a\u4f8b\u3002<\/p>\n<p>\uff08\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u601d\u8003\u601d\u8003\u5982\u4f55\u7528 LSD \u5b9e\u73b0\uff0c\u76ee\u524d\u7f51\u4e0a\u7edd\u5927\u591a\u6570\u90fd\u662f MSD \u5b9e\u73b0\u7684\uff09<\/p>\n<h3 id=\"0.5935947046066585\">\u601d\u60f3<\/h3>\n<ol>\n<li>\u5148\u521b\u5efa 10 \u4e2a\u6876\uff0c\u5206\u522b\u5bf9\u5e94\u6570\u5b57 0-9\u3002<\/li>\n<li>\u4ece\u5de6\u5f80\u53f3\u53d6\u7b2c\u4e00\u4f4d\u7684\u6570\u5b57\uff0c\u653e\u5230\u5bf9\u5e94\u7684\u6876\u4e2d<\/li>\n<li>\u4f9d\u6b21\u4ece\u6876\u4e2d\u53d6\u51fa\u6570\u5b57(\u8981\u6309\u7167\u5148\u8fdb\u5148\u51fa\u7684\u539f\u5219)\u653e\u5230\u6e90\u6570\u7ec4\u4e2d\u3002<\/li>\n<li>\u91cd\u590d 2,3 \u6b65\u9aa4\uff0c\u4f9d\u6b21\u5bf9\u7b2c\u4e8c\u3001\u7b2c\u4e09\u3002\u3002\u3002\u4f4d\u7684\u6570\u5b57\u6392\u5e8f\uff0c\u76f4\u5230\u6700\u5927\u4f4d\u6570\u5904\u7406\u5b8c\u6bd5\u3002<\/li>\n<\/ol>\n<p>\u4e3a\u4ec0\u4e48\u80fd\u591f\u8fd9\u6837\u6392\u5e8f\u5462\uff1f\u7b2c\u4e00\u904d\u6392\u5e8f\u5b8c\u6bd5\u540e\uff0c\u6240\u6709\u7684\u6570\u662f\u6309\u7167\u4e2a\u4f4d\u6392\u5e8f\u7684\uff0c\u5bf9\u4e8e\u6240\u6709\u5c0f\u4e8e 10 \u7684\u6570\u6765\u8bf4\uff0c\u4ed6\u4eec\u5df2\u7ecf\u662f\u76f8\u5bf9\u6709\u5e8f(\u5e76\u4e0d\u662f\u8bf4\u4f4d\u7f6e\u4e0d\u518d\u53d8\u5316\uff0c\u53ea\u662f\u76f8\u5bf9\u987a\u5e8f\u4e0d\u518d\u53d8\u5316)\u7684\u4e86\uff0c\u5728\u7b2c\u4e8c\u8f6e\u5bf9\u5341\u4f4d\u6392\u5e8f\u65f6\uff0c\u6240\u6709\u7684\u4e2a\u4f4d\u6570\u90fd\u5c06\u88ab\u653e\u5230 0 \u6876\u4e86\uff0c\u7528\u5148\u8fdb\u5148\u51fa\u7b56\u7565\u5904\u7406\u8fd9\u4e9b\u4e2a\u4f4d\u6570\uff0c\u53d6\u51fa\u65f6\u4e2a\u4f4d\u6570\u8fd8\u662f\u6709\u5e8f\u7684\u3002<\/p>\n<p>\u7b2c\u4e8c\u8f6e\u6392\u5e8f\u540e\u6240\u6709\u5c0f\u4e8e 10 \u7684\u6570\u7684\u4f4d\u7f6e\u5df2\u7ecf\u786e\u5b9a\u4e14\u4e0d\u518d\u53d8\u5316\uff0c\u5927\u4e8e 10 \u5c0f\u4e8e 100 \u7684\u6570\u7684\u4f4d\u7f6e\u5df2\u7ecf\u76f8\u5bf9\u6709\u5e8f.\u5728\u7b2c\u4e09\u8f6e\u4e2d\u6240\u6709\u5c0f\u4e8e 100 \u7684\u6570\u90fd\u5c06\u88ab\u653e\u5230 0 \u6876\uff0c\u8fd9\u65f6\u76f8\u5bf9\u6709\u5e8f\u5c31\u53d8\u6210\u4e86\u7edd\u5bf9\u7684\u4e86\uff0c\u53d6\u51fa\u540e\u4f4d\u7f6e\u4e0d\u518d\u53d8\u5316\u3002<\/p>\n<p>\u7b2c\u4e09\u8f6e\u6392\u5e8f\u540e\u6240\u6709\u5c0f\u4e8e 100 \u7684\u6570\u7684\u4f4d\u7f6e\u5df2\u7ecf\u786e\u5b9a\u4e14\u4e0d\u518d\u53d8\u5316\u3002\u4ee5\u6b64\u7c7b\u63a8\u76f4\u5230\u5168\u90e8\u6392\u5e8f\u5b8c\u6210\u3002<\/p>\n<p>\u52a8\u56fe\u5982\u4e0b\uff1a<\/p>\n<h3 id=\"0.3062640254009594\">\u4ee3\u7801<\/h3>\n<pre class=\"\"><code class=\"language-java hljs\">\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">RadixSort<\/span> <\/span>{\n\n    <span class=\"hljs-meta\">@SuppressWarnings(\"unchecked\")<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">sort<\/span><span class=\"hljs-params\">(Integer[] arr)<\/span> <\/span>{\n        <span class=\"hljs-comment\">\/\/\u5b9a\u4e49\u6876<\/span>\n        LinkedList&lt;Integer&gt;[] buckets = <span class=\"hljs-keyword\">new<\/span> LinkedList[<span class=\"hljs-number\">10<\/span>];\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> i = <span class=\"hljs-number\">0<\/span>; i &lt; <span class=\"hljs-number\">10<\/span>; i++) {\n            buckets[i] = <span class=\"hljs-keyword\">new<\/span> LinkedList&lt;&gt;();\n        }\n        <span class=\"hljs-keyword\">int<\/span> size = arr.length;\n        <span class=\"hljs-comment\">\/\/\u5f53\u524d\u5904\u7406\u7b2c\u51e0\u4f4d\u7684\u6570<\/span>\n        <span class=\"hljs-keyword\">int<\/span> count = <span class=\"hljs-number\">0<\/span>;\n        <span class=\"hljs-keyword\">while<\/span> (<span class=\"hljs-keyword\">true<\/span>) {\n            <span class=\"hljs-comment\">\/\/\u662f\u5426\u7ee7\u7eed\u8fdb\u4f4d<\/span>\n            <span class=\"hljs-keyword\">boolean<\/span> isContinue = <span class=\"hljs-keyword\">false<\/span>;\n            <span class=\"hljs-comment\">\/\/\u5c06\u6570\u653e\u5230\u6876\u4e2d<\/span>\n            <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> i = <span class=\"hljs-number\">0<\/span>; i &lt; size; i++) {\n                <span class=\"hljs-keyword\">int<\/span> temp = arr[i] \/ (<span class=\"hljs-keyword\">int<\/span>) Math.pow(<span class=\"hljs-number\">10<\/span>, count) % <span class=\"hljs-number\">10<\/span>;\n                <span class=\"hljs-keyword\">if<\/span> (!isContinue &amp;&amp; temp != <span class=\"hljs-number\">0<\/span>) {\n                    <span class=\"hljs-comment\">\/\/ \u5982\u679c\u5b58\u5728\u4e00\u4e2a\u6570\u53d6\u7684\u503c\u4e0d\u4e3a0\uff0c\u8bf4\u660e\u8fd8\u8981\u7ee7\u7eed\u5faa\u73af\u3002<\/span>\n                    isContinue = <span class=\"hljs-keyword\">true<\/span>;\n                }\n                buckets[temp].addLast(arr[i]);\n            }\n            <span class=\"hljs-keyword\">if<\/span> (!isContinue) {\n                <span class=\"hljs-keyword\">return<\/span>;\n            }\n            <span class=\"hljs-comment\">\/\/\u4ece\u6876\u4e2d\u53d6\u51fa\u653e\u5230arr\u4e2d,\u6ce8\u610f\u4ee5\u4ec0\u4e48\u987a\u5e8f\u653e\u8fdb\u53bb\u7684\u5c31\u8981\u4ee5\u4ec0\u4e48\u987a\u5e8f\u53d6\u51fa\u6765\uff08\u5148\u8fdb\u5148\u51fa\uff09<\/span>\n            <span class=\"hljs-keyword\">int<\/span> index = <span class=\"hljs-number\">0<\/span>;\n            <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> i = <span class=\"hljs-number\">0<\/span>; i &lt; <span class=\"hljs-number\">10<\/span>; i++) {\n                Integer item;\n                <span class=\"hljs-keyword\">while<\/span> ((item = buckets[i].pollFirst()) != <span class=\"hljs-keyword\">null<\/span>) {\n                    arr[index++] = item;\n                }\n            }\n            <span class=\"hljs-comment\">\/\/\u4f4d\u6570+1<\/span>\n            count++;\n        }\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String[] args)<\/span> <\/span>{\n        Integer[] arr = {<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">31<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">29<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">2<\/span>};\n        sort(arr);\n        System.out.println(Arrays.toString(arr));\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u5982\u65e0\u7279\u6b8a\u8bf4\u660e\u90fd\u662f\u6309\u7167\u5347\u5e8f\u8fdb\u884c\u6392\u5e8f\u3002 \u6bd4\u8f83\u7c7b\u6392\u5e8f \u4ea4\u6362\u6392\u5e8f&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-311","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/311","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=311"}],"version-history":[{"count":1,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/311\/revisions"}],"predecessor-version":[{"id":1257,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/311\/revisions\/1257"}],"wp:attachment":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/media?parent=311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/categories?post=311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/tags?post=311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}