{"id":211507,"date":"2021-02-26T09:38:26","date_gmt":"2021-02-26T01:38:26","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=211507"},"modified":"2021-02-20T09:39:02","modified_gmt":"2021-02-20T01:39:02","slug":"understand-the-cardinality","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/understand-the-cardinality.html","title":{"rendered":"\u8ba4\u8bc6\u4e0b\u57fa\u6570\u6392\u5e8f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u57fa\u6570\u6392\u5e8f\u662f\u4e00\u79cd\u975e\u6bd4\u8f83\u578b\u6574\u6570\u6392\u5e8f\u7b97\u6cd5\uff0c\u5176\u539f\u7406\u662f\u5c06\u6574\u6570\u6309\u4f4d\u6570\u5207\u5272\u6210\u4e0d\u540c\u7684\u6570\u5b57\uff0c\u7136\u540e\u6309\u6bcf\u4e2a\u4f4d\u6570\u5206\u522b\u6bd4\u8f83\u3002\u7531\u4e8e\u6574\u6570\u4e5f\u53ef\u4ee5\u8868\u8fbe\u5b57\u7b26\u4e32\uff08\u6bd4\u5982\u540d\u5b57\u6216\u65e5\u671f\uff09\u548c\u7279\u5b9a\u683c\u5f0f\u7684\u6d6e\u70b9\u6570\uff0c\u6240\u4ee5\u57fa\u6570\u6392\u5e8f\u4e5f\u4e0d\u662f\u53ea\u80fd\u4f7f\u7528\u4e8e\u6574\u6570\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
1. \u57fa\u6570\u6392\u5e8f vs \u8ba1\u6570\u6392\u5e8f vs \u6876\u6392\u5e8f<\/strong><\/div>\n

\u57fa\u6570\u6392\u5e8f\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a<\/p>\n

\u8fd9\u4e09\u79cd\u6392\u5e8f\u7b97\u6cd5\u90fd\u5229\u7528\u4e86\u6876\u7684\u6982\u5ff5\uff0c\u4f46\u5bf9\u6876\u7684\u4f7f\u7528\u65b9\u6cd5\u4e0a\u6709\u660e\u663e\u5dee\u5f02\uff1a<\/p>\n

    \n
  1. \u57fa\u6570\u6392\u5e8f\uff1a\u6839\u636e\u952e\u503c\u7684\u6bcf\u4f4d\u6570\u5b57\u6765\u5206\u914d\u6876\uff1b<\/li>\n
  2. \u8ba1\u6570\u6392\u5e8f\uff1a\u6bcf\u4e2a\u6876\u53ea\u5b58\u50a8\u5355\u4e00\u952e\u503c\uff1b<\/li>\n
  3. \u6876\u6392\u5e8f\uff1a\u6bcf\u4e2a\u6876\u5b58\u50a8\u4e00\u5b9a\u8303\u56f4\u7684\u6570\u503c\uff1b<\/li>\n<\/ol>\n
    2. LSD \u57fa\u6570\u6392\u5e8f\u52a8\u56fe\u6f14\u793a<\/strong><\/div>\n

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

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

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

    \/\/LSD Radix Sort\r\nvar counter = [];\r\nfunction radixSort(arr, maxDigit) {\r\n    var mod = 10;\r\n    var dev = 1;\r\n    for (var i = 0; i < maxDigit; i++, dev *= 10, mod *= 10) {\r\n        for(var j = 0; j < arr.length; j++) {\r\n            var bucket = parseInt((arr[j] % mod) \/ dev);\r\n            if(counter[bucket]==null) {\r\n                counter[bucket] = [];\r\n            }\r\n            counter[bucket].push(arr[j]);\r\n        }\r\n        var pos = 0;\r\n        for(var j = 0; j < counter.length; j++) {\r\n            var value = null;\r\n            if(counter[j]!=null) {\r\n                while ((value = counter[j].shift()) != null) {\r\n                      arr[pos++] = value;\r\n                }\r\n          }\r\n        }\r\n    }\r\n    return arr;\r\n}<\/pre>\n
    Java<\/strong><\/span><\/div>\n

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

    \/**\r\n * \u57fa\u6570\u6392\u5e8f\r\n * \u8003\u8651\u8d1f\u6570\u7684\u60c5\u51b5\u8fd8\u53ef\u4ee5\u53c2\u8003\uff1a https:\/\/code.i-harness.com\/zh-CN\/q\/e98fa9\r\n *\/\r\npublic class RadixSort 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 maxDigit = getMaxDigit(arr);\r\n        return radixSort(arr, maxDigit);\r\n    }\r\n\r\n    \/**\r\n     * \u83b7\u53d6\u6700\u9ad8\u4f4d\u6570\r\n     *\/\r\n    private int getMaxDigit(int[] arr) {\r\n        int maxValue = getMaxValue(arr);\r\n        return getNumLenght(maxValue);\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    protected int getNumLenght(long num) {\r\n        if (num == 0) {\r\n            return 1;\r\n        }\r\n        int lenght = 0;\r\n        for (long temp = num; temp != 0; temp \/= 10) {\r\n            lenght++;\r\n        }\r\n        return lenght;\r\n    }\r\n\r\n    private int[] radixSort(int[] arr, int maxDigit) {\r\n        int mod = 10;\r\n        int dev = 1;\r\n\r\n        for (int i = 0; i < maxDigit; i++, dev *= 10, mod *= 10) {\r\n            \/\/ \u8003\u8651\u8d1f\u6570\u7684\u60c5\u51b5\uff0c\u8fd9\u91cc\u6269\u5c55\u4e00\u500d\u961f\u5217\u6570\uff0c\u5176\u4e2d [0-9]\u5bf9\u5e94\u8d1f\u6570\uff0c[10-19]\u5bf9\u5e94\u6b63\u6570 (bucket + 10)\r\n            int[][] counter = new int[mod * 2][0];\r\n\r\n            for (int j = 0; j < arr.length; j++) {\r\n                int bucket = ((arr[j] % mod) \/ dev) + mod;\r\n                counter[bucket] = arrayAppend(counter[bucket], arr[j]);\r\n            }\r\n\r\n            int pos = 0;\r\n            for (int[] bucket : counter) {\r\n                for (int value : bucket) {\r\n                    arr[pos++] = value;\r\n                }\r\n            }\r\n        }\r\n\r\n        return arr;\r\n    }\r\n\r\n    \/**\r\n     * \u81ea\u52a8\u6269\u5bb9\uff0c\u5e76\u4fdd\u5b58\u6570\u636e\r\n     *\r\n     * @param arr\r\n     * @param value\r\n     *\/\r\n    private int[] arrayAppend(int[] arr, int value) {\r\n        arr = Arrays.copyOf(arr, arr.length + 1);\r\n        arr[arr.length - 1] = value;\r\n        return arr;\r\n    }\r\n}<\/pre>\n
    PHP<\/strong><\/span><\/div>\n

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

    function radixSort($arr, $maxDigit = null)\r\n{\r\n    if ($maxDigit === null) {\r\n        $maxDigit = max($arr);\r\n    }\r\n    $counter = [];\r\n    for ($i = 0; $i < $maxDigit; $i++) {\r\n        for ($j = 0; $j < count($arr); $j++) {\r\n            preg_match_all('\/\\d\/', (string) $arr[$j], $matches);\r\n            $numArr = $matches[0];\r\n            $lenTmp = count($numArr);\r\n            $bucket = array_key_exists($lenTmp - $i - 1, $numArr)\r\n                ? intval($numArr[$lenTmp - $i - 1])\r\n                : 0;\r\n            if (!array_key_exists($bucket, $counter)) {\r\n                $counter[$bucket] = [];\r\n            }\r\n            $counter[$bucket][] = $arr[$j];\r\n        }\r\n        $pos = 0;\r\n        for ($j = 0; $j < count($counter); $j++) {\r\n            $value = null;\r\n            if ($counter[$j] !== null) {\r\n                while (($value = array_shift($counter[$j])) !== null) {\r\n                    $arr[$pos++] = $value;\r\n                }\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

    int maxbit(int data[], int n) \/\/\u8f85\u52a9\u51fd\u6570\uff0c\u6c42\u6570\u636e\u7684\u6700\u5927\u4f4d\u6570\r\n{\r\n    int maxData = data[0];              \/\/\/< \u6700\u5927\u6570\r\n    \/\/\/ \u5148\u6c42\u51fa\u6700\u5927\u6570\uff0c\u518d\u6c42\u5176\u4f4d\u6570\uff0c\u8fd9\u6837\u6709\u539f\u5148\u4f9d\u6b21\u6bcf\u4e2a\u6570\u5224\u65ad\u5176\u4f4d\u6570\uff0c\u7a0d\u5fae\u4f18\u5316\u70b9\u3002\r\n    for (int i = 1; i < n; ++i)\r\n    {\r\n        if (maxData < data[i])\r\n            maxData = data[i];\r\n    }\r\n    int d = 1;\r\n    int p = 10;\r\n    while (maxData >= p)\r\n    {\r\n        \/\/p *= 10; \/\/ Maybe overflow\r\n        maxData \/= 10;\r\n        ++d;\r\n    }\r\n    return d;\r\n\/*    int d = 1; \/\/\u4fdd\u5b58\u6700\u5927\u7684\u4f4d\u6570\r\n    int p = 10;\r\n    for(int i = 0; i < n; ++i)\r\n    {\r\n        while(data[i] >= p)\r\n        {\r\n            p *= 10;\r\n            ++d;\r\n        }\r\n    }\r\n    return d;*\/\r\n}\r\nvoid radixsort(int data[], int n) \/\/\u57fa\u6570\u6392\u5e8f\r\n{\r\n    int d = maxbit(data, n);\r\n    int *tmp = new int[n];\r\n    int *count = new int[10]; \/\/\u8ba1\u6570\u5668\r\n    int i, j, k;\r\n    int radix = 1;\r\n    for(i = 1; i <= d; i++) \/\/\u8fdb\u884cd\u6b21\u6392\u5e8f\r\n    {\r\n        for(j = 0; j < 10; j++)\r\n            count[j] = 0; \/\/\u6bcf\u6b21\u5206\u914d\u524d\u6e05\u7a7a\u8ba1\u6570\u5668\r\n        for(j = 0; j < n; j++)\r\n        {\r\n            k = (data[j] \/ radix) % 10; \/\/\u7edf\u8ba1\u6bcf\u4e2a\u6876\u4e2d\u7684\u8bb0\u5f55\u6570\r\n            count[k]++;\r\n        }\r\n        for(j = 1; j < 10; j++)\r\n            count[j] = count[j - 1] + count[j]; \/\/\u5c06tmp\u4e2d\u7684\u4f4d\u7f6e\u4f9d\u6b21\u5206\u914d\u7ed9\u6bcf\u4e2a\u6876\r\n        for(j = n - 1; j >= 0; j--) \/\/\u5c06\u6240\u6709\u6876\u4e2d\u8bb0\u5f55\u4f9d\u6b21\u6536\u96c6\u5230tmp\u4e2d\r\n        {\r\n            k = (data[j] \/ radix) % 10;\r\n            tmp[count[k] - 1] = data[j];\r\n            count[k]--;\r\n        }\r\n        for(j = 0; j < n; j++) \/\/\u5c06\u4e34\u65f6\u6570\u7ec4\u7684\u5185\u5bb9\u590d\u5236\u5230data\u4e2d\r\n            data[j] = tmp[j];\r\n        radix = radix * 10;\r\n    }\r\n    delete []tmp;\r\n    delete []count;\r\n}<\/pre>\n
    C<\/strong><\/span><\/div>\n

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

    #include\r\n#define MAX 20\r\n\/\/#define SHOWPASS\r\n#define BASE 10\r\n\r\nvoid print(int *a, int n) {\r\n  int i;\r\n  for (i = 0; i < n; i++) {\r\n    printf(\"%d\\t\", a[i]);\r\n  }\r\n}\r\n\r\nvoid radixsort(int *a, int n) {\r\n  int i, b[MAX], m = a[0], exp = 1;\r\n\r\n  for (i = 1; i < n; i++) {\r\n    if (a[i] > m) {\r\n      m = a[i];\r\n    }\r\n  }\r\n\r\n  while (m \/ exp > 0) {\r\n    int bucket[BASE] = { 0 };\r\n\r\n    for (i = 0; i < n; i++) {\r\n      bucket[(a[i] \/ exp) % BASE]++;\r\n    }\r\n\r\n    for (i = 1; i < BASE; i++) {\r\n      bucket[i] += bucket[i - 1];\r\n    }\r\n\r\n    for (i = n - 1; i >= 0; i--) {\r\n      b[--bucket[(a[i] \/ exp) % BASE]] = a[i];\r\n    }\r\n\r\n    for (i = 0; i < n; i++) {\r\n      a[i] = b[i];\r\n    }\r\n\r\n    exp *= BASE;\r\n\r\n#ifdef SHOWPASS\r\n    printf(\"\\nPASS   : \");\r\n    print(a, n);\r\n#endif\r\n  }\r\n}\r\n\r\nint main() {\r\n  int arr[MAX];\r\n  int i, n;\r\n\r\n  printf(\"Enter total elements (n <= %d) : \", MAX);\r\n  scanf(\"%d\", &n);\r\n  n = n < MAX ? n : MAX;\r\n\r\n  printf(\"Enter %d Elements : \", n);\r\n  for (i = 0; i < n; i++) {\r\n    scanf(\"%d\", &arr[i]);\r\n  }\r\n\r\n  printf(\"\\nARRAY  : \");\r\n  print(&arr[0], n);\r\n\r\n  radixsort(&arr[0], n);\r\n\r\n  printf(\"\\nSORTED : \");\r\n  print(&arr[0], n);\r\n  printf(\"\\n\");\r\n\r\n  return 0;\r\n}<\/pre>\n
    Lua<\/strong><\/span><\/div>\n

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

    -- \u83b7\u53d6\u8868\u4e2d\u4f4d\u6570\r\nlocal maxBit = function (tt)\r\n    local weight = 10;      -- \u5341\u9032\u5236\r\n    local bit = 1;\r\n   \r\n    for k, v in pairs(tt) do\r\n        while v >= weight do\r\n            weight = weight * 10;\r\n            bit = bit + 1;  \r\n        end\r\n    end\r\n    return bit;\r\nend\r\n-- \u57fa\u6570\u6392\u5e8f\r\nlocal radixSort = function (tt)\r\n    local maxbit = maxBit(tt);\r\n\r\n    local bucket = {};\r\n    local temp = {};\r\n    local radix = 1;\r\n    for i = 1, maxbit do\r\n        for j = 1, 10 do\r\n            bucket[j] = 0;      --- \u6e05\u7a7a\u6876\r\n        end\r\n        for k, v in pairs(tt) do\r\n            local remainder = math.floor((v \/ radix)) % 10 + 1;    \r\n            bucket[remainder] = bucket[remainder] + 1;      -- \u6bcf\u500b\u6876\u6578\u91cf\u81ea\u52d5\u589e\u52a01\r\n        end\r\n       \r\n        for j = 2, 10 do\r\n            bucket[j] = bucket[j - 1] + bucket[j];  -- \u6bcf\u4e2a\u6876\u7684\u6570\u91cf = \u4ee5\u524d\u6876\u6570\u91cf\u548c + \u81ea\u4e2a\u6570\u91cf\r\n        end\r\n        -- \u6309\u7167\u6876\u7684\u4f4d\u7f6e\uff0c\u6392\u5e8f--\u8fd9\u4e2a\u662f\u6876\u5f0f\u6392\u5e8f\uff0c\u5fc5\u987b\u4f7f\u7528\u5012\u5e8f\uff0c\u56e0\u4e3a\u6392\u5e8f\u65b9\u6cd5\u662f\u4ece\u5c0f\u5230\u5927\uff0c\u987a\u5e8f\u4e0b\u6765\uff0c\u4f1a\u51fa\u73b0\u5927\u7684\u5728\u5c0f\u7684\u4e0a\u9762\u6e05\u7a7a\u3002\r\n        for k = #tt, 1, -1 do\r\n            local remainder = math.floor((tt[k] \/ radix)) % 10 + 1;\r\n            temp[bucket[remainder]] = tt[k];\r\n            bucket[remainder] = bucket[remainder] - 1;\r\n        end\r\n        for k, v in pairs(temp) do\r\n            tt[k] = v;\r\n        end\r\n        radix = radix * 10;\r\n    end\r\nend;<\/pre>\n","protected":false},"excerpt":{"rendered":"

    \u57fa\u6570\u6392\u5e8f\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a \u8fd9\u4e09\u79cd\u6392\u5e8f\u7b97\u6cd5\u90fd\u5229\u7528\u4e86\u6876\u7684\u6982\u5ff5\uff0c\u4f46\u5bf9\u6876\u7684\u4f7f\u7528\u65b9\u6cd5\u4e0a\u6709\u660e\u663e\u5dee\u5f02\uff1a \u57fa\u6570\u6392\u5e8f\uff1a\u6839\u636e\u952e\u503c\u7684\u6bcf\u4f4d […]<\/p>\n","protected":false},"author":321,"featured_media":211838,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-211507","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\/211507","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=211507"}],"version-history":[{"count":8,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/211507\/revisions"}],"predecessor-version":[{"id":211723,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/211507\/revisions\/211723"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/211838"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=211507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=211507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=211507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}