{"id":211575,"date":"2021-03-01T09:26:24","date_gmt":"2021-03-01T01:26:24","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=211575"},"modified":"2021-02-23T16:29:06","modified_gmt":"2021-02-23T08:29:06","slug":"to-insertion-sort","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/to-insertion-sort.html","title":{"rendered":"\u63d2\u5165\u6392\u5e8f\u7b80\u4ecb"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u63d2\u5165\u6392\u5e8f\u662f\u4e00\u79cd\u6700\u7b80\u5355\u76f4\u89c2\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u662f\u901a\u8fc7\u6784\u5efa\u6709\u5e8f\u5e8f\u5217\uff0c\u5bf9\u4e8e\u672a\u6392\u5e8f\u6570\u636e\uff0c\u5728\u5df2\u6392\u5e8f\u5e8f\u5217\u4e2d\u4ece\u540e\u5411\u524d\u626b\u63cf\uff0c\u627e\u5230\u76f8\u5e94\u4f4d\u7f6e\u5e76\u63d2\u5165\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u63d2\u5165\u6392\u5e8f\u548c\u5192\u6ce1\u6392\u5e8f\u4e00\u6837\uff0c\u4e5f\u6709\u4e00\u79cd\u4f18\u5316\u7b97\u6cd5\uff0c\u53eb\u505a\u62c6\u534a\u63d2\u5165\u3002<\/p>\n

\u7b97\u6cd5\u6b65\u9aa4<\/strong><\/div>\n

\u5c06\u7b2c\u4e00\u5f85\u6392\u5e8f\u5e8f\u5217\u7b2c\u4e00\u4e2a\u5143\u7d20\u770b\u505a\u4e00\u4e2a\u6709\u5e8f\u5e8f\u5217\uff0c\u628a\u7b2c\u4e8c\u4e2a\u5143\u7d20\u5230\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u5f53\u6210\u662f\u672a\u6392\u5e8f\u5e8f\u5217\u3002<\/p>\n

\u4ece\u5934\u5230\u5c3e\u4f9d\u6b21\u626b\u63cf\u672a\u6392\u5e8f\u5e8f\u5217\uff0c\u5c06\u626b\u63cf\u5230\u7684\u6bcf\u4e2a\u5143\u7d20\u63d2\u5165\u6709\u5e8f\u5e8f\u5217\u7684\u9002\u5f53\u4f4d\u7f6e\u3002\uff08\u5982\u679c\u5f85\u63d2\u5165\u7684\u5143\u7d20\u4e0e\u6709\u5e8f\u5e8f\u5217\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u76f8\u7b49\uff0c\u5219\u5c06\u5f85\u63d2\u5165\u5143\u7d20\u63d2\u5165\u5230\u76f8\u7b49\u5143\u7d20\u7684\u540e\u9762\u3002\uff09<\/p>\n

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

\"\"<\/p>\n

\u4ee3\u7801\u5b9e\u73b0<\/strong><\/div>\n
JavaScript<\/strong><\/span><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

function insertionSort(arr) {\r\n    var len = arr.length;\r\n    var preIndex, current;\r\n    for (var i = 1; i < len; i++) {\r\n        preIndex = i - 1;\r\n        current = arr[i];\r\n        while(preIndex >= 0 && arr[preIndex] > current) {\r\n            arr[preIndex+1] = arr[preIndex];\r\n            preIndex--;\r\n        }\r\n        arr[preIndex+1] = current;\r\n    }\r\n    return arr;\r\n}<\/pre>\n
Python<\/strong><\/span><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

def insertionSort(arr):\r\n    for i in range(len(arr)):\r\n        preIndex = i-1\r\n        current = arr[i]\r\n        while preIndex >= 0 and arr[preIndex] > current:\r\n            arr[preIndex+1] = arr[preIndex]\r\n            preIndex-=1\r\n        arr[preIndex+1] = current\r\n    return arr<\/pre>\n
Go<\/strong><\/span><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

func insertionSort(arr []int) []int {\r\n        for i := range arr {\r\n                preIndex := i - 1\r\n                current := arr[i]\r\n                for preIndex >= 0 && arr[preIndex] > current {\r\n                        arr[preIndex+1] = arr[preIndex]\r\n                        preIndex -= 1\r\n                }\r\n                arr[preIndex+1] = current\r\n        }\r\n        return arr\r\n}<\/pre>\n
Java<\/strong><\/span><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

public class InsertSort implements IArraySort {\r\n\r\n    @Override\r\n    public int[] sort(int[] sourceArray) throws Exception {\r\n        \/\/ \u5bf9 arr \u8fdb\u884c\u62f7\u8d1d\uff0c\u4e0d\u6539\u53d8\u53c2\u6570\u5185\u5bb9\r\n        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);\r\n\r\n        \/\/ \u4ece\u4e0b\u6807\u4e3a1\u7684\u5143\u7d20\u5f00\u59cb\u9009\u62e9\u5408\u9002\u7684\u4f4d\u7f6e\u63d2\u5165\uff0c\u56e0\u4e3a\u4e0b\u6807\u4e3a0\u7684\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\uff0c\u9ed8\u8ba4\u662f\u6709\u5e8f\u7684\r\n        for (int i = 1; i < arr.length; i++) {\r\n\r\n            \/\/ \u8bb0\u5f55\u8981\u63d2\u5165\u7684\u6570\u636e\r\n            int tmp = arr[i];\r\n\r\n            \/\/ \u4ece\u5df2\u7ecf\u6392\u5e8f\u7684\u5e8f\u5217\u6700\u53f3\u8fb9\u7684\u5f00\u59cb\u6bd4\u8f83\uff0c\u627e\u5230\u6bd4\u5176\u5c0f\u7684\u6570\r\n            int j = i;\r\n            while (j > 0 && tmp < arr[j - 1]) {\r\n                arr[j] = arr[j - 1];\r\n                j--;\r\n            }\r\n\r\n            \/\/ \u5b58\u5728\u6bd4\u5176\u5c0f\u7684\u6570\uff0c\u63d2\u5165\r\n            if (j != i) {\r\n                arr[j] = tmp;\r\n            }\r\n\r\n        }\r\n        return arr;\r\n    }\r\n}<\/pre>\n
PHP<\/strong><\/span><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

function insertionSort($arr)\r\n{\r\n    $len = count($arr);\r\n    for ($i = 1; $i < $len; $i++) {\r\n        $preIndex = $i - 1;\r\n        $current = $arr[$i];\r\n        while($preIndex >= 0 && $arr[$preIndex] > $current) {\r\n            $arr[$preIndex+1] = $arr[$preIndex];\r\n            $preIndex--;\r\n        }\r\n        $arr[$preIndex+1] = $current;\r\n    }\r\n    return $arr;\r\n}<\/pre>\n
C<\/strong><\/span><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

void insertion_sort(int arr[], int len){\r\n        int i,j,key;\r\n        for (i=1;i=0) && (arr[j]>key)) {\r\n                        arr[j+1] = arr[j];\r\n                        j--;\r\n                }\r\n                arr[j+1] = key;\r\n        }\r\n}<\/pre>\n
C++<\/strong><\/span><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

\r\nvoid<\/span> insertion_sort(<\/span>int<\/span> arr[<\/span>]<\/span>,int<\/span> len)<\/span>{<\/span>
\r\n        for<\/span>(<\/span>int<\/span> i=<\/span>1<\/span>;<\/span>i<<\/span>len;<\/span>i++<\/span>)<\/span>{<\/span>
\r\n                int<\/span> key=<\/span>arr[<\/span>i]<\/span>;<\/span>
\r\n                int<\/span> j=<\/span>i-<\/span>1<\/span>;<\/span>
\r\n                while<\/span>(<\/span>(<\/span>j>=<\/span>0<\/span>)<\/span> &&<\/span> (<\/span>key<<\/span>arr[<\/span>j]<\/span>)<\/span>)<\/span>{<\/span>
\r\n                        arr[<\/span>j+<\/span>1<\/span>]<\/span>=<\/span>arr[<\/span>j]<\/span>;<\/span>
\r\n                        j--<\/span>;<\/span>
\r\n                }<\/span>
\r\n                arr[<\/span>j+<\/span>1<\/span>]<\/span>=<\/span>key;<\/span>
\r\n        }<\/span>
\r\n}<\/span>
\r\n<\/div><\/pre>\n
C#<\/strong><\/span><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

public static void InsertSort(int[] array)\r\n{\r\n    for(int i = 1;i < array.length;i++)\r\n    {\r\n        int temp = array[i];\r\n        for(int j = i - 1;j >= 0;j--)\r\n        {\r\n            if(array[j] > temp)\r\n            {\r\n                array[j + 1] = array[j];\r\n                array[j] = temp;\r\n            }\r\n            else\r\n                break;\r\n        }\r\n    }\r\n}<\/pre>\n
Swift<\/strong><\/span><\/div>\n

\u5b9e\u4f8b<\/strong><\/p>\n

\r\nfor<\/span> i in<\/span> 1<\/span>..<arr.endIndex {\r\n    let temp =<\/span> arr[<\/span>i]\r\n    for<\/span> j in<\/span> (<\/span>0<\/span>..<i)<\/span>.reversed(<\/span>)<\/span> {\r\n        if<\/span> arr[<\/span>j]<\/span> > temp {\r\n            arr.swapAt(<\/span>j, j+<\/span>1<\/span>)\r\n        }\r\n    }\r\n}<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/div><\/pre>\n","protected":false},"excerpt":{"rendered":"

\u63d2\u5165\u6392\u5e8f\u548c\u5192\u6ce1\u6392\u5e8f\u4e00\u6837\uff0c\u4e5f\u6709\u4e00\u79cd\u4f18\u5316\u7b97\u6cd5\uff0c\u53eb\u505a\u62c6\u534a\u63d2\u5165\u3002 \u5c06\u7b2c\u4e00\u5f85\u6392\u5e8f\u5e8f\u5217\u7b2c\u4e00\u4e2a\u5143\u7d20\u770b\u505a\u4e00\u4e2a\u6709\u5e8f\u5e8f\u5217\uff0c\u628a\u7b2c\u4e8c\u4e2a […]<\/p>\n","protected":false},"author":1470,"featured_media":211850,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-211575","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\/211575","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\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=211575"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/211575\/revisions"}],"predecessor-version":[{"id":212002,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/211575\/revisions\/212002"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/211850"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=211575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=211575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=211575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}