gh-pages
krahets 2 years ago
parent d6c1a09d4d
commit fa21213a66

@ -1347,6 +1347,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1450,9 +1452,23 @@
<li class="md-nav__item">
<a href="/chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="/chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1358,6 +1358,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1461,9 +1463,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1358,6 +1358,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1461,9 +1463,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>
@ -1926,7 +1942,7 @@
<nav class="md-footer__inner md-grid" aria-label="页脚" >
<a href="../../chapter_sorting/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 11.7. &amp;nbsp; 小结" rel="prev">
<a href="../../chapter_sorting/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 11.8. &amp;nbsp; 小结" rel="prev">
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
@ -1935,7 +1951,7 @@
<span class="md-footer__direction">
上一页
</span>
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</div>
</div>
</a>

@ -1422,6 +1422,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1525,9 +1527,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1422,6 +1422,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1525,9 +1527,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1408,6 +1408,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1511,9 +1513,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1370,6 +1370,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1473,9 +1475,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1435,6 +1435,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1538,9 +1540,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1456,6 +1456,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1559,9 +1561,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1421,6 +1421,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1524,9 +1526,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1415,6 +1415,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1518,9 +1520,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1511,6 +1511,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1614,9 +1616,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1408,6 +1408,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1511,9 +1513,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1435,6 +1435,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1538,9 +1540,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1370,6 +1370,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1473,9 +1475,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1442,6 +1442,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1545,9 +1547,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1415,6 +1415,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1518,9 +1520,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1448,6 +1448,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1551,9 +1553,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1370,6 +1370,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1473,9 +1475,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1435,6 +1435,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1538,9 +1540,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1422,6 +1422,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1525,9 +1527,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1370,6 +1370,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1473,9 +1475,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1428,6 +1428,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1531,9 +1533,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1456,6 +1456,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1559,9 +1561,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1370,6 +1370,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1473,9 +1475,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1370,6 +1370,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1473,9 +1475,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1370,6 +1370,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1473,9 +1475,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1415,6 +1415,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1518,9 +1520,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1413,6 +1413,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1516,9 +1518,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1429,6 +1429,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1532,9 +1534,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1370,6 +1370,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1473,9 +1475,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1356,6 +1356,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1459,9 +1461,23 @@
<li class="md-nav__item">
<a href="../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1449,6 +1449,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1552,9 +1554,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1415,6 +1415,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1518,9 +1520,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1415,6 +1415,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1518,9 +1520,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1370,6 +1370,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1473,9 +1475,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1360,6 +1360,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1518,9 +1520,23 @@
<li class="md-nav__item">
<a href="../bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

Binary file not shown.

After

Width:  |  Height:  |  Size: 132 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

@ -15,13 +15,17 @@
<link rel="canonical" href="https://www.hello-algo.com/chapter_sorting/bucket_sort/">
<link rel="prev" href="../counting_sort/">
<link rel="next" href="../summary/">
<link rel="icon" href="../../assets/images/favicon.png">
<meta name="generator" content="mkdocs-1.4.2, mkdocs-material-9.1.1">
<title>桶排序 - Hello 算法</title>
<title>11.7.   桶排序New - Hello 算法</title>
@ -75,7 +79,7 @@
<div data-md-component="skip">
<a href="#_1" class="md-skip">
<a href="#117" class="md-skip">
跳转至
</a>
@ -109,7 +113,7 @@
<div class="md-header__topic" data-md-component="header-topic">
<span class="md-ellipsis">
桶排序
11.7. &nbsp; 桶排序New
</span>
</div>
@ -1327,16 +1331,18 @@
<li class="md-nav__item md-nav__item--section md-nav__item--nested">
<li class="md-nav__item md-nav__item--active md-nav__item--section md-nav__item--nested">
<input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_12" >
<input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_12" checked>
@ -1354,6 +1360,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1361,7 +1369,7 @@
<span class="md-nav__icon md-icon"></span>
</label>
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_12_label" aria-expanded="false">
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_12_label" aria-expanded="true">
<label class="md-nav__title" for="__nav_12">
<span class="md-nav__icon md-icon"></span>
11. &nbsp; &nbsp; 排序算法
@ -1456,10 +1464,79 @@
<li class="md-nav__item md-nav__item--active">
<input class="md-nav__toggle md-toggle" type="checkbox" id="__toc">
<label class="md-nav__link md-nav__link--active" for="__toc">
11.7. &nbsp; 桶排序New
<span class="md-nav__icon md-icon"></span>
</label>
<a href="./" class="md-nav__link md-nav__link--active">
11.7. &nbsp; 桶排序New
</a>
<nav class="md-nav md-nav--secondary" aria-label="目录">
<label class="md-nav__title" for="__toc">
<span class="md-nav__icon md-icon"></span>
目录
</label>
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
<li class="md-nav__item">
<a href="#1171" class="md-nav__link">
11.7.1. &nbsp; 算法流程
</a>
</li>
<li class="md-nav__item">
<a href="#1172" class="md-nav__link">
11.7.2. &nbsp; 算法特性
</a>
</li>
<li class="md-nav__item">
<a href="#1173" class="md-nav__link">
11.7.3. &nbsp; 如何实现平均分配
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="../summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>
@ -1610,6 +1687,35 @@
<label class="md-nav__title" for="__toc">
<span class="md-nav__icon md-icon"></span>
目录
</label>
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
<li class="md-nav__item">
<a href="#1171" class="md-nav__link">
11.7.1. &nbsp; 算法流程
</a>
</li>
<li class="md-nav__item">
<a href="#1172" class="md-nav__link">
11.7.2. &nbsp; 算法特性
</a>
</li>
<li class="md-nav__item">
<a href="#1173" class="md-nav__link">
11.7.3. &nbsp; 如何实现平均分配
</a>
</li>
</ul>
</nav>
</div>
</div>
@ -1631,25 +1737,160 @@
<h1 id="_1">桶排序<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h1>
<p>「桶排序 Bucket Sort」考虑设置 <span class="arithmatex">\(k\)</span> 个桶,并将 <span class="arithmatex">\(n\)</span> 个元素根据大小分配到 <span class="arithmatex">\(k\)</span> 个桶中,<strong>并在每个桶内部分别执行排序</strong>,由于桶之间的大小关系的确定的,因此最后按照桶之间的顺序将元素依次展开即可。</p>
<p>假设元素平均分布在各个桶内,则每个桶内元素数量为 <span class="arithmatex">\(\frac{n}{k}\)</span> ;如果使用「快速排序」来实现桶内排序,则排序单个桶使用 <span class="arithmatex">\(O(\frac{n}{k} \log\frac{n}{k})\)</span> 时间,排序所有桶使用 <span class="arithmatex">\(O(n \log\frac{n}{k})\)</span> 时间。<strong>当桶数量 <span class="arithmatex">\(k\)</span> 比较大时,时间复杂度则趋向于 <span class="arithmatex">\(O(n)\)</span></strong></p>
<div class="admonition note 计数排序与桶排序的关系">
<p class="admonition-title">Note</p>
<p><strong>计数排序可以看作是桶排序的一种特例</strong>。我们可以把计数排序中 <code>counter</code> 的每个索引想象成一个桶,将统计数量的过程想象成把 <span class="arithmatex">\(n\)</span> 个元素分配到对应的桶中,再根据桶之间的有序性输出结果,从而实现排序。</p>
<h1 id="117">11.7. &nbsp; 桶排序<a class="headerlink" href="#117" title="Permanent link">&para;</a></h1>
<p>「桶排序 Bucket Sort」是分治思想的典型体现其通过设置一些桶将数据平均分配到各个桶中并在每个桶内部分别执行排序最终根据桶之间天然的大小顺序将各个桶内元素合并从而得到排序结果。</p>
<h2 id="1171">11.7.1. &nbsp; 算法流程<a class="headerlink" href="#1171" title="Permanent link">&para;</a></h2>
<p>输入一个长度为 <span class="arithmatex">\(n\)</span> 的数组,元素是范围 <span class="arithmatex">\([0, 1)\)</span> 的浮点数,桶排序流程为:</p>
<ol>
<li>初始化 <span class="arithmatex">\(k\)</span> 个桶,将 <span class="arithmatex">\(n\)</span> 个元素分配至 <span class="arithmatex">\(k\)</span> 个桶中;</li>
<li>对每个桶分别执行排序(本文采用编程语言的内置排序函数);</li>
<li>按照桶的从小到大的顺序,合并结果;</li>
</ol>
<p><img alt="桶排序算法流程" src="../bucket_sort.assets/bucket_sort_overview.png" /></p>
<p align="center"> Fig. 桶排序算法流程 </p>
<div class="tabbed-set tabbed-alternate" data-tabs="1:10"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" name="__tabbed_1" type="radio" /><input id="__tabbed_1_8" name="__tabbed_1" type="radio" /><input id="__tabbed_1_9" name="__tabbed_1" type="radio" /><input id="__tabbed_1_10" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Java</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Python</label><label for="__tabbed_1_4">Go</label><label for="__tabbed_1_5">JavaScript</label><label for="__tabbed_1_6">TypeScript</label><label for="__tabbed_1_7">C</label><label for="__tabbed_1_8">C#</label><label for="__tabbed_1_9">Swift</label><label for="__tabbed_1_10">Zig</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
<div class="highlight"><span class="filename">bucket_sort.java</span><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="cm">/* 桶排序 */</span>
<a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="kt">void</span><span class="w"> </span><span class="nf">bucketSort</span><span class="p">(</span><span class="kt">float</span><span class="o">[]</span><span class="w"> </span><span class="n">nums</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="w"> </span><span class="c1">// 初始化 k = n/3 个桶,预期向每个桶分配 3 个元素</span>
<a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">nums</span><span class="p">.</span><span class="na">length</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span>
<a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="w"> </span><span class="n">List</span><span class="o">&lt;</span><span class="n">List</span><span class="o">&lt;</span><span class="n">Float</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="n">buckets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ArrayList</span><span class="o">&lt;&gt;</span><span class="p">();</span>
<a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">k</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="w"> </span><span class="n">buckets</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="k">new</span><span class="w"> </span><span class="n">ArrayList</span><span class="o">&lt;&gt;</span><span class="p">());</span>
<a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a><span class="w"> </span><span class="c1">// 1. 将数组元素分配到各个桶中</span>
<a id="__codelineno-0-10" name="__codelineno-0-10" href="#__codelineno-0-10"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">float</span><span class="w"> </span><span class="n">num</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">nums</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-0-11" name="__codelineno-0-11" href="#__codelineno-0-11"></a><span class="w"> </span><span class="c1">// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]</span>
<a id="__codelineno-0-12" name="__codelineno-0-12" href="#__codelineno-0-12"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="w"> </span><span class="n">num</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">k</span><span class="p">;</span>
<a id="__codelineno-0-13" name="__codelineno-0-13" href="#__codelineno-0-13"></a><span class="w"> </span><span class="c1">// 将 num 添加进桶 i</span>
<a id="__codelineno-0-14" name="__codelineno-0-14" href="#__codelineno-0-14"></a><span class="w"> </span><span class="n">buckets</span><span class="p">.</span><span class="na">get</span><span class="p">(</span><span class="n">i</span><span class="p">).</span><span class="na">add</span><span class="p">(</span><span class="n">num</span><span class="p">);</span>
<a id="__codelineno-0-15" name="__codelineno-0-15" href="#__codelineno-0-15"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-0-16" name="__codelineno-0-16" href="#__codelineno-0-16"></a><span class="w"> </span><span class="c1">// 2. 对各个桶执行排序</span>
<a id="__codelineno-0-17" name="__codelineno-0-17" href="#__codelineno-0-17"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">List</span><span class="o">&lt;</span><span class="n">Float</span><span class="o">&gt;</span><span class="w"> </span><span class="n">bucket</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">buckets</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-0-18" name="__codelineno-0-18" href="#__codelineno-0-18"></a><span class="w"> </span><span class="c1">// 使用内置排序函数,也可以替换成其它排序算法</span>
<a id="__codelineno-0-19" name="__codelineno-0-19" href="#__codelineno-0-19"></a><span class="w"> </span><span class="n">Collections</span><span class="p">.</span><span class="na">sort</span><span class="p">(</span><span class="n">bucket</span><span class="p">);</span>
<a id="__codelineno-0-20" name="__codelineno-0-20" href="#__codelineno-0-20"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-0-21" name="__codelineno-0-21" href="#__codelineno-0-21"></a><span class="w"> </span><span class="c1">// 3. 遍历桶合并结果</span>
<a id="__codelineno-0-22" name="__codelineno-0-22" href="#__codelineno-0-22"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<a id="__codelineno-0-23" name="__codelineno-0-23" href="#__codelineno-0-23"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">List</span><span class="o">&lt;</span><span class="n">Float</span><span class="o">&gt;</span><span class="w"> </span><span class="n">bucket</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">buckets</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-0-24" name="__codelineno-0-24" href="#__codelineno-0-24"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">float</span><span class="w"> </span><span class="n">num</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">bucket</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-0-25" name="__codelineno-0-25" href="#__codelineno-0-25"></a><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">i</span><span class="o">++]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">num</span><span class="p">;</span>
<a id="__codelineno-0-26" name="__codelineno-0-26" href="#__codelineno-0-26"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-0-27" name="__codelineno-0-27" href="#__codelineno-0-27"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-0-28" name="__codelineno-0-28" href="#__codelineno-0-28"></a><span class="p">}</span>
</code></pre></div>
</div>
<p>(图)</p>
<p>理论上桶排序的时间复杂度是 <span class="arithmatex">\(O(n)\)</span> <strong>但前提是需要将元素均匀分配到各个桶中</strong>,而这并不容易做到。假设想要把淘宝的 <span class="arithmatex">\(100\)</span> 万件商品根据价格范围平均分配到 <span class="arithmatex">\(100\)</span> 个桶中,而商品价格不是均匀分布的,例如 <span class="arithmatex">\(100\)</span> 元以下的商品非常多、<span class="arithmatex">\(1000\)</span> 元以上的商品非常少等。如果我们将价格区间平均划分为 <span class="arithmatex">\(100\)</span> 份,那么各个桶内的商品数量差距会非常大。为了实现平均分配,我们一般这样做:</p>
<ul>
<li>先粗略设置分界线,将元素分配完后,<strong>把元素较多的桶继续划分为多个桶</strong>,直至所有桶内元素数量合理为止;该做法本质上是一个递归树;</li>
<li>如果我们提前知道商品价格的概率分布,<strong>则可以根据已知分布来设置每个桶的价格分界线</strong>;值得说明的是,数据分布不一定需要特意统计,也可以根据数据特点采用某种常见概率模型来近似,例如自然界的正态分布等;</li>
</ul>
<p>(图)</p>
<p>另外,排序桶内元素需要选择一种合适的排序算法,比如快速排序。</p>
<div class="tabbed-block">
<div class="highlight"><span class="filename">bucket_sort.cpp</span><pre><span></span><code><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="p">[</span><span class="k">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="n">func</span><span class="p">]{</span><span class="n">bucketSort</span><span class="p">}</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><span class="filename">bucket_sort.py</span><pre><span></span><code><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="p">[</span><span class="n">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="n">func</span><span class="p">]{</span><span class="n">bucket_sort</span><span class="p">}</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><span class="filename">bucket_sort.go</span><pre><span></span><code><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="p">[</span><span class="nx">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="kd">func</span><span class="p">]{</span><span class="nx">bucketSort</span><span class="p">}</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><span class="filename">bucket_sort.js</span><pre><span></span><code><a id="__codelineno-4-1" name="__codelineno-4-1" href="#__codelineno-4-1"></a><span class="p">[</span><span class="kd">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="nx">func</span><span class="p">]{</span><span class="nx">bucketSort</span><span class="p">}</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><span class="filename">bucket_sort.ts</span><pre><span></span><code><a id="__codelineno-5-1" name="__codelineno-5-1" href="#__codelineno-5-1"></a><span class="p">[</span><span class="kd">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="nx">func</span><span class="p">]{</span><span class="nx">bucketSort</span><span class="p">}</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><span class="filename">bucket_sort.c</span><pre><span></span><code><a id="__codelineno-6-1" name="__codelineno-6-1" href="#__codelineno-6-1"></a><span class="p">[</span><span class="n">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="n">func</span><span class="p">]{</span><span class="n">bucketSort</span><span class="p">}</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><span class="filename">bucket_sort.cs</span><pre><span></span><code><a id="__codelineno-7-1" name="__codelineno-7-1" href="#__codelineno-7-1"></a><span class="na">[class]</span><span class="p">{</span><span class="n">bucket_sort</span><span class="p">}</span><span class="o">-</span><span class="p">[</span><span class="n">func</span><span class="p">]{</span><span class="n">bucketSort</span><span class="p">}</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><span class="filename">bucket_sort.swift</span><pre><span></span><code><a id="__codelineno-8-1" name="__codelineno-8-1" href="#__codelineno-8-1"></a><span class="p">[</span><span class="kd">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="kd">func</span><span class="p">]{</span><span class="n">bucketSort</span><span class="p">}</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><span class="filename">bucket_sort.zig</span><pre><span></span><code><a id="__codelineno-9-1" name="__codelineno-9-1" href="#__codelineno-9-1"></a><span class="p">[</span><span class="n">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="n">func</span><span class="p">]{</span><span class="n">bucketSort</span><span class="p">}</span>
</code></pre></div>
</div>
</div>
</div>
<div class="admonition note">
<p class="admonition-title">桶排序是计数排序的一种推广</p>
<p>从桶排序的角度,我们可以把计数排序中计数数组 <code>counter</code> 的每个索引想象成一个桶,将统计数量的过程想象成把各个元素分配到对应的桶中,再根据桶之间的有序性输出结果,从而实现排序。</p>
</div>
<h2 id="1172">11.7.2. &nbsp; 算法特性<a class="headerlink" href="#1172" title="Permanent link">&para;</a></h2>
<p><strong>时间复杂度 <span class="arithmatex">\(O(n + k)\)</span></strong> :假设元素平均分布在各个桶内,则每个桶内元素数量为 <span class="arithmatex">\(\frac{n}{k}\)</span> 。假设排序单个桶使用 <span class="arithmatex">\(O(\frac{n}{k} \log\frac{n}{k})\)</span> 时间,则排序所有桶使用 <span class="arithmatex">\(O(n \log\frac{n}{k})\)</span> 时间,<strong>当桶数量 <span class="arithmatex">\(k\)</span> 比较大时,时间复杂度则趋向于 <span class="arithmatex">\(O(n)\)</span></strong> 。最后合并结果需要遍历 <span class="arithmatex">\(n\)</span> 个桶,使用 <span class="arithmatex">\(O(k)\)</span> 时间。</p>
<p>最差情况下,所有数据被分配到一个桶中,且排序算法退化至 <span class="arithmatex">\(O(n^2)\)</span> ,此时使用 <span class="arithmatex">\(O(n^2)\)</span> 时间,因此是“自适应排序”。</p>
<p><strong>空间复杂度 <span class="arithmatex">\(O(n + k)\)</span></strong> :需要借助 <span class="arithmatex">\(k\)</span> 个桶和共 <span class="arithmatex">\(n\)</span> 个元素的额外空间,是“非原地排序”。</p>
<p>桶排序是否稳定取决于排序桶内元素的算法是否稳定。</p>
<h2 id="1173">11.7.3. &nbsp; 如何实现平均分配<a class="headerlink" href="#1173" title="Permanent link">&para;</a></h2>
<p>桶排序的时间复杂度理论上可以达到 <span class="arithmatex">\(O(n)\)</span> <strong>难点是需要将元素均匀分配到各个桶中</strong>,因为现实中的数据往往都不是均匀分布的。举个例子,假设我们想要把淘宝的所有商品根据价格范围平均分配到 10 个桶中然而商品价格不是均匀分布的100 元以下非常多、1000 元以上非常少;如果我们将价格区间平均划为 10 份,那么各个桶内的商品数量差距会非常大。</p>
<p>为了实现平均分配,我们可以先大致设置一个分界线,将数据粗略分到 3 个桶,分配完后,<strong>再把商品较多的桶继续划分为 3 个桶,直至所有桶内元素数量大致平均为止</strong>。此方法本质上是生成一个递归树,让叶结点的值尽量平均。当然,不一定非要划分为 3 个桶,可以根据数据特点灵活选取。</p>
<p><img alt="递归划分桶" src="../bucket_sort.assets/scatter_in_buckets_recursively.png" /></p>
<p align="center"> Fig. 递归划分桶 </p>
<p>如果我们提前知道商品价格的概率分布,<strong>那么也可以根据数据概率分布来设置每个桶的价格分界线</strong>。注意,数据分布不一定需要特意去统计,也可以根据数据特点采用某种概率模型来近似。如下图所示,我们假设商品价格服从正态分布,就可以合理设置价格区间,将商品平均分配到各个桶中。</p>
<p><img alt="根据概率分布划分桶" src="../bucket_sort.assets/scatter_in_buckets_distribution.png" /></p>
<p align="center"> Fig. 根据概率分布划分桶 </p>
<h2 id="__comments">评论</h2>
<!-- Insert generated snippet here -->
<script
src="https://giscus.app/client.js"
data-repo="krahets/hello-algo"
data-repo-id="R_kgDOIXtSqw"
data-category="Announcements"
data-category-id="DIC_kwDOIXtSq84CSZk_"
data-mapping="pathname"
data-strict="1"
data-reactions-enabled="1"
data-emit-metadata="0"
data-input-position="bottom"
data-theme="preferred_color_scheme"
data-lang="zh-CN"
crossorigin="anonymous"
async
>
</script>
<!-- Synchronize Giscus theme with palette -->
<script>
var giscus = document.querySelector("script[src*=giscus]")
/* Set palette on initial load */
var palette = __md_get("__palette")
if (palette && typeof palette.color === "object") {
var theme = palette.color.scheme === "slate" ? "dark" : "light"
giscus.setAttribute("data-theme", theme)
}
/* Register event handlers after documented loaded */
document.addEventListener("DOMContentLoaded", function() {
var ref = document.querySelector("[data-md-component=palette]")
ref.addEventListener("change", function() {
var palette = __md_get("__palette")
if (palette && typeof palette.color === "object") {
var theme = palette.color.scheme === "slate" ? "dark" : "light"
/* Instruct Giscus to change theme */
var frame = document.querySelector(".giscus-frame")
frame.contentWindow.postMessage(
{ giscus: { setConfig: { theme } } },
"https://giscus.app"
)
}
})
})
</script>
</article>
@ -1670,6 +1911,42 @@
<footer class="md-footer">
<nav class="md-footer__inner md-grid" aria-label="页脚" >
<a href="../counting_sort/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 11.6. &amp;nbsp; 计数排序New" rel="prev">
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
<div class="md-footer__title">
<div class="md-ellipsis">
<span class="md-footer__direction">
上一页
</span>
11.6. &nbsp; 计数排序New
</div>
</div>
</a>
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 11.8. &amp;nbsp; 小结" rel="next">
<div class="md-footer__title">
<div class="md-ellipsis">
<span class="md-footer__direction">
下一页
</span>
11.8. &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
</div>
</a>
</nav>
<div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid">

@ -18,7 +18,7 @@
<link rel="prev" href="../merge_sort/">
<link rel="next" href="../summary/">
<link rel="next" href="../bucket_sort/">
<link rel="icon" href="../../assets/images/favicon.png">
<meta name="generator" content="mkdocs-1.4.2, mkdocs-material-9.1.1">
@ -1360,6 +1360,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1525,9 +1527,23 @@
<li class="md-nav__item">
<a href="../bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>
@ -2227,13 +2243,13 @@
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 11.7. &amp;nbsp; 小结" rel="next">
<a href="../bucket_sort/" class="md-footer__link md-footer__link--next" aria-label="下一页: 11.7. &amp;nbsp; 桶排序New" rel="next">
<div class="md-footer__title">
<div class="md-ellipsis">
<span class="md-footer__direction">
下一页
</span>
11.7. &nbsp; 小结
11.7. &nbsp; 桶排序New
</div>
</div>
<div class="md-footer__button md-icon">

@ -1360,6 +1360,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1518,9 +1520,23 @@
<li class="md-nav__item">
<a href="../bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1360,6 +1360,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1545,9 +1547,23 @@
<li class="md-nav__item">
<a href="../bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1360,6 +1360,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1518,9 +1520,23 @@
<li class="md-nav__item">
<a href="../bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1360,6 +1360,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1532,9 +1534,23 @@
<li class="md-nav__item">
<a href="../bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>
@ -2171,7 +2187,8 @@
</div>
</div>
<h2 id="1142">11.4.2. &nbsp; 算法特性<a class="headerlink" href="#1142" title="Permanent link">&para;</a></h2>
<p><strong>时间复杂度 <span class="arithmatex">\(O(n \log n)\)</span></strong> :平均情况下,哨兵划分的递归层数为 <span class="arithmatex">\(\log n\)</span> ,每层中的总循环数为 <span class="arithmatex">\(n\)</span> ,总体使用 <span class="arithmatex">\(O(n \log n)\)</span> 时间。最差情况下,每轮哨兵划分操作都将长度为 <span class="arithmatex">\(n\)</span> 的数组划分为长度为 <span class="arithmatex">\(0\)</span><span class="arithmatex">\(n - 1\)</span> 的两个子数组,此时递归层数达到 <span class="arithmatex">\(n\)</span> 层,每层中的循环数为 <span class="arithmatex">\(n\)</span> ,总体使用 <span class="arithmatex">\(O(n^2)\)</span> 时间,因此是“非稳定排序”。</p>
<p><strong>时间复杂度 <span class="arithmatex">\(O(n \log n)\)</span></strong> :平均情况下,哨兵划分的递归层数为 <span class="arithmatex">\(\log n\)</span> ,每层中的总循环数为 <span class="arithmatex">\(n\)</span> ,总体使用 <span class="arithmatex">\(O(n \log n)\)</span> 时间。</p>
<p>最差情况下,每轮哨兵划分操作都将长度为 <span class="arithmatex">\(n\)</span> 的数组划分为长度为 <span class="arithmatex">\(0\)</span><span class="arithmatex">\(n - 1\)</span> 的两个子数组,此时递归层数达到 <span class="arithmatex">\(n\)</span> 层,每层中的循环数为 <span class="arithmatex">\(n\)</span> ,总体使用 <span class="arithmatex">\(O(n^2)\)</span> 时间,因此是“非稳定排序”。</p>
<p><strong>空间复杂度 <span class="arithmatex">\(O(n)\)</span></strong> :输入数组完全倒序下,达到最差递归深度 <span class="arithmatex">\(n\)</span> 。由于未借助辅助数组空间,因此是“原地排序”。</p>
<p><strong>非稳定排序</strong>:哨兵划分最后一步可能会将基准数交换至相等元素的右边。</p>
<h2 id="1143">11.4.3. &nbsp; 快排为什么快?<a class="headerlink" href="#1143" title="Permanent link">&para;</a></h2>

@ -15,7 +15,7 @@
<link rel="canonical" href="https://www.hello-algo.com/chapter_sorting/summary/">
<link rel="prev" href="../counting_sort/">
<link rel="prev" href="../bucket_sort/">
<link rel="next" href="../../chapter_appendix/installation/">
@ -25,7 +25,7 @@
<title>11.7.   小结 - Hello 算法</title>
<title>11.8.   小结 - Hello 算法</title>
@ -79,7 +79,7 @@
<div data-md-component="skip">
<a href="#117" class="md-skip">
<a href="#118" class="md-skip">
跳转至
</a>
@ -113,7 +113,7 @@
<div class="md-header__topic" data-md-component="header-topic">
<span class="md-ellipsis">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</span>
</div>
@ -1360,6 +1360,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1462,6 +1464,20 @@
<li class="md-nav__item">
<a href="../bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
@ -1474,7 +1490,7 @@
<a href="./" class="md-nav__link md-nav__link--active">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>
@ -1647,7 +1663,7 @@
<h1 id="117">11.7. &nbsp; 小结<a class="headerlink" href="#117" title="Permanent link">&para;</a></h1>
<h1 id="118">11.8. &nbsp; 小结<a class="headerlink" href="#118" title="Permanent link">&para;</a></h1>
<ul>
<li>冒泡排序通过交换相邻元素来实现排序。通过增加标志位实现提前返回,我们可将冒泡排序的最佳时间复杂度优化至 <span class="arithmatex">\(O(N)\)</span></li>
<li>插入排序每轮将待排序区间内元素插入至已排序区间的正确位置,从而实现排序。插入排序的时间复杂度虽为 <span class="arithmatex">\(O(N^2)\)</span> ,但因为总体操作少而很受欢迎,一般用于小数据量的排序工作。</li>
@ -1738,7 +1754,7 @@
<nav class="md-footer__inner md-grid" aria-label="页脚" >
<a href="../counting_sort/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 11.6. &amp;nbsp; 计数排序New" rel="prev">
<a href="../bucket_sort/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 11.7. &amp;nbsp; 桶排序New" rel="prev">
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
@ -1747,7 +1763,7 @@
<span class="md-footer__direction">
上一页
</span>
11.6. &nbsp; 计数排序New
11.7. &nbsp;排序New
</div>
</div>
</a>

@ -1435,6 +1435,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1538,9 +1540,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1442,6 +1442,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1545,9 +1547,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1469,6 +1469,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1572,9 +1574,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1370,6 +1370,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1473,9 +1475,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1510,6 +1510,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1613,9 +1615,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1456,6 +1456,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1559,9 +1561,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1463,6 +1463,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1566,9 +1568,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1448,6 +1448,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1551,9 +1553,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1415,6 +1415,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1518,9 +1520,23 @@
<li class="md-nav__item">
<a href="../../chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

@ -1351,6 +1351,8 @@
<label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
@ -1454,9 +1456,23 @@
<li class="md-nav__item">
<a href="chapter_sorting/bucket_sort/" class="md-nav__link">
11.7. &nbsp; 桶排序New
</a>
</li>
<li class="md-nav__item">
<a href="chapter_sorting/summary/" class="md-nav__link">
11.7. &nbsp; 小结
11.8. &nbsp; 小结
</a>
</li>

File diff suppressed because one or more lines are too long

Binary file not shown.
Loading…
Cancel
Save