{"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":"
\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 |