{"id":3622,"date":"2023-01-19T16:31:44","date_gmt":"2023-01-19T08:31:44","guid":{"rendered":"https:\/\/qaqaq.top\/?p=3622"},"modified":"2023-01-19T16:31:45","modified_gmt":"2023-01-19T08:31:45","slug":"%e5%86%92%e6%b3%a1%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=3622","title":{"rendered":"\u5192\u6ce1\u6392\u5e8f"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package chapter4.part2;\r\n\r\nimport java.util.Arrays;\r\n\r\npublic class BubbleSort {\r\n\r\n    public static void sort(int array&#91;])\r\n    {\r\n        int tmp  = 0;\r\n        \/\/\u8bb0\u5f55\u6700\u540e\u4e00\u6b21\u4ea4\u6362\u7684\u4f4d\u7f6e\r\n        int lastExchangeIndex = 0;\r\n        \/\/\u65e0\u5e8f\u6570\u5217\u7684\u8fb9\u754c\uff0c\u6bcf\u6b21\u6bd4\u8f83\u53ea\u9700\u8981\u6bd4\u5230\u8fd9\u91cc\u4e3a\u6b62\r\n        int sortBorder = array.length - 1;\r\n        for(int i = 0; i &lt; array.length; i++)\r\n        {\r\n            \/\/\u6709\u5e8f\u6807\u8bb0\uff0c\u6bcf\u4e00\u8f6e\u7684\u521d\u59cb\u662ftrue\r\n            boolean isSorted = true;\r\n\r\n            for(int j = 0; j &lt; sortBorder; j++)\r\n            {\r\n                if(array&#91;j] > array&#91;j+1])\r\n                {\r\n                    tmp = array&#91;j];\r\n                    array&#91;j] = array&#91;j+1];\r\n                    array&#91;j+1] = tmp;\r\n                    \/\/\u6709\u5143\u7d20\u4ea4\u6362\uff0c\u6240\u4ee5\u4e0d\u662f\u6709\u5e8f\uff0c\u6807\u8bb0\u53d8\u4e3afalse\r\n                    isSorted = false;\r\n                    \/\/\u628a\u65e0\u5e8f\u6570\u5217\u7684\u8fb9\u754c\u66f4\u65b0\u4e3a\u6700\u540e\u4e00\u6b21\u4ea4\u6362\u5143\u7d20\u7684\u4f4d\u7f6e\r\n                    lastExchangeIndex = j;\r\n                }\r\n            }\r\n            sortBorder = lastExchangeIndex;\r\n            if(isSorted){\r\n                break;\r\n            }\r\n        }\r\n    }\r\n\r\n    public static void main(String&#91;] args){\r\n        int&#91;] array = new int&#91;]{3,4,2,1,5,6,7,8};\r\n        sort(array);\r\n        System.out.println(Arrays.toString(array));\r\n    }\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[133],"class_list":["post-3622","post","type-post","status-publish","format-standard","hentry","category-82","tag-133"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/3622"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3622"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/3622\/revisions"}],"predecessor-version":[{"id":3623,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/3622\/revisions\/3623"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}