{"id":211522,"date":"2021-02-23T10:00:37","date_gmt":"2021-02-23T02:00:37","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=211522"},"modified":"2021-02-17T16:02:30","modified_gmt":"2021-02-17T08:02:30","slug":"is-count-sort","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/is-count-sort.html","title":{"rendered":"\u4ec0\u4e48\u662f\u8ba1\u6570\u6392\u5e8f\uff1f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8ba1\u6570\u6392\u5e8f\u7684\u6838\u5fc3\u5728\u4e8e\u5c06\u8f93\u5165\u7684\u6570\u636e\u503c\u8f6c\u5316\u4e3a\u952e\u5b58\u50a8\u5728\u989d\u5916\u5f00\u8f9f\u7684\u6570\u7ec4\u7a7a\u95f4\u4e2d\u3002\u4f5c\u4e3a\u4e00\u79cd\u7ebf\u6027\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u6392\u5e8f\uff0c\u8ba1\u6570\u6392\u5e8f\u8981\u6c42\u8f93\u5165\u7684\u6570\u636e\u5fc5\u987b\u662f\u6709\u786e\u5b9a\u8303\u56f4\u7684\u6574\u6570\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
1. \u8ba1\u6570\u6392\u5e8f\u7684\u7279\u5f81<\/strong><\/div>\n

\u5f53\u8f93\u5165\u7684\u5143\u7d20\u662f n \u4e2a 0 \u5230 k \u4e4b\u95f4\u7684\u6574\u6570\u65f6\uff0c\u5b83\u7684\u8fd0\u884c\u65f6\u95f4\u662f \u0398(n + k)\u3002\u8ba1\u6570\u6392\u5e8f\u4e0d\u662f\u6bd4\u8f83\u6392\u5e8f\uff0c\u6392\u5e8f\u7684\u901f\u5ea6\u5feb\u4e8e\u4efb\u4f55\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\u3002<\/p>\n

\u7531\u4e8e\u7528\u6765\u8ba1\u6570\u7684\u6570\u7ec4C\u7684\u957f\u5ea6\u53d6\u51b3\u4e8e\u5f85\u6392\u5e8f\u6570\u7ec4\u4e2d\u6570\u636e\u7684\u8303\u56f4\uff08\u7b49\u4e8e\u5f85\u6392\u5e8f\u6570\u7ec4\u7684\u6700\u5927\u503c\u4e0e\u6700\u5c0f\u503c\u7684\u5dee\u52a0\u4e0a1\uff09\uff0c\u8fd9\u4f7f\u5f97\u8ba1\u6570\u6392\u5e8f\u5bf9\u4e8e\u6570\u636e\u8303\u56f4\u5f88\u5927\u7684\u6570\u7ec4\uff0c\u9700\u8981\u5927\u91cf\u65f6\u95f4\u548c\u5185\u5b58\u3002\u4f8b\u5982\uff1a\u8ba1\u6570\u6392\u5e8f\u662f\u7528\u6765\u6392\u5e8f0\u5230100\u4e4b\u95f4\u7684\u6570\u5b57\u7684\u6700\u597d\u7684\u7b97\u6cd5\uff0c\u4f46\u662f\u5b83\u4e0d\u9002\u5408\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\u4eba\u540d\u3002\u4f46\u662f\uff0c\u8ba1\u6570\u6392\u5e8f\u53ef\u4ee5\u7528\u5728\u57fa\u6570\u6392\u5e8f\u4e2d\u7684\u7b97\u6cd5\u6765\u6392\u5e8f\u6570\u636e\u8303\u56f4\u5f88\u5927\u7684\u6570\u7ec4\u3002<\/p>\n

\u901a\u4fd7\u5730\u7406\u89e3\uff0c\u4f8b\u5982\u6709 10 \u4e2a\u5e74\u9f84\u4e0d\u540c\u7684\u4eba\uff0c\u7edf\u8ba1\u51fa\u6709 8 \u4e2a\u4eba\u7684\u5e74\u9f84\u6bd4 A \u5c0f\uff0c\u90a3 A \u7684\u5e74\u9f84\u5c31\u6392\u5728\u7b2c 9 \u4f4d,\u7528\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5f97\u5230\u5176\u4ed6\u6bcf\u4e2a\u4eba\u7684\u4f4d\u7f6e,\u4e5f\u5c31\u6392\u597d\u4e86\u5e8f\u3002\u5f53\u7136\uff0c\u5e74\u9f84\u6709\u91cd\u590d\u65f6\u9700\u8981\u7279\u6b8a\u5904\u7406\uff08\u4fdd\u8bc1\u7a33\u5b9a\u6027\uff09\uff0c\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u6700\u540e\u8981\u53cd\u5411\u586b\u5145\u76ee\u6807\u6570\u7ec4\uff0c\u4ee5\u53ca\u5c06\u6bcf\u4e2a\u6570\u5b57\u7684\u7edf\u8ba1\u51cf\u53bb 1 \u7684\u539f\u56e0\u3002<\/p>\n

\u7b97\u6cd5\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n

    \n
  1. \u627e\u51fa\u5f85\u6392\u5e8f\u7684\u6570\u7ec4\u4e2d\u6700\u5927\u548c\u6700\u5c0f\u7684\u5143\u7d20<\/li>\n
  2. \u7edf\u8ba1\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u503c\u4e3ai\u7684\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u5b58\u5165\u6570\u7ec4C\u7684\u7b2ci\u9879<\/li>\n
  3. \u5bf9\u6240\u6709\u7684\u8ba1\u6570\u7d2f\u52a0\uff08\u4eceC\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u6bcf\u4e00\u9879\u548c\u524d\u4e00\u9879\u76f8\u52a0\uff09<\/li>\n
  4. \u53cd\u5411\u586b\u5145\u76ee\u6807\u6570\u7ec4\uff1a\u5c06\u6bcf\u4e2a\u5143\u7d20i\u653e\u5728\u65b0\u6570\u7ec4\u7684\u7b2cC(i)\u9879\uff0c\u6bcf\u653e\u4e00\u4e2a\u5143\u7d20\u5c31\u5c06C(i)\u51cf\u53bb1<\/li>\n<\/ol>\n
    2. \u52a8\u56fe\u6f14\u793a<\/strong><\/div>\n

    \u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n

    JavaScript<\/strong><\/span><\/div>\n

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

    function countingSort(arr, maxValue) {\r\n    var bucket = new Array(maxValue+1),\r\n        sortedIndex = 0;\r\n        arrLen = arr.length,\r\n        bucketLen = maxValue + 1;\r\n\r\n    for (var i = 0; i < arrLen; i++) {\r\n        if (!bucket[arr[i]]) {\r\n            bucket[arr[i]] = 0;\r\n        }\r\n        bucket[arr[i]]++;\r\n    }\r\n\r\n    for (var j = 0; j < bucketLen; j++) {\r\n        while(bucket[j] > 0) {\r\n            arr[sortedIndex++] = j;\r\n            bucket[j]--;\r\n        }\r\n    }\r\n\r\n    return arr;\r\n}<\/pre>\n
    Python<\/strong><\/span><\/div>\n

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

    def countingSort(arr, maxValue):\r\n    bucketLen = maxValue+1\r\n    bucket = [0]*bucketLen\r\n    sortedIndex =0\r\n    arrLen = len(arr)\r\n    for i in range(arrLen):\r\n        if not bucket[arr[i]]:\r\n            bucket[arr[i]]=0\r\n        bucket[arr[i]]+=1\r\n    for j in range(bucketLen):\r\n        while bucket[j]>0:\r\n            arr[sortedIndex] = j\r\n            sortedIndex+=1\r\n            bucket[j]-=1\r\n    return arr<\/pre>\n
    Go<\/strong><\/span><\/div>\n

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

    func countingSort(arr []int, maxValue int) []int {\r\n        bucketLen := maxValue + 1\r\n        bucket := make([]int, bucketLen) \/\/ \u521d\u59cb\u4e3a0\u7684\u6570\u7ec4\r\n\r\n        sortedIndex := 0\r\n        length := len(arr)\r\n\r\n        for i := 0; i < length; i++ {\r\n                bucket[arr[i]] += 1\r\n        }\r\n\r\n        for j := 0; j < bucketLen; j++ {\r\n                for bucket[j] > 0 {\r\n                        arr[sortedIndex] = j\r\n                        sortedIndex += 1\r\n                        bucket[j] -= 1\r\n                }\r\n        }\r\n\r\n        return arr\r\n}<\/pre>\n
    Java<\/strong><\/span><\/div>\n

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

    public class CountingSort 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        int maxValue = getMaxValue(arr);\r\n\r\n        return countingSort(arr, maxValue);\r\n    }\r\n\r\n    private int[] countingSort(int[] arr, int maxValue) {\r\n        int bucketLen = maxValue + 1;\r\n        int[] bucket = new int[bucketLen];\r\n\r\n        for (int value : arr) {\r\n            bucket[value]++;\r\n        }\r\n\r\n        int sortedIndex = 0;\r\n        for (int j = 0; j < bucketLen; j++) {\r\n            while (bucket[j] > 0) {\r\n                arr[sortedIndex++] = j;\r\n                bucket[j]--;\r\n            }\r\n        }\r\n        return arr;\r\n    }\r\n\r\n    private int getMaxValue(int[] arr) {\r\n        int maxValue = arr[0];\r\n        for (int value : arr) {\r\n            if (maxValue < value) {\r\n                maxValue = value;\r\n            }\r\n        }\r\n        return maxValue;\r\n    }\r\n\r\n}<\/pre>\n
    PHP<\/strong><\/span><\/div>\n

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

    function countingSort($arr, $maxValue = null)\r\n{\r\n    if ($maxValue === null) {\r\n        $maxValue = max($arr);\r\n    }\r\n    for ($m = 0; $m < $maxValue + 1; $m++) {\r\n        $bucket[] = null;\r\n    }\r\n\r\n    $arrLen = count($arr);\r\n    for ($i = 0; $i < $arrLen; $i++) {\r\n        if (!array_key_exists($arr[$i], $bucket)) {\r\n            $bucket[$arr[$i]] = 0;\r\n        }\r\n        $bucket[$arr[$i]]++;\r\n    }\r\n\r\n    $sortedIndex = 0;\r\n    foreach ($bucket as $key => $len) {\r\n        if ($len !== null) $arr[$sortedIndex++] = $key;\r\n        if($len !== null){\r\n            for($j = 0; $j < $len; $j++){\r\n                $arr[$sortedIndex++] = $key;\r\n            }\r\n        }\r\n    }\r\n\r\n    return $arr;\r\n}<\/pre>\n
    C<\/strong><\/span><\/div>\n

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

    #include \r\n#include \r\n#include \r\n\r\nvoid print_arr(int *arr, int n) {\r\n        int i;\r\n        printf(\"%d\", arr[0]);\r\n        for (i = 1; i < n; i++)\r\n                printf(\" %d\", arr[i]);\r\n        printf(\"\\n\");\r\n}\r\n\r\nvoid counting_sort(int *ini_arr, int *sorted_arr, int n) {\r\n        int *count_arr = (int *) malloc(sizeof(int) * 100);\r\n        int i, j, k;\r\n        for (k = 0; k < 100; k++)\r\n                count_arr[k] = 0;\r\n        for (i = 0; i < n; i++)\r\n                count_arr[ini_arr[i]]++;\r\n        for (k = 1; k < 100; k++)\r\n                count_arr[k] += count_arr[k - 1];\r\n        for (j = n; j > 0; j--)\r\n                sorted_arr[--count_arr[ini_arr[j - 1]]] = ini_arr[j - 1];\r\n        free(count_arr);\r\n}\r\n\r\nint main(int argc, char **argv) {\r\n        int n = 10;\r\n        int i;\r\n        int *arr = (int *) malloc(sizeof(int) * n);\r\n        int *sorted_arr = (int *) malloc(sizeof(int) * n);\r\n        srand(time(0));\r\n        for (i = 0; i < n; i++)\r\n                arr[i] = rand() % 100;\r\n        printf(\"ini_array: \");\r\n        print_arr(arr, n);\r\n        counting_sort(arr, sorted_arr, n);\r\n        printf(\"sorted_array: \");\r\n        print_arr(sorted_arr, n);\r\n        free(arr);\r\n        free(sorted_arr);\r\n        return 0;\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"

    \u5f53\u8f93\u5165\u7684\u5143\u7d20\u662f n \u4e2a 0 \u5230 k \u4e4b\u95f4\u7684\u6574\u6570\u65f6\uff0c\u5b83\u7684\u8fd0\u884c\u65f6\u95f4\u662f \u0398(n + k)\u3002\u8ba1\u6570\u6392\u5e8f\u4e0d\u662f\u6bd4\u8f83\u6392\u5e8f\uff0c\u6392\u5e8f […]<\/p>\n","protected":false},"author":321,"featured_media":211524,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-211522","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\/211522","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\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=211522"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/211522\/revisions"}],"predecessor-version":[{"id":211726,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/211522\/revisions\/211726"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/211524"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=211522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=211522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=211522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}