{"id":1814,"date":"2017-03-30T14:08:19","date_gmt":"2017-03-30T06:08:19","guid":{"rendered":"http:\/\/www.qdabc.cn\/?p=1814"},"modified":"2017-03-30T14:08:19","modified_gmt":"2017-03-30T06:08:19","slug":"javascript%e5%86%92%e6%b3%a1%e3%80%81%e6%8f%92%e5%85%a5%e3%80%81%e5%bf%ab%e9%80%9f%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"http:\/\/www.qdabc.cn\/?p=1814","title":{"rendered":"JavaScript\u5192\u6ce1\u3001\u63d2\u5165\u3001\u5feb\u901f\u6392\u5e8f"},"content":{"rendered":"<p>1.\u5192\u6ce1\u6392\u5e8f\uff0c\u6027\u80fd\u5df2\u4f18\u5316<\/p>\n<pre>function bubbleSort(ary) {\r\n        var len = ary.length, flag = null;\r\n        if (len &gt; 1) {\r\n            for (var i = 0; i &lt; len - 1; i++) {\r\n                flag = false;\/\/\u4f4d\u7f6e\u53d8\u66f4\u6807\u8bc6\u7b26,\u53ef\u4ee5\u4f18\u5316\u6027\u80fd\r\n                for (var j = 0; j &lt; len - 1 - i; j++) {\r\n                    \/\/console.log(i);\r\n                    if (ary[j] &gt; ary[j + 1]) {\r\n                        var temp = ary[j];\r\n                        ary[j] = ary[j + 1];\r\n                        ary[j + 1] = temp;\r\n                        flag = true;\/\/\u6709\u4f4d\u7f6e\u4ea4\u6362\u5219\u6539\u53d8flag\u7684\u503c\r\n                    }\r\n                }\r\n                if (!flag) {\/\/\u68c0\u6d4b\u5230\u4e0a\u4e00\u8f6e\u5df2\u7ecf\u6ca1\u6709\u8fc7\u4f4d\u7f6e\u53d8\u66f4,\u5c31\u8bf4\u660e\u6392\u5e8f\u5df2\u7ecf\u5b8c\u6210\r\n                    break;\r\n                }\r\n            }\r\n        }\r\n        return ary;\r\n    }\r\n    var ary1 = [111, 99, 12, 32, 78, 88, 34];\r\n    console.log(bubbleSort(ary1));<\/pre>\n<p>2.\u63d2\u5165\u6392\u5e8f<\/p>\n<pre>function insertSort(ary) {\r\n        var len = ary.length;\r\n        if (len &gt; 1) {\r\n            for (var i = 1; i &lt; len; i++) {\r\n                if (ary[i] &lt; ary[i - 1]) {  \/\/\u5f53\u524d\u9879\u5c0f\u4e8e\u524d\u4e00\u9879\u7684\u65f6\u5019\u624d\u5411\u524d\u63d2\u5165\r\n                    var cur = ary[i], j = i - 1;    \/\/\u4fdd\u5b58\u7b2ci\u9879\u7684\u503c,\u627e\u5230\u524d\u4e00\u9879\u7684\u7d22\u5f15\r\n                    while (j &gt;= 0 &amp;&amp; cur &lt; ary[j]) {    \/\/\u524d\u4e00\u9879\u5b58\u5728\u4e14\u7b2ci\u9879\u7684\u503c\u5c0f\u4e8e\u524d\u4e00\u9879\r\n                        ary[j + 1] = ary[j];        \/\/\u5f53\u524d\u6bd4\u8f83\u9879\u8d4b\u503c\u4e3a\u524d\u4e00\u9879\u7684\u503c\r\n                        j--;                        \/\/\u7ee7\u7eed\u5411\u524d\u67e5\u627e\r\n                    }\r\n                    ary[j + 1] = cur;           \/\/\u5c06\u7b2ci\u9879\u63d2\u5165\u5230\u5f53\u524d\u4f4d\u7f6e\r\n                }\r\n            }\r\n        }\r\n        return ary;\r\n    }\r\n    \r\n    var ary2 = [111, 99, 12, 32, 78, 88, 34];\r\n    console.log(insertSort(ary2));<\/pre>\n<p>3.\u5feb\u901f\u6392\u5e8f<\/p>\n<pre>function quickSort(ary) {\r\n        if (ary.length &lt;= 1) {\r\n            return ary;\r\n        }\r\n        var midIndex = Math.floor(ary.length \/ 2);\r\n        var minItem = ary.splice(midIndex, 1)[0];\r\n        var leftAry = [], rightAry = [];\r\n        for (var i = 0; i &lt; ary.length; i++) {\r\n            var cur = ary[i];\r\n            cur &lt; minItem ? leftAry.push(cur) : rightAry.push(cur);\r\n        }\r\n        return quickSort(leftAry).concat(minItem, quickSort(rightAry));\r\n    }\r\n    var ary3 = [111, 99, 12, 32, 78, 88, 34];\r\n    console.log(quickSort(ary3));<\/pre>\n<p class=\"post-copyright\">\u6b22\u8fce\u5206\u4eab\u672c\u6587\uff0c\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u51fa\u5904\uff1a<a href=\"http:\/\/www.qdabc.cn\">\u524d\u7aefABC<\/a> &raquo; <a href=\"http:\/\/www.qdabc.cn\/?p=1814\">JavaScript\u5192\u6ce1\u3001\u63d2\u5165\u3001\u5feb\u901f\u6392\u5e8f<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>1.\u5192\u6ce1\u6392\u5e8f\uff0c\u6027\u80fd\u5df2\u4f18\u5316 function bubbleSort(ary) { var len = ary.length, flag = null; if (len &gt; 1) { for (va [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[9,200],"_links":{"self":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/1814"}],"collection":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1814"}],"version-history":[{"count":1,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/1814\/revisions"}],"predecessor-version":[{"id":1815,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/1814\/revisions\/1815"}],"wp:attachment":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1814"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}