This commit is contained in:
krahets
2024-04-22 01:45:11 +08:00
parent 98c2e30a0d
commit e730b2dd04
14 changed files with 318 additions and 279 deletions

View File

@@ -4512,7 +4512,7 @@
</details>
<h2 id="1193">11.9.3 &nbsp; 算法特性<a class="headerlink" href="#1193" title="Permanent link">&para;</a></h2>
<ul>
<li><strong>时间复杂度为 <span class="arithmatex">\(O(n + m)\)</span></strong> :涉及遍历 <code>nums</code> 和遍历 <code>counter</code> ,都使用线性时间。一般情况下 <span class="arithmatex">\(n \gg m\)</span> ,时间复杂度趋于 <span class="arithmatex">\(O(n)\)</span></li>
<li><strong>时间复杂度为 <span class="arithmatex">\(O(n + m)\)</span>、非自适应排序</strong> :涉及遍历 <code>nums</code> 和遍历 <code>counter</code> ,都使用线性时间。一般情况下 <span class="arithmatex">\(n \gg m\)</span> ,时间复杂度趋于 <span class="arithmatex">\(O(n)\)</span></li>
<li><strong>空间复杂度为 <span class="arithmatex">\(O(n + m)\)</span>、非原地排序</strong>:借助了长度分别为 <span class="arithmatex">\(n\)</span><span class="arithmatex">\(m\)</span> 的数组 <code>res</code><code>counter</code></li>
<li><strong>稳定排序</strong>:由于向 <code>res</code> 中填充元素的顺序是“从右向左”的,因此倒序遍历 <code>nums</code> 可以避免改变相等元素之间的相对位置,从而实现稳定排序。实际上,正序遍历 <code>nums</code> 也可以得到正确的排序结果,但结果是非稳定的。</li>
</ul>

View File

@@ -4388,7 +4388,7 @@ x_k = \lfloor\frac{x}{d^{k-1}}\rfloor \bmod d
<h2 id="11102">11.10.2 &nbsp; 算法特性<a class="headerlink" href="#11102" title="Permanent link">&para;</a></h2>
<p>相较于计数排序,基数排序适用于数值范围较大的情况,<strong>但前提是数据必须可以表示为固定位数的格式,且位数不能过大</strong>。例如,浮点数不适合使用基数排序,因为其位数 <span class="arithmatex">\(k\)</span> 过大,可能导致时间复杂度 <span class="arithmatex">\(O(nk) \gg O(n^2)\)</span></p>
<ul>
<li><strong>时间复杂度为 <span class="arithmatex">\(O(nk)\)</span></strong>:设数据量为 <span class="arithmatex">\(n\)</span>、数据为 <span class="arithmatex">\(d\)</span> 进制、最大位数为 <span class="arithmatex">\(k\)</span> ,则对某一位执行计数排序使用 <span class="arithmatex">\(O(n + d)\)</span> 时间,排序所有 <span class="arithmatex">\(k\)</span> 位使用 <span class="arithmatex">\(O((n + d)k)\)</span> 时间。通常情况下,<span class="arithmatex">\(d\)</span><span class="arithmatex">\(k\)</span> 都相对较小,时间复杂度趋向 <span class="arithmatex">\(O(n)\)</span></li>
<li><strong>时间复杂度为 <span class="arithmatex">\(O(nk)\)</span>、非自适应排序</strong>:设数据量为 <span class="arithmatex">\(n\)</span>、数据为 <span class="arithmatex">\(d\)</span> 进制、最大位数为 <span class="arithmatex">\(k\)</span> ,则对某一位执行计数排序使用 <span class="arithmatex">\(O(n + d)\)</span> 时间,排序所有 <span class="arithmatex">\(k\)</span> 位使用 <span class="arithmatex">\(O((n + d)k)\)</span> 时间。通常情况下,<span class="arithmatex">\(d\)</span><span class="arithmatex">\(k\)</span> 都相对较小,时间复杂度趋向 <span class="arithmatex">\(O(n)\)</span></li>
<li><strong>空间复杂度为 <span class="arithmatex">\(O(n + d)\)</span>、非原地排序</strong>:与计数排序相同,基数排序需要借助长度为 <span class="arithmatex">\(n\)</span><span class="arithmatex">\(d\)</span> 的数组 <code>res</code><code>counter</code></li>
<li><strong>稳定排序</strong>:当计数排序稳定时,基数排序也稳定;当计数排序不稳定时,基数排序无法保证得到正确的排序结果。</li>
</ul>