{"id":3181,"date":"2022-06-26T22:58:17","date_gmt":"2022-06-26T19:58:17","guid":{"rendered":"https:\/\/demensdeum.com\/blog\/?p=3181"},"modified":"2024-12-16T22:32:19","modified_gmt":"2024-12-16T19:32:19","slug":"counting-sort","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/zh\/2022\/06\/26\/counting-sort\/","title":{"rendered":"\u8ba1\u6570\u6392\u5e8f"},"content":{"rendered":"<p>\u8ba1\u6570\u6392\u5e8f&#8211;\u8ba1\u6570\u6392\u5e8f\u7b97\u6cd5\u3002\u6309\u7167\uff1f\u662f\u7684\uff01\u5c31\u8fd9\u6837\uff01<\/p>\n<p>\u8be5\u7b97\u6cd5\u81f3\u5c11\u6d89\u53ca\u4e24\u4e2a\u6570\u7ec4\uff0c\u7b2c\u4e00\u4e2a &#8211;\u8981\u6392\u5e8f\u7684\u6574\u6570\u5217\u8868\uff0c\u7b2c\u4e8c\u4e2a &#8211;\u5927\u5c0f = (\u6700\u5927\u6570 &#8211; \u6700\u5c0f\u6570) + 1 \u7684\u6570\u7ec4\uff0c\u6700\u521d\u4ec5\u5305\u542b\u96f6\u3002\u63a5\u4e0b\u6765\uff0c\u4ece\u7b2c\u4e00\u4e2a\u6570\u7ec4\u4e2d\u53d6\u51fa\u6570\u5b57\uff0c\u5e76\u4f7f\u7528\u6570\u5b57\u5143\u7d20\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\uff0c\u8be5\u7d22\u5f15\u52a0\u4e00\u3002\u904d\u5386\u6574\u4e2a\u5217\u8868\u540e\uff0c\u6211\u4eec\u5c06\u5f97\u5230\u4e00\u4e2a\u5b8c\u5168\u586b\u5145\u7684\u7b2c\u4e8c\u4e2a\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u7b2c\u4e00\u4e2a\u6570\u7ec4\u4e2d\u6570\u5b57\u7684\u91cd\u590d\u6b21\u6570\u3002 <strong>\u8be5\u7b97\u6cd5\u6709\u4e25\u91cd\u7684\u5f00\u9500\u2014\u2014\u7b2c\u4e8c\u4e2a\u6570\u7ec4\u8fd8\u5305\u542b\u4e0d\u5728\u7b2c\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u6570\u5b57\u7684\u96f6\uff0c\u5373\u6240\u8c13\u7684\u3002\u5185\u5b58\u5f00\u9500<\/strong><\/p>\n<p>\u6536\u5230\u7b2c\u4e8c\u4e2a\u6570\u7ec4\u540e\uff0c\u6211\u4eec\u8fed\u4ee3\u5b83\u5e76\u6309\u7d22\u5f15\u5199\u5165\u6570\u5b57\u7684\u6392\u5e8f\u7248\u672c\uff0c\u5c06\u8ba1\u6570\u5668\u9012\u51cf\u81f3\u96f6\u3002\u6700\u521d\uff0c\u96f6\u8ba1\u6570\u5668\u88ab\u5ffd\u7565\u3002<\/p>\n<p>\u8ba1\u6570\u6392\u5e8f\u7b97\u6cd5\u7684\u975e\u4f18\u5316\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<ol>\n<li>\u8f93\u5165\u6570\u7ec4 1,9,1,4,6,4,4<\/li>\n<li>\u90a3\u4e48\u8981\u8ba1\u6570\u7684\u6570\u7ec4\u5c06\u4e3a 0,1,2,3,4,5,6,7,8,9\uff08\u6700\u5c0f\u6570\u5b57 0\uff0c\u6700\u5927\u6570\u5b57 9\uff09<\/li>\n<li>\u8ba1\u6570\u5668\u603b\u6570\u4e3a 0,2,0,0,3,0,1,0,0,1<\/li>\n<li>\u6392\u5e8f\u6570\u7ec4\u603b\u6570 1,1,4,4,4,6,9<\/li>\n<\/ol>\n<p>Python 3 \u4e2d\u7684\u7b97\u6cd5\u4ee3\u7801\uff1a<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>\nnumbers = [42, 89, 69, 777, 22, 35, 42, 69, 42, 90, 777]\n\nminimal = min(numbers)\nmaximal = max(numbers)\ncountListRange = maximal - minimal\ncountListRange += 1\ncountList = [0] * countListRange\n\nprint(numbers)\nprint(f\"Minimal number: {minimal}\")\nprint(f\"Maximal number: {maximal}\")\nprint(f\"Count list size: {countListRange}\")\n\nfor number in numbers:\n    index = number - minimal\n    countList[index] += 1\n\nreplacingIndex = 0\nfor index, count in enumerate(countList):\n    for i in range(count):\n        outputNumber = minimal + index\n        numbers[replacingIndex] = outputNumber\n        replacingIndex += 1\n\nprint(numbers)<\/code><\/pre>\n<p>\u0418\u0437-\u0437\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0432\u0443\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 <strong>O<\/strong>(<strong>n<\/strong> + <strong>k<\/strong>)<\/p>\n<h3>\u0421\u0441\u044b\u043b\u043a\u0438<\/h3>\n<p><a href=\"https:\/\/gitlab.com\/demensdeum\/algorithms\/-\/tree\/master\/sortAlgorithms\/countingSort\" target=\"_blank\" rel=\"noopener\">https:\/\/gitlab.com\/demensdeum\/algorithms\/-\/tree\/master\/sortAlgorithms\/countingSort<\/a><\/p>\n<h3>\u0418\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0438<\/h3>\n<p><a href=\"https:\/\/www.youtube.com\/watch?v=6dk_csyWif0\" target=\"_blank\" rel=\"noopener\">https:\/\/www.youtube.com\/watch?v=6dk_csyWif0 <\/a><br \/>\n<a href=\"https:\/\/www.youtube.com\/watch?v=OKd534EWcdk\" target=\"_blank\" rel=\"noopener\">https:\/\/www.youtube.com\/watch?v=OKd534EWcdk <\/a><br \/>\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Counting_sort\" target=\"_blank\" rel=\"noopener\">https:\/\/en.wikipedia.org\/wiki\/Counting_sort<\/a><br \/>\n<a href=\"https:\/\/rosettacode.org\/wiki\/Sorting_algorithms\/Counting_sort\" target=\"_blank\" rel=\"noopener\">https:\/\/rosettacode.org\/wiki\/Sorting_algorithms\/Counting_sort<\/a><br \/>\n<a href=\"https:\/\/pro-prof.com\/forums\/topic\/%D0%B0%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC-%D1%81%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B8-%D0%BF%D0%BE%D0%B4%D1%81%D1%87%D0%B5%D1%82%D0%BE%D0%BC\" target=\"_blank\" rel=\"noopener\">https:\/\/pro-prof.com\/forums\/topic\/%D0%B0%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC-%D1%81%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B8-%D0%BF%D0%BE%D0%B4%D1%81%D1%87%D0%B5%D1%82%D0%BE%D0%BC<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8ba1\u6570\u6392\u5e8f&#8211;\u8ba1\u6570\u6392\u5e8f\u7b97\u6cd5\u3002\u6309\u7167\uff1f\u662f\u7684\uff01\u5c31\u8fd9\u6837\uff01 \u8be5\u7b97\u6cd5\u81f3\u5c11\u6d89\u53ca\u4e24\u4e2a\u6570\u7ec4\uff0c\u7b2c\u4e00\u4e2a &#8211;\u8981\u6392\u5e8f\u7684\u6574\u6570\u5217\u8868\uff0c\u7b2c\u4e8c\u4e2a &#8211;\u5927\u5c0f = (\u6700\u5927\u6570 &#8211; \u6700\u5c0f\u6570) + 1 \u7684\u6570\u7ec4\uff0c\u6700\u521d\u4ec5\u5305\u542b\u96f6\u3002\u63a5\u4e0b\u6765\uff0c\u4ece\u7b2c\u4e00\u4e2a\u6570\u7ec4\u4e2d\u53d6\u51fa\u6570\u5b57\uff0c\u5e76\u4f7f\u7528\u6570\u5b57\u5143\u7d20\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\uff0c\u8be5\u7d22\u5f15\u52a0\u4e00\u3002\u904d\u5386\u6574\u4e2a\u5217\u8868\u540e\uff0c\u6211\u4eec\u5c06\u5f97\u5230\u4e00\u4e2a\u5b8c\u5168\u586b\u5145\u7684\u7b2c\u4e8c\u4e2a\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u7b2c\u4e00\u4e2a\u6570\u7ec4\u4e2d\u6570\u5b57\u7684\u91cd\u590d\u6b21\u6570\u3002 \u8be5\u7b97\u6cd5\u6709\u4e25\u91cd\u7684\u5f00\u9500\u2014\u2014\u7b2c\u4e8c\u4e2a\u6570\u7ec4\u8fd8\u5305\u542b\u4e0d\u5728\u7b2c\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u6570\u5b57\u7684\u96f6\uff0c\u5373\u6240\u8c13\u7684\u3002\u5185\u5b58\u5f00\u9500 \u6536\u5230\u7b2c\u4e8c\u4e2a\u6570\u7ec4\u540e\uff0c\u6211\u4eec\u8fed\u4ee3\u5b83\u5e76\u6309\u7d22\u5f15\u5199\u5165\u6570\u5b57\u7684\u6392\u5e8f\u7248\u672c\uff0c\u5c06\u8ba1\u6570\u5668\u9012\u51cf\u81f3\u96f6\u3002\u6700\u521d\uff0c\u96f6\u8ba1\u6570\u5668\u88ab\u5ffd\u7565\u3002 \u8ba1\u6570\u6392\u5e8f\u7b97\u6cd5\u7684\u975e\u4f18\u5316\u64cd\u4f5c\u793a\u4f8b\uff1a \u8f93\u5165\u6570\u7ec4 1,9,1,4,6,4,4 \u90a3\u4e48\u8981\u8ba1\u6570\u7684\u6570\u7ec4\u5c06\u4e3a 0,1,2,3,4,5,6,7,8,9\uff08\u6700\u5c0f\u6570\u5b57 0\uff0c\u6700\u5927\u6570\u5b57 9\uff09 \u8ba1\u6570\u5668\u603b\u6570\u4e3a 0,2,0,0,3,0,1,0,0,1 \u6392\u5e8f\u6570\u7ec4\u603b\u6570 1,1,4,4,4,6,9 Python 3 \u4e2d\u7684\u7b97\u6cd5\u4ee3\u7801\uff1a numbers = [42, 89, 69, 777, 22, 35, 42, 69, 42, 90, 777] minimal = min(numbers) maximal = max(numbers) countListRange = maximal &#8211; minimal countListRange += 1 countList<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/zh\/2022\/06\/26\/counting-sort\/\">Continue reading <span class=\"screen-reader-text\">&#8220;\u8ba1\u6570\u6392\u5e8f&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[61,52],"tags":[131,191,190],"class_list":["post-3181","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","tag-algorithms","tag-counting-sort","tag-sorting","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"zh","enabled_languages":["en","ru","zh","de","fr","ja","pt","hi"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"ru":{"title":true,"content":true,"excerpt":false},"zh":{"title":true,"content":true,"excerpt":false},"de":{"title":true,"content":true,"excerpt":false},"fr":{"title":true,"content":true,"excerpt":false},"ja":{"title":true,"content":true,"excerpt":false},"pt":{"title":true,"content":true,"excerpt":false},"hi":{"title":false,"content":false,"excerpt":false}}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/posts\/3181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/comments?post=3181"}],"version-history":[{"count":21,"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/posts\/3181\/revisions"}],"predecessor-version":[{"id":3879,"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/posts\/3181\/revisions\/3879"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/media?parent=3181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/categories?post=3181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/zh\/wp-json\/wp\/v2\/tags?post=3181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}