mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-31 14:23:30 +08:00
deploy
This commit is contained in:
@ -1765,20 +1765,20 @@
|
||||
<p>理想情况下,哈希函数应该为每个输入产生唯一的输出,使得 key 和 value 一一对应。而实际上,往往存在向哈希函数输入不同的 key 而产生相同输出的情况,这种情况被称为「哈希冲突 Hash Collision」。哈希冲突会导致查询结果错误,从而严重影响哈希表的可用性。</p>
|
||||
<p>那么,为什么会出现哈希冲突呢?本质上看,<strong>由于哈希函数的输入空间往往远大于输出空间</strong>,因此不可避免地会出现多个输入产生相同输出的情况,即为哈希冲突。比如,输入空间是全体整数,输出空间是一个固定大小的数组,那么必定会有多个整数映射到同一个数组索引。</p>
|
||||
<p>为了缓解哈希冲突,一方面,<strong>我们可以通过哈希表扩容来减小冲突概率</strong>。极端情况下,当输入空间和输出空间大小相等时,哈希表就等价于数组了,每个 key 都对应唯一的数组索引,可谓“大力出奇迹”。</p>
|
||||
<p>另一方面,<strong>考虑通过优化哈希表的表示来缓解哈希冲突</strong>,常见的方法有「链式地址」和「开放寻址」。</p>
|
||||
<p>另一方面,<strong>考虑通过优化哈希表的表示来缓解哈希冲突</strong>,常见的方法有「链式地址 Separate Chaining」和「开放寻址 Open Addressing」。</p>
|
||||
<h2 id="621">6.2.1. 哈希表扩容<a class="headerlink" href="#621" title="Permanent link">¶</a></h2>
|
||||
<p>哈希函数的最后一步往往是对桶数量 <span class="arithmatex">\(n\)</span> 取余,以将哈希值映射到桶的索引范围,从而将 key 放入对应的桶中。当哈希表容量越大(即 <span class="arithmatex">\(n\)</span> 越大)时,多个 key 被分配到同一个桶中的概率就越低,冲突就越少。</p>
|
||||
<p>因此,<strong>在哈希表内的冲突整体比较严重时,编程语言一般通过扩容哈希表来缓解</strong>。与数组扩容类似,哈希表扩容需要将所有键值对从原哈希表移动至新哈希表,<strong>开销很大</strong>。</p>
|
||||
<p>编程语言一般使用「负载因子 Load Factor」来评估哈希冲突的严重程度,<strong>其定义为哈希表中元素数量除以桶数量</strong>,常用作哈希表扩容的触发条件。比如在 Java 中,当负载因子 <span class="arithmatex">\(> 0.75\)</span> 时,系统会将 HashMap 容量扩充至原先的 <span class="arithmatex">\(2\)</span> 倍。</p>
|
||||
<h2 id="622">6.2.2. 链式地址<a class="headerlink" href="#622" title="Permanent link">¶</a></h2>
|
||||
<p>在原始哈希表中,每个桶只能存储一个元素(即键值对)。<strong>考虑将单个元素转化成一个链表,将所有冲突元素都存储在一个链表中</strong>。</p>
|
||||
<p>在原始哈希表中,每个桶只能存储一个键值对。<strong>链式地址考虑将单个元素转化成一个链表,将键值对作为链表结点,将所有冲突键值对都存储在一个链表中</strong>。</p>
|
||||
<p><img alt="链式地址" src="../hash_collision.assets/hash_collision_chaining.png" /></p>
|
||||
<p align="center"> Fig. 链式地址 </p>
|
||||
|
||||
<p>链式地址下,哈希表操作方法为:</p>
|
||||
<ul>
|
||||
<li><strong>查询元素</strong>:输入 key ,经过哈希函数得到数组索引,即可访问链表头结点,再通过遍历链表并对比 key 来查找键值对。</li>
|
||||
<li><strong>添加元素</strong>:先通过哈希函数访问链表头部,再将结点(即键值对)添加到链表头部即可。</li>
|
||||
<li><strong>添加元素</strong>:先通过哈希函数访问链表头结点,再将结点(即键值对)添加到链表即可。</li>
|
||||
<li><strong>删除元素</strong>:同样先根据哈希函数结果访问链表头部,再遍历链表查找对应结点,删除之即可。</li>
|
||||
</ul>
|
||||
<p>链式地址虽然解决了哈希冲突问题,但仍存在局限性,包括:</p>
|
||||
|
@ -1722,13 +1722,13 @@
|
||||
|
||||
|
||||
<h1 id="112">11.2. 冒泡排序<a class="headerlink" href="#112" title="Permanent link">¶</a></h1>
|
||||
<p>「冒泡排序 Bubble Sort」是一种最基础的排序算法,非常适合作为第一个学习的排序算法。顾名思义,「冒泡」是该算法的核心操作。</p>
|
||||
<p>「冒泡排序 Bubble Sort」是一种基于元素交换实现排序的算法,非常适合作为第一个学习的排序算法。</p>
|
||||
<div class="admonition question">
|
||||
<p class="admonition-title">为什么叫“冒泡”</p>
|
||||
<p>在水中,越大的泡泡浮力越大,所以最大的泡泡会最先浮到水面。</p>
|
||||
</div>
|
||||
<p>「冒泡」操作则是在模拟上述过程,具体做法为:从数组最左端开始向右遍历,依次对比相邻元素大小,若 <strong>左元素 > 右元素</strong> 则将它俩交换,最终可将最大元素移动至数组最右端。</p>
|
||||
<p>完成此次冒泡操作后,<strong>数组最大元素已在正确位置,接下来只需排序剩余 <span class="arithmatex">\(n - 1\)</span> 个元素</strong>。</p>
|
||||
<p>「冒泡操作」则是在模拟上述过程,具体做法为:从数组最左端开始向右遍历,依次对比相邻元素大小,若“左元素 > 右元素”则将它俩交换,最终可将最大元素移动至数组最右端。</p>
|
||||
<p>完成一次冒泡操作后,<strong>数组最大元素已在正确位置,接下来只需排序剩余 <span class="arithmatex">\(n - 1\)</span> 个元素</strong>。</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="1:7"><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" /><div class="tabbed-labels"><label for="__tabbed_1_1"><1></label><label for="__tabbed_1_2"><2></label><label for="__tabbed_1_3"><3></label><label for="__tabbed_1_4"><4></label><label for="__tabbed_1_5"><5></label><label for="__tabbed_1_6"><6></label><label for="__tabbed_1_7"><7></label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
@ -1755,10 +1755,11 @@
|
||||
</div>
|
||||
</div>
|
||||
<h2 id="1121">11.2.1. 算法流程<a class="headerlink" href="#1121" title="Permanent link">¶</a></h2>
|
||||
<p>设输入数组长度为 <span class="arithmatex">\(n\)</span> ,循环执行「冒泡」操作:</p>
|
||||
<ol>
|
||||
<li>设数组长度为 <span class="arithmatex">\(n\)</span> ,完成第一轮「冒泡」后,数组最大元素已在正确位置,接下来只需排序剩余 <span class="arithmatex">\(n - 1\)</span> 个元素。</li>
|
||||
<li>同理,对剩余 <span class="arithmatex">\(n - 1\)</span> 个元素执行「冒泡」,可将第二大元素交换至正确位置,因而待排序元素只剩 <span class="arithmatex">\(n - 2\)</span> 个。</li>
|
||||
<li>以此类推…… <strong>循环 <span class="arithmatex">\(n - 1\)</span> 轮「冒泡」,即可完成整个数组的排序</strong>。</li>
|
||||
<li>完成第一轮「冒泡」后,数组最大元素已在正确位置,接下来只需排序剩余 <span class="arithmatex">\(n - 1\)</span> 个元素;</li>
|
||||
<li>对剩余 <span class="arithmatex">\(n - 1\)</span> 个元素执行「冒泡」,可将第二大元素交换至正确位置,因而待排序元素只剩 <span class="arithmatex">\(n - 2\)</span> 个;</li>
|
||||
<li>以此类推…… <strong>循环 <span class="arithmatex">\(n - 1\)</span> 轮「冒泡」,即可完成整个数组的排序</strong>;</li>
|
||||
</ol>
|
||||
<p><img alt="冒泡排序流程" src="../bubble_sort.assets/bubble_sort_overview.png" /></p>
|
||||
<p align="center"> Fig. 冒泡排序流程 </p>
|
||||
@ -1932,14 +1933,12 @@
|
||||
</div>
|
||||
</div>
|
||||
<h2 id="1122">11.2.2. 算法特性<a class="headerlink" href="#1122" title="Permanent link">¶</a></h2>
|
||||
<p><strong>时间复杂度 <span class="arithmatex">\(O(n^2)\)</span></strong> :各轮「冒泡」遍历的数组长度为 <span class="arithmatex">\(n - 1\)</span> , <span class="arithmatex">\(n - 2\)</span> , <span class="arithmatex">\(\cdots\)</span> , <span class="arithmatex">\(2\)</span> , <span class="arithmatex">\(1\)</span> 次,求和为 <span class="arithmatex">\(\frac{(n - 1) n}{2}\)</span> ,因此使用 <span class="arithmatex">\(O(n^2)\)</span> 时间。</p>
|
||||
<p><strong>空间复杂度 <span class="arithmatex">\(O(1)\)</span></strong> :指针 <span class="arithmatex">\(i\)</span> , <span class="arithmatex">\(j\)</span> 使用常数大小的额外空间。</p>
|
||||
<p><strong>原地排序</strong>:指针变量仅使用常数大小额外空间。</p>
|
||||
<p><strong>稳定排序</strong>:不交换相等元素。</p>
|
||||
<p><strong>自适应排序</strong>:引入 <code>flag</code> 优化后(见下文),最佳时间复杂度为 <span class="arithmatex">\(O(N)\)</span> 。</p>
|
||||
<p><strong>时间复杂度 <span class="arithmatex">\(O(n^2)\)</span></strong> :各轮冒泡遍历的数组长度为 <span class="arithmatex">\(n - 1\)</span> , <span class="arithmatex">\(n - 2\)</span> , <span class="arithmatex">\(\cdots\)</span> , <span class="arithmatex">\(2\)</span> , <span class="arithmatex">\(1\)</span> 次,求和为 <span class="arithmatex">\(\frac{(n - 1) n}{2}\)</span> ,因此使用 <span class="arithmatex">\(O(n^2)\)</span> 时间。引入下文的 <code>flag</code> 优化后,最佳时间复杂度可以达到 <span class="arithmatex">\(O(N)\)</span> ,因此是“自适应排序”。</p>
|
||||
<p><strong>空间复杂度 <span class="arithmatex">\(O(1)\)</span></strong> :指针 <span class="arithmatex">\(i\)</span> , <span class="arithmatex">\(j\)</span> 使用常数大小的额外空间,因此是“原地排序”。</p>
|
||||
<p>在冒泡操作中遇到相等元素不交换,因此是“稳定排序”。</p>
|
||||
<h2 id="1123">11.2.3. 效率优化<a class="headerlink" href="#1123" title="Permanent link">¶</a></h2>
|
||||
<p>我们发现,若在某轮「冒泡」中未执行任何交换操作,则说明数组已经完成排序,可直接返回结果。考虑可以增加一个标志位 <code>flag</code> 来监听该情况,若出现则直接返回。</p>
|
||||
<p>优化后,冒泡排序的最差和平均时间复杂度仍为 <span class="arithmatex">\(O(n^2)\)</span> ;而在输入数组 <strong>已排序</strong> 时,达到 <strong>最佳时间复杂度</strong> <span class="arithmatex">\(O(n)\)</span> 。 </p>
|
||||
<p>优化后,冒泡排序的最差和平均时间复杂度仍为 <span class="arithmatex">\(O(n^2)\)</span> ;而在输入数组完全有序时,达到最佳时间复杂度 <span class="arithmatex">\(O(n)\)</span> 。 </p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="3:10"><input checked="checked" id="__tabbed_3_1" name="__tabbed_3" type="radio" /><input id="__tabbed_3_2" name="__tabbed_3" type="radio" /><input id="__tabbed_3_3" name="__tabbed_3" type="radio" /><input id="__tabbed_3_4" name="__tabbed_3" type="radio" /><input id="__tabbed_3_5" name="__tabbed_3" type="radio" /><input id="__tabbed_3_6" name="__tabbed_3" type="radio" /><input id="__tabbed_3_7" name="__tabbed_3" type="radio" /><input id="__tabbed_3_8" name="__tabbed_3" type="radio" /><input id="__tabbed_3_9" name="__tabbed_3" type="radio" /><input id="__tabbed_3_10" name="__tabbed_3" type="radio" /><div class="tabbed-labels"><label for="__tabbed_3_1">Java</label><label for="__tabbed_3_2">C++</label><label for="__tabbed_3_3">Python</label><label for="__tabbed_3_4">Go</label><label for="__tabbed_3_5">JavaScript</label><label for="__tabbed_3_6">TypeScript</label><label for="__tabbed_3_7">C</label><label for="__tabbed_3_8">C#</label><label for="__tabbed_3_9">Swift</label><label for="__tabbed_3_10">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
|
@ -2128,14 +2128,8 @@
|
||||
</div>
|
||||
<h2 id="1163">11.6.3. 算法特性<a class="headerlink" href="#1163" title="Permanent link">¶</a></h2>
|
||||
<p><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> 时间。</p>
|
||||
<p><strong>空间复杂度 <span class="arithmatex">\(O(n + m)\)</span></strong> :数组 <code>res</code> 和 <code>counter</code> 长度分别为 <span class="arithmatex">\(n\)</span> , <span class="arithmatex">\(m\)</span> 。</p>
|
||||
<p><strong>非原地排序</strong>:借助了辅助数组 <code>counter</code> 和结果数组 <code>res</code> 的额外空间。</p>
|
||||
<p><strong>稳定排序</strong>:倒序遍历 <code>nums</code> 保持了相等元素的相对位置。</p>
|
||||
<p><strong>非自适应排序</strong>:与元素分布无关。</p>
|
||||
<div class="admonition question">
|
||||
<p class="admonition-title">为什么是稳定排序?</p>
|
||||
<p>由于向 <code>res</code> 中填充元素的顺序是“从右向左”的,因此倒序遍历 <code>nums</code> 可以避免改变相等元素之间的相对位置,从而实现“稳定排序”;其实正序遍历 <code>nums</code> 也可以得到正确的排序结果,但结果“非稳定”。</p>
|
||||
</div>
|
||||
<p><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> ,是“非原地排序”;</p>
|
||||
<p><strong>稳定排序</strong>:由于向 <code>res</code> 中填充元素的顺序是“从右向左”的,因此倒序遍历 <code>nums</code> 可以避免改变相等元素之间的相对位置,从而实现“稳定排序”;其实正序遍历 <code>nums</code> 也可以得到正确的排序结果,但结果“非稳定”。</p>
|
||||
<h2 id="1164">11.6.4. 局限性<a class="headerlink" href="#1164" title="Permanent link">¶</a></h2>
|
||||
<p>看到这里,你也许会觉得计数排序太妙了,咔咔一通操作,时间复杂度就下来了。然而,使用技术排序的前置条件比较苛刻。</p>
|
||||
<p><strong>计数排序只适用于非负整数</strong>。若想要用在其他类型数据上,则要求该数据必须可以被转化为非负整数,并且不能改变各个元素之间的相对大小关系。例如,对于包含负数的整数数组,可以先给所有数字加上一个常数,将全部数字转化为正数,排序完成后再转换回去即可。</p>
|
||||
|
@ -1729,10 +1729,11 @@
|
||||
<p align="center"> Fig. 单次插入操作 </p>
|
||||
|
||||
<h2 id="1131">11.3.1. 算法流程<a class="headerlink" href="#1131" title="Permanent link">¶</a></h2>
|
||||
<p>循环执行插入操作:</p>
|
||||
<ol>
|
||||
<li>第 1 轮先选取数组的 <strong>第 2 个元素</strong> 为 <code>base</code> ,执行「插入操作」后,<strong>数组前 2 个元素已完成排序</strong>。</li>
|
||||
<li>第 2 轮选取 <strong>第 3 个元素</strong> 为 <code>base</code> ,执行「插入操作」后,<strong>数组前 3 个元素已完成排序</strong>。</li>
|
||||
<li>以此类推……最后一轮选取 <strong>数组尾元素</strong> 为 <code>base</code> ,执行「插入操作」后,<strong>所有元素已完成排序</strong>。</li>
|
||||
<li>先选取数组的 <strong>第 2 个元素</strong> 为 <code>base</code> ,执行插入操作后,<strong>数组前 2 个元素已完成排序</strong>。</li>
|
||||
<li>选取 <strong>第 3 个元素</strong> 为 <code>base</code> ,执行插入操作后,<strong>数组前 3 个元素已完成排序</strong>。</li>
|
||||
<li>以此类推……最后一轮选取 <strong>数组尾元素</strong> 为 <code>base</code> ,执行插入操作后,<strong>所有元素已完成排序</strong>。</li>
|
||||
</ol>
|
||||
<p><img alt="插入排序流程" src="../insertion_sort.assets/insertion_sort_overview.png" /></p>
|
||||
<p align="center"> Fig. 插入排序流程 </p>
|
||||
@ -1895,23 +1896,21 @@
|
||||
</div>
|
||||
</div>
|
||||
<h2 id="1132">11.3.2. 算法特性<a class="headerlink" href="#1132" title="Permanent link">¶</a></h2>
|
||||
<p><strong>时间复杂度 <span class="arithmatex">\(O(n^2)\)</span></strong> :最差情况下,各轮插入操作循环 <span class="arithmatex">\(n - 1\)</span> , <span class="arithmatex">\(n-2\)</span> , <span class="arithmatex">\(\cdots\)</span> , <span class="arithmatex">\(2\)</span> , <span class="arithmatex">\(1\)</span> 次,求和为 <span class="arithmatex">\(\frac{(n - 1) n}{2}\)</span> ,使用 <span class="arithmatex">\(O(n^2)\)</span> 时间。</p>
|
||||
<p><strong>空间复杂度 <span class="arithmatex">\(O(1)\)</span></strong> :指针 <span class="arithmatex">\(i\)</span> , <span class="arithmatex">\(j\)</span> 使用常数大小的额外空间。</p>
|
||||
<p><strong>原地排序</strong>:指针变量仅使用常数大小额外空间。</p>
|
||||
<p><strong>稳定排序</strong>:不交换相等元素。</p>
|
||||
<p><strong>自适应排序</strong>:最佳情况下,时间复杂度为 <span class="arithmatex">\(O(n)\)</span> 。</p>
|
||||
<p><strong>时间复杂度 <span class="arithmatex">\(O(n^2)\)</span></strong> :最差情况下,各轮插入操作循环 <span class="arithmatex">\(n - 1\)</span> , <span class="arithmatex">\(n-2\)</span> , <span class="arithmatex">\(\cdots\)</span> , <span class="arithmatex">\(2\)</span> , <span class="arithmatex">\(1\)</span> 次,求和为 <span class="arithmatex">\(\frac{(n - 1) n}{2}\)</span> ,使用 <span class="arithmatex">\(O(n^2)\)</span> 时间。输入数组完全有序下,达到最佳时间复杂度 <span class="arithmatex">\(O(n)\)</span> ,因此是“自适应排序”。</p>
|
||||
<p><strong>空间复杂度 <span class="arithmatex">\(O(1)\)</span></strong> :指针 <span class="arithmatex">\(i\)</span> , <span class="arithmatex">\(j\)</span> 使用常数大小的额外空间,因此是“原地排序”。</p>
|
||||
<p>在插入操作中,我们会将元素插入到相等元素的右边,不会改变它们的次序,因此是“稳定排序”。</p>
|
||||
<h2 id="1133-vs">11.3.3. 插入排序 vs 冒泡排序<a class="headerlink" href="#1133-vs" title="Permanent link">¶</a></h2>
|
||||
<div class="admonition question">
|
||||
<p class="admonition-title">Question</p>
|
||||
<p>虽然「插入排序」和「冒泡排序」的时间复杂度皆为 <span class="arithmatex">\(O(n^2)\)</span> ,但实际运行速度却有很大差别,这是为什么呢?</p>
|
||||
</div>
|
||||
<p>回顾复杂度分析,两个方法的循环次数都是 <span class="arithmatex">\(\frac{(n - 1) n}{2}\)</span> 。但不同的是,「冒泡操作」是在做 <strong>元素交换</strong>,需要借助一个临时变量实现,共 3 个单元操作;而「插入操作」是在做 <strong>赋值</strong>,只需 1 个单元操作;因此,可以粗略估计出冒泡排序的计算开销约为插入排序的 3 倍。</p>
|
||||
<p>插入排序运行速度快,并且具有原地、稳定、自适应的优点,因此很受欢迎。实际上,包括 Java 在内的许多编程语言的排序库函数的实现都用到了插入排序。库函数的大致思路:</p>
|
||||
<p>回顾「冒泡排序」和「插入排序」的复杂度分析,两者的循环轮数都是 <span class="arithmatex">\(\frac{(n - 1) n}{2}\)</span> 。但不同的是:</p>
|
||||
<ul>
|
||||
<li>冒泡操作基于 <strong>元素交换</strong> 实现,需要借助一个临时变量实现,共 3 个单元操作;</li>
|
||||
<li>插入操作基于 <strong>元素赋值</strong> 实现,只需 1 个单元操作;</li>
|
||||
</ul>
|
||||
<p>因此,可以粗略估计出冒泡排序的计算开销约为插入排序的 3 倍,因此更受欢迎。实际上,许多编程语言(例如 Java)的内置排序函数都使用到了插入排序,大致思路为:</p>
|
||||
<ul>
|
||||
<li>对于 <strong>长数组</strong>,采用基于分治的排序算法,例如「快速排序」,时间复杂度为 <span class="arithmatex">\(O(n \log n)\)</span> ;</li>
|
||||
<li>对于 <strong>短数组</strong>,直接使用「插入排序」,时间复杂度为 <span class="arithmatex">\(O(n^2)\)</span> ;</li>
|
||||
</ul>
|
||||
<p>在数组较短时,复杂度中的常数项(即每轮中的单元操作数量)占主导作用,此时插入排序运行地更快。这个现象与「线性查找」和「二分查找」的情况类似。</p>
|
||||
<p><strong>在数据量较小时插入排序更快</strong>,这是因为复杂度中的常数项(即每轮中的单元操作数量)占主导作用。这个现象与「线性查找」和「二分查找」的情况类似。</p>
|
||||
|
||||
|
||||
|
||||
|
@ -2198,13 +2198,9 @@
|
||||
<li>判断 <code>tmp[i]</code> 和 <code>tmp[j]</code> 的大小的操作中,还 <strong>需考虑当子数组遍历完成后的索引越界问题</strong>,即 <code>i > leftEnd</code> 和 <code>j > rightEnd</code> 的情况,索引越界的优先级是最高的,例如如果左子数组已经被合并完了,那么不用继续判断,直接合并右子数组元素即可。</li>
|
||||
</ul>
|
||||
<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> 时间。</li>
|
||||
<li><strong>空间复杂度 <span class="arithmatex">\(O(n)\)</span></strong> :需借助辅助数组实现合并,使用 <span class="arithmatex">\(O(n)\)</span> 大小的额外空间;递归深度为 <span class="arithmatex">\(\log n\)</span> ,使用 <span class="arithmatex">\(O(\log n)\)</span> 大小的栈帧空间。</li>
|
||||
<li><strong>非原地排序</strong>:辅助数组需要使用 <span class="arithmatex">\(O(n)\)</span> 额外空间。</li>
|
||||
<li><strong>稳定排序</strong>:在合并时可保证相等元素的相对位置不变。</li>
|
||||
<li><strong>非自适应排序</strong>:对于任意输入数据,归并排序的时间复杂度皆相同。</li>
|
||||
</ul>
|
||||
<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><strong>空间复杂度 <span class="arithmatex">\(O(n)\)</span></strong> :需借助辅助数组实现合并,使用 <span class="arithmatex">\(O(n)\)</span> 大小的额外空间;递归深度为 <span class="arithmatex">\(\log n\)</span> ,使用 <span class="arithmatex">\(O(\log n)\)</span> 大小的栈帧空间,因此是“非原地排序”。</p>
|
||||
<p>在合并时,不改变相等元素的次序,是“稳定排序”。</p>
|
||||
<h2 id="1153">11.5.3. 链表排序 *<a class="headerlink" href="#1153" title="Permanent link">¶</a></h2>
|
||||
<p>归并排序有一个很特别的优势,用于排序链表时有很好的性能表现,<strong>空间复杂度可被优化至 <span class="arithmatex">\(O(1)\)</span></strong> ,这是因为:</p>
|
||||
<ul>
|
||||
|
@ -2171,12 +2171,9 @@
|
||||
</div>
|
||||
</div>
|
||||
<h2 id="1142">11.4.2. 算法特性<a class="headerlink" href="#1142" title="Permanent link">¶</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> 时间。</p>
|
||||
<p><strong>最差时间复杂度 <span class="arithmatex">\(O(n^2)\)</span></strong> :最差情况下,哨兵划分操作将长度为 <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>:只在递归中使用 <span class="arithmatex">\(O(\log n)\)</span> 大小的栈帧空间。</p>
|
||||
<p><strong>非稳定排序</strong>:哨兵划分操作可能改变相等元素的相对位置。</p>
|
||||
<p><strong>自适应排序</strong>:最差情况下,时间复杂度劣化至 <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> 时间。最差情况下,每轮哨兵划分操作都将长度为 <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. 快排为什么快?<a class="headerlink" href="#1143" title="Permanent link">¶</a></h2>
|
||||
<p>从命名能够看出,快速排序在效率方面一定“有两把刷子”。快速排序的平均时间复杂度虽然与「归并排序」和「堆排序」一致,但实际 <strong>效率更高</strong>,这是因为:</p>
|
||||
<ul>
|
||||
|
File diff suppressed because one or more lines are too long
106
sitemap.xml
106
sitemap.xml
@ -2,267 +2,267 @@
|
||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_appendix/contribution/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_appendix/installation/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/array/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/linked_list/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/list/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/summary/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/performance_evaluation/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/space_complexity/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/space_time_tradeoff/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/summary/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/time_complexity/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/classification_of_data_structure/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/data_and_memory/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/summary/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/graph/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/graph_operations/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/graph_traversal/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/summary/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_hashing/hash_collision/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_hashing/hash_map/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_hashing/summary/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_heap/build_heap/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_heap/heap/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_heap/summary/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_introduction/algorithms_are_everywhere/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_introduction/summary/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_introduction/what_is_dsa/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_preface/about_the_book/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_preface/suggestions/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_preface/summary/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_reference/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/binary_search/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/hashing_search/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/linear_search/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/summary/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/bubble_sort/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/bucket_sort/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/counting_sort/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/insertion_sort/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/intro_to_sort/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/merge_sort/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/quick_sort/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/summary/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/deque/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/queue/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/stack/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/summary/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/avl_tree/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/binary_search_tree/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/binary_tree/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/binary_tree_traversal/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/summary/</loc>
|
||||
<lastmod>2023-03-23</lastmod>
|
||||
<lastmod>2023-03-25</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
</urlset>
|
BIN
sitemap.xml.gz
BIN
sitemap.xml.gz
Binary file not shown.
Reference in New Issue
Block a user