{"id":251315,"date":"2022-09-05T10:59:20","date_gmt":"2022-09-05T02:59:20","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=251315"},"modified":"2022-08-29T09:00:00","modified_gmt":"2022-08-29T01:00:00","slug":"go-linux-insertion","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/go-linux-insertion.html","title":{"rendered":"Go\u8bed\u8a00\u6570\u636e\u7ed3\u6784\u4e4b\u63d2\u5165\u6392\u5e8f\u793a\u4f8b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u4e86Go\u8bed\u8a00\u6570\u636e\u7ed3\u6784\u4e4b\u63d2\u5165\u6392\u5e8f\u793a\u4f8b\u8be6\u89e3\uff0c\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u501f\u9274\u53c2\u8003\u4e0b\uff0c\u5e0c\u671b\u80fd\u591f\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u5927\u5bb6\u591a\u591a\u8fdb\u6b65\uff0c\u65e9\u65e5\u5347\u804c\u52a0\u85aa<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u63d2\u5165\u6392\u5e8f<\/strong><\/div>\n

\u63d2\u5165\u6392\u5e8f\uff0c\u82f1\u6587\u540d\uff08insertion sort\uff09\u662f\u4e00\u79cd\u7b80\u5355\u4e14\u6709\u6548\u7684\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\u3002<\/p>\n

\u601d\u60f3\uff1a \u5728\u6bcf\u6b21\u8fed\u4ee3\u8fc7\u7a0b\u4e2d\u7b97\u6cd5\u968f\u673a\u5730\u4ece\u8f93\u5165\u5e8f\u5217\u4e2d\u79fb\u9664\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u6539\u5143\u7d20\u63d2\u5165\u5f85\u6392\u5e8f\u5e8f\u5217\u7684\u6b63\u786e\u4f4d\u7f6e\u3002\u91cd\u590d\u8be5\u8fc7\u7a0b\uff0c\u76f4\u5230\u6240\u6709\u8f93\u5165\u5143\u7d20\u90fd\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u6392\u5e8f\u7ed3\u675f\u3002<\/p>\n

\u63d2\u5165\u6392\u5e8f\u6709\u70b9\u50cf\u5c0f\u65f6\u5019\u6211\u4eec\u6293\u6251\u514b\u724c\u7684\u65b9\u5f0f\uff0c\u5982\u679c\u6293\u8d77\u4e00\u5f20\u724c\uff0c\u6211\u4eec\u653e\u5728\u624b\u91cc\uff1b\u6293\u8d77\u7b2c\u4e8c\u5f20\u7684\u65f6\u5019\uff0c\u4f1a\u8ddf\u624b\u91cc\u7684\u7b2c\u4e00\u5f20\u724c\u8fdb\u884c\u6bd4\u8f83\uff0c\u6bd4\u624b\u91cc\u7684\u7b2c\u4e00\u5f20\u724c\u5c0f\u653e\u5728\u5de6\u8fb9\uff0c\u5426\u5219\uff0c\u653e\u5728\u53f3\u8fb9\u3002<\/p>\n

\u56e0\u6b64\uff0c\u5bf9\u6240\u6709\u7684\u724c\u91cd\u590d\u8fd9\u6837\u7684\u64cd\u4f5c\uff0c\u6240\u4ee5\u6bcf\u4e00\u6b21\u90fd\u662f\u63d2\u5165\u6700\u6b63\u786e\u7684\u6392\u5e8f\u987a\u5e8f\uff0c\u76f4\u5230\u724c\u6293\u5b8c\u4e3a\u6b62\u3002<\/p>\n

\"\"<\/p>\n

\u52a8\u753b\u6f14\u793a<\/strong><\/div>\n

\u5047\u8bbe\u6211\u4eec\u9700\u8981\u4ece\u5c0f\u5230\u5927\u8fdb\u884c\u6392\u5e8f\uff0c\u52a8\u753b\u6f14\u793a\u5982\u4e0b\uff1a<\/p>\n

\"\"<\/p>\n

Go \u4ee3\u7801\u5b9e\u73b0<\/strong><\/div>\n
package main\r\nimport \"fmt\"\r\nfunc main() {\r\n    arrays := []int{6, 2, 5, 8, 9, 3, 1}\r\n    length := len(arrays)\r\n    insertionSort(arrays, length)\r\n    for i := 0; i < length; i++ {\r\n        fmt.Printf(\"%d \", arrays[i])\r\n    }\r\n}\r\nfunc insertionSort(unsorted []int, length int) {\r\n    for i := 0; i < length; i++ {\r\n        var insertElement = unsorted[i]\r\n        var insertPosition = i\r\n        for j := insertPosition - 1; j >= 0; j-- {\r\n            if insertElement < unsorted[j] {\r\n                unsorted[j+1] = unsorted[j]\r\n                insertPosition--\r\n            }\r\n        }\r\n        unsorted[insertPosition] = insertElement\r\n    }\r\n}<\/pre>\n

\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n

[Running] go run \"e:\\Coding Workspaces\\LearningGoTheEasiestWay\\Go \u6570\u636e\u7ed3\u6784\\main.go\"\r\n1 2 3 5 6 8 9<\/pre>\n
\u603b\u7ed3<\/strong><\/div>\n

\u63d2\u5165\u6392\u5e8f\u5b9e\u73b0\u7b80\u5355\uff0c\u7406\u89e3\u4e5f\u8f83\u5bb9\u6613\uff0c\u6570\u636e\u91cf\u8f83\u5c11\u65f6\u6548\u7387\u9ad8\uff0c\u7b97\u6cd5\u7684\u5b9e\u9645\u8fd0\u884c\u6548\u7387\u4f18\u4e8e\u9009\u62e9\u6392\u5e8f\u548c\u5192\u6ce1\u6392\u5e8f\u3002<\/p>\n

\"\"<\/p>\n

\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a \u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1)\uff0c\u5373\u4e0d\u8f93\u5165\u501f\u52a9\u5176\u4ed6\u7684\u8f85\u52a9\u7a7a\u95f4\u3002<\/p>\n

\u7a33\u5b9a\u6027\uff1a \u63d2\u5165\u6392\u5e8f\u662f\u7a33\u5b9a\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5728\u952e\u503c\u76f8\u540c\u65f6\u5b83\u80fd\u591f\u4fdd\u6301\u8f93\u5165\u6570\u636e\u7684\u539f\u6709\u6b21\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u63d2\u5165\u6392\u5e8f\uff0c\u82f1\u6587\u540d\uff08insertion sort\uff09\u662f\u4e00\u79cd\u7b80\u5355\u4e14\u6709\u6548\u7684\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\u3002 \u601d\u60f3\uff1a \u5728\u6bcf\u6b21\u8fed\u4ee3\u8fc7\u7a0b\u4e2d\u7b97\u6cd5 […]<\/p>\n","protected":false},"author":668,"featured_media":222173,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-251315","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=251315"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251315\/revisions"}],"predecessor-version":[{"id":251321,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251315\/revisions\/251321"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/222173"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=251315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=251315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=251315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}