mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-23 12:12:01 +08:00
Release reviewed gh-pages candidate
This commit is contained in:
@@ -47,7 +47,7 @@
|
||||
set(key, value, storage, key === "__palette" ? shared : scope);
|
||||
})();
|
||||
</script>
|
||||
<link href="/assets/figure-svg/css/ca08fd1c3a121ac517cf0998.css" rel="stylesheet"/><link href="/assets/figure-svg/css/9f909651eeceaf5c7dda140d.css" rel="stylesheet"/></head>
|
||||
<link href="/assets/figure-svg/css/ca08fd1c3a121ac517cf0998.css" rel="stylesheet"/><link href="/assets/figure-svg/css/f41655ef506ff5ce3cf0fd42.css" rel="stylesheet"/></head>
|
||||
<body data-md-color-accent="teal" data-md-color-primary="white" data-md-color-scheme="default" dir="ltr">
|
||||
<input autocomplete="off" class="md-toggle" data-md-toggle="drawer" id="__drawer" type="checkbox"/>
|
||||
<input autocomplete="off" class="md-toggle" data-md-toggle="search" id="__search" type="checkbox"/>
|
||||
@@ -2093,8 +2093,8 @@
|
||||
<!-- Page content -->
|
||||
<h1 id="41">4.1 数组<a class="headerlink" href="#41" title="Permanent link">¶</a></h1>
|
||||
<p><u>数组(array)</u>是一种线性数据结构,其将相同类型的元素存储在连续的内存空间中。我们将元素在数组中的位置称为该元素的<u>索引(index)</u>。图 4-1 展示了数组的主要概念和存储方式。</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="chapter_array_and_linkedlist/array.assets/array_definition.png" data-dark-src="/assets/figure-svg/figures/08926516250ad8e82a3dee96.json" data-src="/assets/figure-svg/figures/fd306a2e1258399df916cc2b.json" style=";--figure-ratio:960.0/540.0"><object data="/chapter_array_and_linkedlist/array.assets/array_definition.svg" data-theme="light" tabindex="-1" type="image/svg+xml">数组定义与存储方式</object><object data="/chapter_array_and_linkedlist/array.assets/array_definition.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">数组定义与存储方式</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 4-1 数组定义与存储方式 </p>
|
||||
<p><ha-svg-figure aria-label="数组的元素、索引与地址" class="animation-figure" data-book-key="chapter_array_and_linkedlist/array.assets/array_definition.png" data-dark-src="/assets/figure-svg/figures/08926516250ad8e82a3dee96.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/fd306a2e1258399df916cc2b.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/chapter_array_and_linkedlist/array.assets/array_definition.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">数组定义与存储方式</object><object aria-hidden="true" data="/chapter_array_and_linkedlist/array.assets/array_definition.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">数组定义与存储方式</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 4-1 数组定义与存储方式 </p><div class="ha-sr-only figure-description"><h2>图解 4-1:数组的元素、索引与地址</h2><p>数组通过从 0 开始的索引定位元素,所有元素在内存中连续存放且占用相同大小的空间。</p><p>左侧列出数组、索引和内存地址;右侧网格的第二行有 5 个连续绿色单元格,表示划分给数组的空间,其余浅灰格表示可用空间。</p><table><caption>五个元素的位置与内存地址</caption><thead><tr><th scope="col">索引</th><th scope="col">元素值</th><th scope="col">起始内存地址</th></tr></thead><tbody><tr><th scope="row">0</th><td>1</td><td>00</td></tr><tr><th scope="row">1</th><td>3</td><td>04</td></tr><tr><th scope="row">2</th><td>2</td><td>08</td></tr><tr><th scope="row">3</th><td>5</td><td>12</td></tr><tr><th scope="row">4</th><td>4</td><td>16</td></tr></tbody></table><p>本例每个元素占 4 字节,所以相邻元素的起始地址固定相差 4。地址中的前导 0 仅用于对齐位数,不表示八进制。</p><p>索引表示元素在数组中的位置序号,元素值是该位置存放的数据,内存地址则是存储单元的起始编号,三者概念不同。</p></div>
|
||||
<h2 id="411">4.1.1 数组常用操作<a class="headerlink" href="#411" title="Permanent link">¶</a></h2>
|
||||
<h3 id="1">1. 初始化数组<a class="headerlink" href="#1" title="Permanent link">¶</a></h3>
|
||||
<p>我们可以根据需求选用数组的两种初始化方式:无初始值、给定初始值。在未指定初始值的情况下,大多数编程语言会将数组元素初始化为 <span class="arithmatex">\(0\)</span> :</p>
|
||||
@@ -2199,8 +2199,8 @@
|
||||
</details>
|
||||
<h3 id="2">2. 访问元素<a class="headerlink" href="#2" title="Permanent link">¶</a></h3>
|
||||
<p>数组元素被存储在连续的内存空间中,这意味着计算数组元素的内存地址非常容易。给定数组内存地址(首元素内存地址)和某个元素的索引,我们可以使用图 4-2 所示的公式计算得到该元素的内存地址,从而直接访问该元素。</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="chapter_array_and_linkedlist/array.assets/array_memory_location_calculation.png" data-dark-src="/assets/figure-svg/figures/4298551d1ca78a301c808e4b.json" data-src="/assets/figure-svg/figures/0d6f46f0e05f3f02adce0d40.json" style=";--figure-ratio:960.0/540.0"><object data="/chapter_array_and_linkedlist/array.assets/array_memory_location_calculation.svg" data-theme="light" tabindex="-1" type="image/svg+xml">数组元素的内存地址计算</object><object data="/chapter_array_and_linkedlist/array.assets/array_memory_location_calculation.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">数组元素的内存地址计算</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 4-2 数组元素的内存地址计算 </p>
|
||||
<p><ha-svg-figure aria-label="数组元素的地址计算" class="animation-figure" data-book-key="chapter_array_and_linkedlist/array.assets/array_memory_location_calculation.png" data-dark-src="/assets/figure-svg/figures/4298551d1ca78a301c808e4b.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/0d6f46f0e05f3f02adce0d40.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/chapter_array_and_linkedlist/array.assets/array_memory_location_calculation.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">数组元素的内存地址计算</object><object aria-hidden="true" data="/chapter_array_and_linkedlist/array.assets/array_memory_location_calculation.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">数组元素的内存地址计算</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 4-2 数组元素的内存地址计算 </p><div class="ha-sr-only figure-description"><h4>图解 4-2:数组元素的地址计算</h4><p>利用首地址、固定元素长度和索引,直接算出元素位置。</p><p>图上方排列数组、索引和内存地址,突出索引 3 这一列;下方给出地址公式和代入示例。每个元素占 4 字节,上方地址行用两位写法(如 00、12),下方算式用三位写法(000、012);前导零仅用于对齐,均按十进制解释。</p><table><caption>索引、值与图中地址</caption><thead><tr><th scope="col">索引</th><th scope="col">元素值</th><th scope="col">内存地址</th></tr></thead><tbody><tr><th scope="row">0</th><td>1</td><td>00</td></tr><tr><th scope="row">1</th><td>3</td><td>04</td></tr><tr><th scope="row">2</th><td>2</td><td>08</td></tr><tr><th scope="row">3</th><td>5</td><td>12</td></tr><tr><th scope="row">4</th><td>4</td><td>16</td></tr></tbody></table><p>元素地址 = 首元素地址 + 元素长度 × 索引。索引从 0 起算,所以索引 3 是第 4 个元素,值为 5;相对首元素跨过 3 个元素,字节偏移为 4 × 3 = 12,地址为 000 + 4 × 3 = 012。这里按索引定位,并不是搜索值为 3 的元素。</p><p>连续存储且元素等长时,用一次乘法和加法即可求出地址,支持 O(1) 随机访问。</p></div>
|
||||
<p>观察图 4-2 ,我们发现数组首个元素的索引为 <span class="arithmatex">\(0\)</span> ,这似乎有些反直觉,因为从 <span class="arithmatex">\(1\)</span> 开始计数会更自然。但从地址计算公式的角度看,<strong>索引本质上是内存地址的偏移量</strong>。首个元素的地址偏移量是 <span class="arithmatex">\(0\)</span> ,因此它的索引为 <span class="arithmatex">\(0\)</span> 是合理的。</p>
|
||||
<p>在数组中访问元素非常高效,我们可以在 <span class="arithmatex">\(O(1)\)</span> 时间内随机访问数组中的任意一个元素。</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="2:13"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio"/><input id="__tabbed_2_2" name="__tabbed_2" type="radio"/><input id="__tabbed_2_3" name="__tabbed_2" type="radio"/><input id="__tabbed_2_4" name="__tabbed_2" type="radio"/><input id="__tabbed_2_5" name="__tabbed_2" type="radio"/><input id="__tabbed_2_6" name="__tabbed_2" type="radio"/><input id="__tabbed_2_7" name="__tabbed_2" type="radio"/><input id="__tabbed_2_8" name="__tabbed_2" type="radio"/><input id="__tabbed_2_9" name="__tabbed_2" type="radio"/><input id="__tabbed_2_10" name="__tabbed_2" type="radio"/><input id="__tabbed_2_11" name="__tabbed_2" type="radio"/><input id="__tabbed_2_12" name="__tabbed_2" type="radio"/><input id="__tabbed_2_13" name="__tabbed_2" type="radio"/><div class="tabbed-labels"><label for="__tabbed_2_1">Python</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Java</label><label for="__tabbed_2_4">C#</label><label for="__tabbed_2_5">Go</label><label for="__tabbed_2_6">Swift</label><label for="__tabbed_2_7">JS</label><label for="__tabbed_2_8">TS</label><label for="__tabbed_2_9">Dart</label><label for="__tabbed_2_10">Rust</label><label for="__tabbed_2_11">C</label><label for="__tabbed_2_12">Kotlin</label><label for="__tabbed_2_13">Ruby</label></div>
|
||||
@@ -2357,8 +2357,8 @@
|
||||
</details>
|
||||
<h3 id="3">3. 插入元素<a class="headerlink" href="#3" title="Permanent link">¶</a></h3>
|
||||
<p>数组元素在内存中是“紧挨着的”,它们之间没有空间再存放任何数据。如图 4-3 所示,如果想在数组中间插入一个元素,则需要将该元素之后的所有元素都向后移动一位,之后再把元素赋值给该索引。</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="chapter_array_and_linkedlist/array.assets/array_insert_element.png" data-dark-src="/assets/figure-svg/figures/fd70cb36e3ca0e8bca2e1ac3.json" data-src="/assets/figure-svg/figures/f5d313e025efe47e102ca844.json" style=";--figure-ratio:960.0/540.0"><object data="/chapter_array_and_linkedlist/array.assets/array_insert_element.svg" data-theme="light" tabindex="-1" type="image/svg+xml">数组插入元素示例</object><object data="/chapter_array_and_linkedlist/array.assets/array_insert_element.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">数组插入元素示例</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 4-3 数组插入元素示例 </p>
|
||||
<p><ha-svg-figure aria-label="在数组中插入元素" class="animation-figure" data-book-key="chapter_array_and_linkedlist/array.assets/array_insert_element.png" data-dark-src="/assets/figure-svg/figures/fd70cb36e3ca0e8bca2e1ac3.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/f5d313e025efe47e102ca844.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/chapter_array_and_linkedlist/array.assets/array_insert_element.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">数组插入元素示例</object><object aria-hidden="true" data="/chapter_array_and_linkedlist/array.assets/array_insert_element.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">数组插入元素示例</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 4-3 数组插入元素示例 </p><div class="ha-sr-only figure-description"><h4>图解 4-3:在数组中插入元素</h4><p>在索引 1 处插入元素 3:先从末端开始将原有元素依次向右复制一位,再写入新元素。</p><p>数组 nums 共有 5 个槽位,索引为 0 至 4。初始存储为 [1, 2, 5, 4, 0],其中只有前 4 个元素 [1, 2, 5, 4] 有效,末尾索引 4 的 0 是无意义的占位值。目标是在索引 1 处插入 3,并保持原有有效元素的相对顺序。</p><table><caption>每次赋值后的五个槽位</caption><thead><tr><th scope="col">操作</th><th scope="col">索引0</th><th scope="col">索引1</th><th scope="col">索引2</th><th scope="col">索引3</th><th scope="col">索引4</th></tr></thead><tbody><tr><th scope="row">初始状态</th><td>1</td><td>2</td><td>5</td><td>4</td><td>0</td></tr><tr><th scope="row">nums[4] = nums[3]</th><td>1</td><td>2</td><td>5</td><td>4</td><td>4</td></tr><tr><th scope="row">nums[3] = nums[2]</th><td>1</td><td>2</td><td>5</td><td>5</td><td>4</td></tr><tr><th scope="row">nums[2] = nums[1]</th><td>1</td><td>2</td><td>2</td><td>5</td><td>4</td></tr><tr><th scope="row">nums[1] = 3</th><td>1</td><td>3</td><td>2</td><td>5</td><td>4</td></tr></tbody></table><p>从末端向插入点倒序复制,保证每个即将被覆盖、仍需保留的元素已复制到右侧相邻格。复制不会清空源槽位,因此中间状态会出现重复值。在数组中间插入,平均需要移动 O(n) 个元素。</p><p>插入后 5 个槽位全部有效;调用方据此将有效长度更新为 5,示例 insert 本身只执行槽位赋值。后文所说的定长数组尾部元素“丢失”,在本例中是末槽原有的占位值 0 被覆盖,四个原有有效元素均未丢失。若原本 5 个槽位全部有效,这种覆盖就会丢失末尾有效元素,需要先扩容或拒绝插入。</p></div>
|
||||
<p>值得注意的是,由于数组的长度是固定的,因此插入一个元素必定会导致数组尾部元素“丢失”。我们将这个问题的解决方案留在“列表”章节中讨论。</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="3:13"><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"/><input id="__tabbed_3_11" name="__tabbed_3" type="radio"/><input id="__tabbed_3_12" name="__tabbed_3" type="radio"/><input id="__tabbed_3_13" name="__tabbed_3" type="radio"/><div class="tabbed-labels"><label for="__tabbed_3_1">Python</label><label for="__tabbed_3_2">C++</label><label for="__tabbed_3_3">Java</label><label for="__tabbed_3_4">C#</label><label for="__tabbed_3_5">Go</label><label for="__tabbed_3_6">Swift</label><label for="__tabbed_3_7">JS</label><label for="__tabbed_3_8">TS</label><label for="__tabbed_3_9">Dart</label><label for="__tabbed_3_10">Rust</label><label for="__tabbed_3_11">C</label><label for="__tabbed_3_12">Kotlin</label><label for="__tabbed_3_13">Ruby</label></div>
|
||||
<div class="tabbed-content">
|
||||
@@ -2526,8 +2526,8 @@
|
||||
</details>
|
||||
<h3 id="4">4. 删除元素<a class="headerlink" href="#4" title="Permanent link">¶</a></h3>
|
||||
<p>同理,如图 4-4 所示,若想删除索引 <span class="arithmatex">\(i\)</span> 处的元素,则需要把索引 <span class="arithmatex">\(i\)</span> 之后的元素都向前移动一位。</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="chapter_array_and_linkedlist/array.assets/array_remove_element.png" data-dark-src="/assets/figure-svg/figures/246002dfb77cff4cc772c5db.json" data-src="/assets/figure-svg/figures/076b12e10123507a55400d82.json" style=";--figure-ratio:960.0/540.0"><object data="/chapter_array_and_linkedlist/array.assets/array_remove_element.svg" data-theme="light" tabindex="-1" type="image/svg+xml">数组删除元素示例</object><object data="/chapter_array_and_linkedlist/array.assets/array_remove_element.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">数组删除元素示例</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 4-4 数组删除元素示例 </p>
|
||||
<p><ha-svg-figure aria-label="数组删除与前移" class="animation-figure" data-book-key="chapter_array_and_linkedlist/array.assets/array_remove_element.png" data-dark-src="/assets/figure-svg/figures/246002dfb77cff4cc772c5db.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/076b12e10123507a55400d82.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/chapter_array_and_linkedlist/array.assets/array_remove_element.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">数组删除元素示例</object><object aria-hidden="true" data="/chapter_array_and_linkedlist/array.assets/array_remove_element.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">数组删除元素示例</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 4-4 数组删除元素示例 </p><div class="ha-sr-only figure-description"><h4>图解 4-4:数组删除与前移</h4><p>覆盖待删除位置后保持其余元素顺序,并区分有效长度与存储槽位数。</p><p>数组 nums 有 5 个槽位,初始为 [1, 3, 2, 5, 4],有效长度为 5,索引从 0 起。目标是删除索引 1 的 3,并保持其余元素的次序。</p><table><caption>前移过程中的完整存储</caption><thead><tr><th scope="col">状态或赋值</th><th scope="col">存储内容</th><th scope="col">有效长度或含义</th></tr></thead><tbody><tr><th scope="row">初始</th><td>[1,3,2,5,4]</td><td>5,索引1的3待删除</td></tr><tr><th scope="row">第 1 次复制:nums[1]=nums[2]</th><td>[1,2,2,5,4]</td><td>尚未完成,记录长度5</td></tr><tr><th scope="row">第 2 次复制:nums[2]=nums[3]</th><td>[1,2,5,5,4]</td><td>尚未完成,记录长度5</td></tr><tr><th scope="row">第 3 次复制:nums[3]=nums[4];调用方将有效长度改为 4</th><td>[1,2,5,4,4]</td><td>4;仅前4项有效,末项4为残留</td></tr></tbody></table><p>从删除位置向右依次读取相邻元素、向左覆盖,确保每个源值都在被覆盖前读出。复制只改写目标槽位,不清除源槽位,所以过程中会出现相邻重复值。</p><p>三次复制后,由调用方把有效长度从 5 改为 4,仅索引 0 至 3 的 [1, 2, 5, 4] 有效,末槽的 4 是无效残留,不必清零。示例 remove 只覆盖赋值,不会自动缩短 Python 列表;这类中间删除的平均时间复杂度为 O(n)。</p></div>
|
||||
<p>请注意,删除元素完成后,原先末尾的元素变得“无意义”了,所以我们无须特意去修改它。</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="4:13"><input checked="checked" id="__tabbed_4_1" name="__tabbed_4" type="radio"/><input id="__tabbed_4_2" name="__tabbed_4" type="radio"/><input id="__tabbed_4_3" name="__tabbed_4" type="radio"/><input id="__tabbed_4_4" name="__tabbed_4" type="radio"/><input id="__tabbed_4_5" name="__tabbed_4" type="radio"/><input id="__tabbed_4_6" name="__tabbed_4" type="radio"/><input id="__tabbed_4_7" name="__tabbed_4" type="radio"/><input id="__tabbed_4_8" name="__tabbed_4" type="radio"/><input id="__tabbed_4_9" name="__tabbed_4" type="radio"/><input id="__tabbed_4_10" name="__tabbed_4" type="radio"/><input id="__tabbed_4_11" name="__tabbed_4" type="radio"/><input id="__tabbed_4_12" name="__tabbed_4" type="radio"/><input id="__tabbed_4_13" name="__tabbed_4" type="radio"/><div class="tabbed-labels"><label for="__tabbed_4_1">Python</label><label for="__tabbed_4_2">C++</label><label for="__tabbed_4_3">Java</label><label for="__tabbed_4_4">C#</label><label for="__tabbed_4_5">Go</label><label for="__tabbed_4_6">Swift</label><label for="__tabbed_4_7">JS</label><label for="__tabbed_4_8">TS</label><label for="__tabbed_4_9">Dart</label><label for="__tabbed_4_10">Rust</label><label for="__tabbed_4_11">C</label><label for="__tabbed_4_12">Kotlin</label><label for="__tabbed_4_13">Ruby</label></div>
|
||||
<div class="tabbed-content">
|
||||
@@ -3395,5 +3395,5 @@
|
||||
<script src="../../javascripts/mathjax.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.min.js"></script>
|
||||
<script src="../../javascripts/animation_player.js"></script>
|
||||
<script defer="" src="/assets/figure-svg/js/bcdea376f01bc2bc0cac7c6c.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/99a707e9f0a14d8280f16808.js"></script><script defer="" src="/assets/figure-svg/js/b789dc8b01e43e756fc89964.js"></script></body>
|
||||
<script defer="" src="/assets/figure-svg/js/bcdea376f01bc2bc0cac7c6c.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/bff17b8259339758cb1c4a06.js"></script></body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user