{"id":4652,"date":"2020-01-14T19:06:38","date_gmt":"2020-01-14T11:06:38","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=4652"},"modified":"2020-01-14T19:15:01","modified_gmt":"2020-01-14T11:15:01","slug":"%e6%8e%92%e5%ba%8f%e7%ae%97%e6%b3%95%e4%b9%8b%e6%a1%b6%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4652","title":{"rendered":"\u6392\u5e8f\u7b97\u6cd5\u4e4b\u6876\u6392\u5e8f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u6876\u6392\u5e8f\uff08Bucket sort\uff09\u4e5f\u6709\u4eba\u53eb\u505a\u6240\u8c13\u7684\u7bb1\u6392\u5e8f\u3002\u542c\u8fc7\u7684\u4eba\u53ef\u80fd\u4e0d\u591a\uff0c\u540d\u6c14\u4e0d\u5927\uff0c\u4f46\u5b83\u5374\u662f 10 \u5927\u6392\u5e8f\u7b97\u6cd5\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u8db3\u89c1\u5f97\u5b83\u7684\u91cd\u8981\u6027\u3002\u4eca\u5929\u6211\u4eec\u4e00\u8d77\u6765\u804a\u804a\u5b83\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6876\u6392\u5e8f\u7684\u5de5\u4f5c\u539f\u7406\u662f\u5c06\u6570\u7ec4\u5206\u5230\u6709\u9650\u6570\u91cf\u7684\u6876\u91cc\u3002\u6bcf\u4e2a\u6876\u518d\u4e2a\u522b\u6392\u5e8f\uff08\u6709\u53ef\u80fd\u518d\u4f7f\u7528\u522b\u7684\u6392\u5e8f\u7b97\u6cd5\u6216\u662f\u4ee5\u9012\u5f52\u65b9\u5f0f\u7ee7\u7eed\u4f7f\u7528\u6876\u6392\u5e8f\u8fdb\u884c\u6392\u5e8f\uff09\u3002\u6876\u6392\u5e8f\u662f\u9e3d\u5de2\u6392\u5e8f\u7684\u4e00\u79cd\u5f52\u7eb3\u7ed3\u679c\u3002\u5f53\u8981\u88ab\u6392\u5e8f\u7684\u6570\u7ec4\u5185\u7684\u6570\u503c\u662f\u5747\u5300\u5206\u914d\u7684\u65f6\u5019\uff0c\u6876\u6392\u5e8f\u4f7f\u7528\u7ebf\u6027\u65f6\u95f4\u3002\u4f46\u6876\u6392\u5e8f\u5e76\u4e0d\u662f\u6bd4\u8f83\u6392\u5e8f\uff0c\u4ed6\u4e0d\u53d7\u5230O(n log n) \u4e0b\u9650\u7684\u5f71\u54cd\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6876\u6392\u5e8f\u662f\u8ba1\u6570\u6392\u5e8f\u7684\u5347\u7ea7\u7248\u3002\u5b83\u5229\u7528\u4e86\u51fd\u6570\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u9ad8\u6548\u4e0e\u5426\u7684\u5173\u952e\u5c31\u5728\u4e8e\u8fd9\u4e2a\u6620\u5c04\u51fd\u6570\u7684\u786e\u5b9a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u4f7f\u6876\u6392\u5e8f\u66f4\u52a0\u9ad8\u6548\uff0c\u6211\u4eec\u9700\u8981\u505a\u5230\u8fd9\u4e24\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5728\u989d\u5916\u7a7a\u95f4\u5145\u8db3\u7684\u60c5\u51b5\u4e0b\uff0c\u5c3d\u91cf\u589e\u5927\u6876\u7684\u6570\u91cf<\/li><li>\u4f7f\u7528\u7684\u6620\u5c04\u51fd\u6570\u80fd\u591f\u5c06\u8f93\u5165\u7684 N \u4e2a\u6570\u636e\u5747\u5300\u7684\u5206\u914d\u5230 K \u4e2a\u6876\u4e2d<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u540c\u65f6\uff0c\u5bf9\u4e8e\u6876\u4e2d\u5143\u7d20\u7684\u6392\u5e8f\uff0c\u9009\u62e9\u4f55\u79cd\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\u5bf9\u4e8e\u6027\u80fd\u7684\u5f71\u54cd\u81f3\u5173\u91cd\u8981\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6392\u5e8f\u8fc7\u7a0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u8bbe\u7f6e\u4e00\u4e2a\u5b9a\u91cf\u7684\u6570\u7ec4\u5f53\u4f5c\u7a7a\u6876\u5b50\u3002<\/li><li>\u5bfb\u8bbf\u5e8f\u5217\uff0c\u5e76\u4e14\u628a\u9879\u76ee\u4e00\u4e2a\u4e00\u4e2a\u653e\u5230\u5bf9\u5e94\u7684\u6876\u5b50\u53bb\u3002<\/li><li>\u5bf9\u6bcf\u4e2a\u4e0d\u662f\u7a7a\u7684\u6876\u5b50\u8fdb\u884c\u6392\u5e8f\u3002<\/li><li>\u4ece\u4e0d\u662f\u7a7a\u7684\u6876\u5b50\u91cc\u628a\u9879\u76ee\u518d\u653e\u56de\u539f\u6765\u7684\u5e8f\u5217\u4e2d\u3002<\/li><\/ol>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/tongpaixu.gif\" alt=\"\" \/><figcaption>\u6876\u6392\u5e8f<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u6876\u6392\u5e8f\u7684 Java \u6848\u4f8b\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private int indexFor(int a, int min, int step) {\n    return (a - min) \/ step;\n}\npublic void bucketSort(int[] arr) {\n    int max = arr[0], min = arr[0];\n    for (int a : arr) {\n        if (max &lt; a)\n            max = a;\n        if (min &gt; a)\n            min = a;\n    }\n    \/\/ \u8be5\u503c\u4e5f\u53ef\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u9009\u62e9\n    int bucketNum = max \/ 10 - min \/ 10 + 1;\n    List buckList = new ArrayList&lt;List&lt;Integer&gt;&gt;();\n    \/\/ create bucket\n    for (int i = 1; i &lt;= bucketNum; i++) {\n        buckList.add(new ArrayList&lt;Integer&gt;());\n    }\n    \/\/ push into the bucket\n    for (int i = 0; i &lt; arr.length; i++) {\n        int index = indexFor(arr[i], min, 10);\n        ((ArrayList&lt;Integer&gt;) buckList.get(index)).add(arr[i]);\n    }\n    ArrayList&lt;Integer&gt; bucket = null;\n    int index = 0;\n    for (int i = 0; i &lt; bucketNum; i++) {\n        bucket = (ArrayList&lt;Integer&gt;) buckList.get(i);\n        insertSort(bucket);\n        for (int k : bucket) {\n            arr[index++] = k;\n        }\n    }\n}\n\/\/ \u628a\u6876\u5167\u5143\u7d20\u63d2\u5165\u6392\u5e8f\nprivate void insertSort(List&lt;Integer&gt; bucket) {\n    for (int i = 1; i &lt; bucket.size(); i++) {\n        int temp = bucket.get(i);\n        int j = i - 1;\n        for (; j &gt;= 0 &amp;&amp; bucket.get(j) &gt; temp; j--) {\n            bucket.set(j + 1, bucket.get(j));\n        }\n        bucket.set(j + 1, temp);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6392\u5e8f\u52a8\u6001\u6548\u679c\u56fe\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/buck_sort.gif\" alt=\"\" \/><figcaption>\u6876\u6392\u5e8f\u52a8\u753b<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6570\u636e\u662f\u5747\u5300\u5206\u5e03\u7684\uff0c\u5219\u6bcf\u4e2a\u6876\u7684\u5143\u7d20\u5e73\u5747\u4e2a\u6570\u4e3an\/k\u3002\u5047\u8bbe\u9009\u62e9\u7528\u5feb\u901f\u6392\u5e8f\u5bf9\u6bcf\u4e2a\u6876\u5185\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff0c\u90a3\u4e48\u6bcf\u6b21\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n\/klog(n\/k))\u3002\u603b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)+O(m)O(n\/klog(n\/k))=O(n+nlog(n\/k))=O(n+nlogn-nlogk\u3002\u5f53k\u63a5\u8fd1\u4e8en\u65f6\uff0c\u6876\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c31\u53ef\u4ee5\u91d1\u65af\u8ba4\u4e3a\u662fO(n)\u7684\u3002\u5373\u6876\u8d8a\u591a\uff0c\u65f6\u95f4\u6548\u7387\u5c31\u8d8a\u9ad8\uff0c\u800c\u6876\u8d8a\u591a\uff0c\u7a7a\u95f4\u5c31\u8d8a\u5927\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=4652\">\u6392\u5e8f\u7b97\u6cd5\u4e4b\u6876\u6392\u5e8f<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6876\u6392\u5e8f\uff08Bucket sort\uff09\u4e5f\u6709\u4eba\u53eb\u505a\u6240\u8c13\u7684\u7bb1\u6392\u5e8f\u3002\u542c\u8fc7\u7684\u4eba\u53ef\u80fd\u4e0d\u591a\uff0c\u540d\u6c14\u4e0d\u5927\uff0c\u4f46\u5b83\u5374\u662f 10 \u5927\u6392\u5e8f\u7b97\u6cd5 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[823,175,821,172,2054],"class_list":["post-4652","post","type-post","status-publish","format-standard","hentry","category-java","tag-823","tag-175","tag-821","tag-172","tag-2054"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4652"}],"version-history":[{"count":2,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4652\/revisions"}],"predecessor-version":[{"id":4654,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4652\/revisions\/4654"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}