{"id":185,"date":"2025-09-24T22:00:22","date_gmt":"2025-09-24T14:00:22","guid":{"rendered":"https:\/\/www.xunfen.cloud\/?p=185"},"modified":"2025-12-17T07:16:07","modified_gmt":"2025-12-16T23:16:07","slug":"%e3%80%90%e7%ac%94%e8%ae%b0%e3%80%91%e5%87%a0%e4%b8%aa%e5%85%b3%e4%ba%8e%e6%8e%92%e5%ba%8f%e4%b8%8e%e6%9f%a5%e6%89%be%e7%9a%84%e7%ae%97%e6%b3%95%ef%bc%88%e4%ba%8c%ef%bc%89","status":"publish","type":"post","link":"https:\/\/blog.xunfen.cloud\/?p=185","title":{"rendered":"\u3010\u7b14\u8bb0\u3011\u51e0\u4e2a\u5173\u4e8e\u6392\u5e8f\u4e0e\u67e5\u627e\u7684\u7b97\u6cd5\uff08\u4e8c\uff09"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4e8c.\u6392\u5e8f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e00.\u5192\u6ce1\u6392\u5e8f<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5b9a\u4e49\u4fe9\u4e2a\u53d8\u91cf\uff0c<strong>\u5916\u5c42\u53d8\u91cfi<\/strong>(\u5faa\u73af\u8f6e\u6570)\u4e0e<strong>\u5185\u5c42\u53d8\u91cfj<\/strong>(\u6bd4\u8f83\u7684\u6b21\u6570)<\/li>\n\n\n\n<li>\u904d\u5386\u6bcf\u4e00\u6b21\u5916\u5c42\u5faa\u73af\u65f6\u96c6\u5408\u7684\u6570\u636e\uff08\u5176\u4e2d\u6bcf\u6b21\u904d\u5386\u6570\u91cf\u662f\u9012\u51cf\u7684\uff0c\u56e0\u4e3a\u5f53\u4f60\u7ecf\u8fc7\u4e00\u6b21\u5192\u6ce1\u540e\u5df2\u7ecf\u6392\u597d\u4e86\u4e00\u4e2a\u5143\u7d20\uff09\uff0c\u627e\u5230\u6700\u5927\u5143\u7d20\uff0c\u5176\u4e2d\uff0c\u5982\u679c\u524d\u4e00\u4e2a\u5143\u7d20\u5927\u4e8e\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u90a3\u4e48\u4e8c\u8005\u4ea4\u6362<\/li>\n\n\n\n<li>\u5916\u5c42\u8fdb\u884c\u4e0b\u4e00\u6b21\u5faa\u73af<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>public class BubbleDemo1 {\n    public static void main(String&#91;] args) {\n        int&#91;] arr = { 2, 4, 5, 3, 1 };\n        for (int i = 0; i &lt; arr.length - 1; i++) {\n            for (int j = 0; j &lt; arr.length - 1 - i; j++) {\n                if (arr&#91;j] &gt; arr&#91;j + 1]) {\n                    int temp = arr&#91;j];\n                    arr&#91;j] = arr&#91;j + 1];\n                    arr&#91;j + 1] = temp;\n                }\n            }\n        }\n        for (int i = 0; i &lt; arr.length; i++) {\n            System.out.print(arr&#91;i] + \" \");\n        }\n    }\n\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c.\u9009\u62e9\u6392\u5e8f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0e<em>\u5192\u6ce1\u6392\u5e8f<\/em>\u6709\u4e9b\u7c7b\u4f3c\uff0c\u4f46\u662f\u8fd8\u662f\u6709\u4e0d\u540c\uff0c\u4ee5\u4e0b\u5217\u4e3e\u90e8\u5206\u4e0d\u540c(\u901a\u4e49\u8f85\u52a9)<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p class=\"wp-block-paragraph\">1.&nbsp;<strong>\u5de5\u4f5c\u539f\u7406\u4e0d\u540c<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9009\u62e9\u6392\u5e8f<\/strong>\uff1a\u6bcf\u8f6e\u4ece\u672a\u6392\u5e8f\u90e8\u5206\u9009\u62e9\u6700\u5c0f\u5143\u7d20\uff0c\u76f4\u63a5\u653e\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u672b\u5c3e<\/li>\n\n\n\n<li><strong>\u5192\u6ce1\u6392\u5e8f<\/strong>\uff1a\u76f8\u90bb\u5143\u7d20\u4e24\u4e24\u6bd4\u8f83\uff0c\u5982\u679c\u987a\u5e8f\u9519\u8bef\u5c31\u4ea4\u6362\uff0c\u8f83\u5927\u5143\u7d20\u9010\u6b65&#8221;\u5192\u6ce1&#8221;\u5230\u672b\u5c3e<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">2.&nbsp;<strong>\u4ea4\u6362\u6b21\u6570\u4e0d\u540c<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9009\u62e9\u6392\u5e8f<\/strong>\uff1a\u6bcf\u8f6e\u6700\u591a\u4ea4\u63621\u6b21\uff0c\u603b\u7684\u4ea4\u6362\u6b21\u6570\u6700\u591a\u4e3a n-1 \u6b21<\/li>\n\n\n\n<li><strong>\u5192\u6ce1\u6392\u5e8f<\/strong>\uff1a\u6bcf\u8f6e\u53ef\u80fd\u8fdb\u884c\u591a\u6b21\u4ea4\u6362\uff0c\u4ea4\u6362\u6b21\u6570\u901a\u5e38\u66f4\u591a<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">3.&nbsp;<strong>\u6bd4\u8f83\u6b21\u6570<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9009\u62e9\u6392\u5e8f<\/strong>\uff1a\u6bd4\u8f83\u6b21\u6570\u56fa\u5b9a\u4e3a n(n-1)\/2 \u6b21<\/li>\n\n\n\n<li><strong>\u5192\u6ce1\u6392\u5e8f<\/strong>\uff1a\u6bd4\u8f83\u6b21\u6570\u4e5f\u4e3a n(n-1)\/2 \u6b21\uff0c\u4f46\u4f1a\u6709\u66f4\u591a\u6b21\u4ea4\u6362\u64cd\u4f5c<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">4.&nbsp;<strong>\u7a33\u5b9a\u6027<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9009\u62e9\u6392\u5e8f<\/strong>\uff1a\u4e0d\u7a33\u5b9a\u6392\u5e8f\uff08\u76f8\u7b49\u5143\u7d20\u7684\u76f8\u5bf9\u4f4d\u7f6e\u53ef\u80fd\u6539\u53d8\uff09<\/li>\n\n\n\n<li><strong>\u5192\u6ce1\u6392\u5e8f<\/strong>\uff1a\u7a33\u5b9a\u6392\u5e8f\uff08\u76f8\u7b49\u5143\u7d20\u7684\u76f8\u5bf9\u4f4d\u7f6e\u4e0d\u4f1a\u6539\u53d8\uff09<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">5.&nbsp;<strong>\u6267\u884c\u6548\u7387<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9009\u62e9\u6392\u5e8f<\/strong>\uff1a\u7531\u4e8e\u4ea4\u6362\u6b21\u6570\u5c11\uff0c\u5b9e\u9645\u6267\u884c\u6548\u7387\u901a\u5e38\u6bd4\u5192\u6ce1\u6392\u5e8f\u9ad8<\/li>\n\n\n\n<li><strong>\u5192\u6ce1\u6392\u5e8f<\/strong>\uff1a\u4ea4\u6362\u9891\u7e41\uff0c\u6267\u884c\u6548\u7387\u76f8\u5bf9\u8f83\u4f4e<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>public class SelectionDemo1 {\n    public static void main(String&#91;] args) {\n        int&#91;] arr = { 2, 5, 1, 4, 3 };\n        for (int i = 0; i &lt; arr.length - 1; i++) {\n            for (int j = i + 1; j &lt; arr.length; j++) {\n                if (arr&#91;i] &gt; arr&#91;j]) {\n                    int temp = arr&#91;i];\n                    arr&#91;i] = arr&#91;j];\n                    arr&#91;j] = temp;\n                }\n            }\n        }\n        for (int i = 0; i &lt; arr.length; i++) {\n            System.out.print(arr&#91;i] + \" \");\n        }\n    }\n\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e09.\u63d2\u5165\u6392\u5e8f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6570\u5217\u4e2d\u627e\u5230\u65e0\u5e8f\u6570\u5217\u5f00\u59cb\u7684\u4f4d\u7f6e\uff08\u4e00\u822c\u4ece-1\u5f00\u59cb\u67e5\u627e\u7136\u540e\u4f9d\u6b21\u589e\u52a0\u67e5\u627e\u53d8\u91cf\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class InsertDemo1 {\n    public static void main(String&#91;] args) {\n        int&#91;] arr = { 3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48 };\n        int startIndex = -1;\/\/\u4ece-1\u5f00\u59cb\u67e5\u627e\u65e0\u5e8f\u4f4d\u7f6e\uff0c\u5982\u679c\u672c\u8eab\u662f\u6709\u5e8f\u7684\uff0c\u90a3\u4e48-1\u5c31\u662f\u65e0\u6548\u7d22\u5f15\uff0c\u5c06\u4e0d\u4f1a\u6267\u884c\u6392\u5e8f\u7684\u4ee3\u7801\n        for(int i = 0; i &lt; arr.length; i++) {\/\/\u904d\u5386\u6570\u7ec4\uff0c\u627e\u5230\u65e0\u5e8f\u5f00\u59cb\u7684\u4f4d\u7f6e\n            if (arr&#91;i] &lt; arr&#91;i + 1]) {\n                startIndex = i + 1;\n                break;\n            }\n        }\n\n        for (int i = startIndex; i &lt; arr.length; i++) {\/\/\u4ece\u4e0a\u65b9\u62ff\u5230\u65e0\u5e8f\u7d22\u5f15\uff0c\u7136\u540e\u6392\u5e8f\u65e0\u5e8f\u7684\u90e8\u5206\n            int j = i;\n            while (j &gt; 0 &amp;&amp; arr&#91;j] &lt; arr&#91;j - 1]) {\/\/\u5982\u679c\u540e\u4e00\u4e2a\u6bd4\u524d\u4e00\u4e2a\u5927\uff0c\u90a3\u4e48\u5c31\u4ea4\u6362\u4f4d\u7f6e\n                int temp = arr&#91;j];\n                arr&#91;j] = arr&#91;j - 1];\n                arr&#91;j - 1] = temp;\n                j--;\n            }\n        }\n\n        for (int i = 0; i &lt; arr.length; i++) {\n            System.out.print(arr&#91;i] + \" \");\n        }\n\n\n    }\n\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u56db.\u5feb\u901f\u6392\u5e8f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5feb\u901f\u6392\u5e8f\u7528\u4e86<a href=\"https:\/\/blog.csdn.net\/weixin_44572229\/article\/details\/119909728\">\u9012\u5f52<\/a><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u9009\u62e9\u57fa\u51c6\u503c\uff08Pivot\uff09<\/strong>\uff1a\u4ece\u6570\u7ec4\u4e2d\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u503c\uff0c\u901a\u5e38\u9009\u62e9\u7b2c\u4e00\u4e2a\u5143\u7d20\u6216\u6700\u540e\u4e00\u4e2a\u5143\u7d20<\/li>\n\n\n\n<li><strong>\u5206\u533a\uff08Partition\uff09<\/strong>\uff1a\u91cd\u65b0\u6392\u5217\u6570\u7ec4\uff0c\u4f7f\u5f97\u6bd4\u57fa\u51c6\u503c\u5c0f\u7684\u5143\u7d20\u653e\u5728\u57fa\u51c6\u503c\u5de6\u8fb9\uff0c\u6bd4\u57fa\u51c6\u503c\u5927\u7684\u5143\u7d20\u653e\u5728\u57fa\u51c6\u503c\u53f3\u8fb9<\/li>\n\n\n\n<li><strong>\u9012\u5f52\u6392\u5e8f<\/strong>\uff1a\u5bf9\u57fa\u51c6\u503c\u5de6\u53f3\u4e24\u4e2a\u5b50\u6570\u7ec4\u5206\u522b\u8fdb\u884c\u5feb\u901f\u6392\u5e8f<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>public class QuickSort {\n    public static void main(String&#91;] args) {\n        int&#91;] arr = {6,1,2,7,9,3,4,5,10,8};\n        quickSort(arr, 0, arr.length - 1);\n\n        for(int i = 0; i &lt; arr.length; i++) {\n            System.out.print(arr&#91;i] + \" \");\n        }\n\n    }\n\n    public static void quickSort(int&#91;] arr, int i, int j) {\n        int start = i;\n        int end = j;\n        \n\n        if (start &gt; end) {\n            return;\n        }\n\n        int baseNum = arr&#91;i];\/\/\u8981\u5148\u7ecf\u8fc7\u4e0a\u9762\u7684if\u5224\u65ad\uff0c\u5982\u679c\u5df2\u7ecf\u6392\u5e8f\u5b8c\uff0c\u4f20\u8fc7\u6765\u7684i\u662f10\uff0c\u90a3\u4e48\u5c31\u4f1a\u628a\u7d22\u5f1510\u7684\u5143\u7d20\u7ed9baseNum\u4e86\uff0c\u7136\u9e45\u5e76\u4e0d\u5b58\u572810\u7d22\u5f15\uff0c\u5e02\u8bae\u4f1a\u62a5\u9519\n\n        while (start != end) {\n            while(true) {\n                if(end &lt;= start || arr&#91;end] &lt; baseNum) {\n                    break;\n                }\n                end--;\n            }\n            while(true) {\n                if(start &gt;= end || arr&#91;start] &gt; baseNum) {\n                    break;\n                }\n                start++;\n            }\n\n            int temp = arr&#91;start];\n            arr&#91;start] = arr&#91;end];\n            arr&#91;end] = temp;\n        }\n        int temp = arr&#91;i];\n        arr&#91;i] = arr&#91;start];\n        arr&#91;start] = temp;\n\n        quickSort(arr, i, start - 1);\n        quickSort(arr, start + 1, j);\n\n    }\n    \n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u7684\u535a\u5ba2\u5373\u5c06\u540c\u6b65\u81f3\u817e\u8baf\u4e91\u5f00\u53d1\u8005\u793e\u533a\uff0c\u9080\u8bf7\u5927\u5bb6\u4e00\u540c\u5165\u9a7b\uff1a<a href=\"https:\/\/cloud.tencent.com.cn\/developer\/support-plan?invite_code=1r244lfzquqoz\">https:\/\/cloud.tencent.com.cn\/developer\/support-plan?invite_code=1r244lfzquqoz<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e8c.\u6392\u5e8f \u4e00.\u5192\u6ce1\u6392\u5e8f \u4e8c.\u9009\u62e9\u6392\u5e8f \u4e0e\u5192\u6ce1\u6392\u5e8f\u6709\u4e9b\u7c7b\u4f3c\uff0c\u4f46\u662f\u8fd8\u662f\u6709\u4e0d\u540c\uff0c\u4ee5\u4e0b\u5217\u4e3e\u90e8\u5206\u4e0d\u540c(\u901a\u4e49\u8f85\u52a9) 1.&#038; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,2],"tags":[],"class_list":["post-185","post","type-post","status-publish","format-standard","hentry","category-java","category-2"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=\/wp\/v2\/posts\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=185"}],"version-history":[{"count":2,"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=\/wp\/v2\/posts\/185\/revisions"}],"predecessor-version":[{"id":250,"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=\/wp\/v2\/posts\/185\/revisions\/250"}],"wp:attachment":[{"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}