mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-22 03:16:08 +00:00
deploy
This commit is contained in:
@@ -1142,14 +1142,6 @@
|
||||
4.4 内存与缓存 *
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
|
||||
@@ -1142,14 +1142,6 @@
|
||||
4.4 内存与缓存 *
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -4100,8 +4092,7 @@
|
||||
<h2 id="1182">11.8.2 算法特性<a class="headerlink" href="#1182" title="Permanent link">¶</a></h2>
|
||||
<p>桶排序适用于处理体量很大的数据。例如,输入数据包含 100 万个元素,由于空间限制,系统内存无法一次性加载所有数据。此时,可以将数据分成 1000 个桶,然后分别对每个桶进行排序,最后将结果合并。</p>
|
||||
<ul>
|
||||
<li><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">\(O(n + k)\)</span> 时间。</li>
|
||||
<li><strong>自适应排序</strong>:在最差情况下,所有数据被分配到一个桶中,且排序该桶使用 <span class="arithmatex">\(O(n^2)\)</span> 时间。</li>
|
||||
<li><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">\(O(n + k)\)</span> 时间。在最差情况下,所有数据被分配到一个桶中,且排序该桶使用 <span class="arithmatex">\(O(n^2)\)</span> 时间。</li>
|
||||
<li><strong>空间复杂度为 <span class="arithmatex">\(O(n + k)\)</span>、非原地排序</strong>:需要借助 <span class="arithmatex">\(k\)</span> 个桶和总共 <span class="arithmatex">\(n\)</span> 个元素的额外空间。</li>
|
||||
<li>桶排序是否稳定取决于排序桶内元素的算法是否稳定。</li>
|
||||
</ul>
|
||||
|
||||
@@ -1142,14 +1142,6 @@
|
||||
4.4 内存与缓存 *
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
|
||||
@@ -1142,14 +1142,6 @@
|
||||
4.4 内存与缓存 *
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
|
||||
@@ -1142,14 +1142,6 @@
|
||||
4.4 内存与缓存 *
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
|
||||
@@ -1142,14 +1142,6 @@
|
||||
4.4 内存与缓存 *
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
|
||||
@@ -1142,14 +1142,6 @@
|
||||
4.4 内存与缓存 *
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
|
||||
@@ -1142,14 +1142,6 @@
|
||||
4.4 内存与缓存 *
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -4313,7 +4305,7 @@
|
||||
</details>
|
||||
<h2 id="1152">11.5.2 算法特性<a class="headerlink" href="#1152" title="Permanent link">¶</a></h2>
|
||||
<ul>
|
||||
<li><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> 时间。</li>
|
||||
<li><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> 时间。</li>
|
||||
<li><strong>空间复杂度为 <span class="arithmatex">\(O(n)\)</span>、原地排序</strong>:在输入数组完全倒序的情况下,达到最差递归深度 <span class="arithmatex">\(n\)</span> ,使用 <span class="arithmatex">\(O(n)\)</span> 栈帧空间。排序操作是在原数组上进行的,未借助额外数组。</li>
|
||||
<li><strong>非稳定排序</strong>:在哨兵划分的最后一步,基准数可能会被交换至相等元素的右侧。</li>
|
||||
</ul>
|
||||
|
||||
@@ -1142,14 +1142,6 @@
|
||||
4.4 内存与缓存 *
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
|
||||
@@ -1142,14 +1142,6 @@
|
||||
4.4 内存与缓存 *
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
|
||||
@@ -1142,14 +1142,6 @@
|
||||
4.4 内存与缓存 *
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3691,11 +3683,10 @@
|
||||
<a id="__codelineno-0-15" name="__codelineno-0-15" href="#__codelineno-0-15"></a><span class="w"> </span><span class="o">(</span><span class="s1">'C'</span>,<span class="w"> </span><span class="m">21</span><span class="o">)</span>
|
||||
<a id="__codelineno-0-16" name="__codelineno-0-16" href="#__codelineno-0-16"></a><span class="w"> </span><span class="o">(</span><span class="s1">'E'</span>,<span class="w"> </span><span class="m">23</span><span class="o">)</span>
|
||||
</code></pre></div>
|
||||
<p><strong>自适应性</strong>:<u>自适应排序</u>的时间复杂度会受输入数据的影响,即最佳时间复杂度、最差时间复杂度、平均时间复杂度并不完全相等。</p>
|
||||
<p>自适应性需要根据具体情况来评估。如果最差时间复杂度差于平均时间复杂度,说明排序算法在某些数据下性能可能劣化,因此被视为负面属性;而如果最佳时间复杂度优于平均时间复杂度,则被视为正面属性。</p>
|
||||
<p><strong>自适应性</strong>:<u>自适应排序</u>能够利用输入数据已有的顺序信息来减少计算量,达到更优的时间效率。自适应排序算法的最佳时间复杂度通常优于平均时间复杂度。</p>
|
||||
<p><strong>是否基于比较</strong>:<u>基于比较的排序</u>依赖比较运算符(<span class="arithmatex">\(<\)</span>、<span class="arithmatex">\(=\)</span>、<span class="arithmatex">\(>\)</span>)来判断元素的相对顺序,从而排序整个数组,理论最优时间复杂度为 <span class="arithmatex">\(O(n \log n)\)</span> 。而<u>非比较排序</u>不使用比较运算符,时间复杂度可达 <span class="arithmatex">\(O(n)\)</span> ,但其通用性相对较差。</p>
|
||||
<h2 id="1112">11.1.2 理想排序算法<a class="headerlink" href="#1112" title="Permanent link">¶</a></h2>
|
||||
<p><strong>运行快、原地、稳定、正向自适应、通用性好</strong>。显然,迄今为止尚未发现兼具以上所有特性的排序算法。因此,在选择排序算法时,需要根据具体的数据特点和问题需求来决定。</p>
|
||||
<p><strong>运行快、原地、稳定、自适应、通用性好</strong>。显然,迄今为止尚未发现兼具以上所有特性的排序算法。因此,在选择排序算法时,需要根据具体的数据特点和问题需求来决定。</p>
|
||||
<p>接下来,我们将共同学习各种排序算法,并基于上述评价维度对各个排序算法的优缺点进行分析。</p>
|
||||
|
||||
<!-- Source file information -->
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 58 KiB |
@@ -1142,14 +1142,6 @@
|
||||
4.4 内存与缓存 *
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3673,7 +3665,7 @@
|
||||
<li>桶排序包含三个步骤:数据分桶、桶内排序和合并结果。它同样体现了分治策略,适用于数据体量很大的情况。桶排序的关键在于对数据进行平均分配。</li>
|
||||
<li>计数排序是桶排序的一个特例,它通过统计数据出现的次数来实现排序。计数排序适用于数据量大但数据范围有限的情况,并且要求数据能够转换为正整数。</li>
|
||||
<li>基数排序通过逐位排序来实现数据排序,要求数据能够表示为固定位数的数字。</li>
|
||||
<li>总的来说,我们希望找到一种排序算法,具有高效率、稳定、原地以及正向自适应性等优点。然而,正如其他数据结构和算法一样,没有一种排序算法能够同时满足所有这些条件。在实际应用中,我们需要根据数据的特性来选择合适的排序算法。</li>
|
||||
<li>总的来说,我们希望找到一种排序算法,具有高效率、稳定、原地以及自适应性等优点。然而,正如其他数据结构和算法一样,没有一种排序算法能够同时满足所有这些条件。在实际应用中,我们需要根据数据的特性来选择合适的排序算法。</li>
|
||||
<li>图 11-19 对比了主流排序算法的效率、稳定性、就地性和自适应性等。</li>
|
||||
</ul>
|
||||
<p><a class="glightbox" href="../summary.assets/sorting_algorithms_comparison.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="排序算法对比" class="animation-figure" src="../summary.assets/sorting_algorithms_comparison.png" /></a></p>
|
||||
|
||||
Reference in New Issue
Block a user