mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-23 04:02:44 +08:00
Release reviewed gh-pages candidate
This commit is contained in:
@@ -2065,8 +2065,8 @@
|
||||
<!-- Page content -->
|
||||
<h1 id="41-arrays">4.1 Arrays<a class="headerlink" href="#41-arrays" title="Permanent link">¶</a></h1>
|
||||
<p>An <u>array</u> is a linear data structure that stores elements of the same type in contiguous memory. An element's position in the array is its <u>index</u>. Figure 4-1 shows the main concepts and storage layout.</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_array_and_linkedlist/array.assets/array_definition.png" data-dark-src="/assets/figure-svg/figures/91f177614dfd226cebfa4cb8.json" data-src="/assets/figure-svg/figures/11f0cd181745d783cdba45cf.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_array_and_linkedlist/array.assets/array_definition.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Array definition and storage layout</object><object data="/en/chapter_array_and_linkedlist/array.assets/array_definition.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Array definition and storage layout</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 4-1 Array definition and storage layout </p>
|
||||
<p><ha-svg-figure aria-label="Array elements, indices, and memory addresses" class="animation-figure" data-book-key="en/chapter_array_and_linkedlist/array.assets/array_definition.png" data-dark-src="/assets/figure-svg/figures/91f177614dfd226cebfa4cb8.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/11f0cd181745d783cdba45cf.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_array_and_linkedlist/array.assets/array_definition.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Array definition and storage layout</object><object aria-hidden="true" data="/en/chapter_array_and_linkedlist/array.assets/array_definition.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Array definition and storage layout</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 4-1 Array definition and storage layout </p><div class="ha-sr-only figure-description"><h2>Description of Figure 4-1: Array elements, indices, and memory addresses</h2><p>An array locates elements using 0-based indices, storing all elements contiguously in memory in equal-sized slots.</p><p>The left side lists the array, indices, and memory addresses; in the second row of the grid on the right, 5 contiguous green cells represent the memory allocated to the array, while the remaining light gray cells represent free memory.</p><table><caption>Positions and memory addresses of the five elements</caption><thead><tr><th scope="col">Index</th><th scope="col">Element value</th><th scope="col">Starting memory address</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>In this example, each element occupies 4 bytes, so the starting addresses of adjacent elements always differ by 4. The leading 0 in an address only pads the width and does not indicate octal notation.</p><p>An index is an element's ordinal position in the array, the element value is the data stored at that position, and the memory address is the starting number of its storage unit; the three are distinct concepts.</p></div>
|
||||
<h2 id="411-common-array-operations">4.1.1 Common array operations<a class="headerlink" href="#411-common-array-operations" title="Permanent link">¶</a></h2>
|
||||
<h3 id="1-initializing-an-array">1. Initializing an array<a class="headerlink" href="#1-initializing-an-array" title="Permanent link">¶</a></h3>
|
||||
<p>We can initialize an array with or without explicit values. When values are not specified, most programming languages initialize the elements to <span class="arithmatex">\(0\)</span>:</p>
|
||||
@@ -2171,8 +2171,8 @@
|
||||
</details>
|
||||
<h3 id="2-accessing-an-element">2. Accessing an element<a class="headerlink" href="#2-accessing-an-element" title="Permanent link">¶</a></h3>
|
||||
<p>Because array elements occupy contiguous memory, their addresses are easy to calculate. Given the array's address (the address of its first element) and an index, the formula in Figure 4-2 gives the element's address, allowing direct access.</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_array_and_linkedlist/array.assets/array_memory_location_calculation.png" data-dark-src="/assets/figure-svg/figures/3f3ad9fc8fd1d0ec4a9edcbc.json" data-src="/assets/figure-svg/figures/c859ba77be30b329c85a6367.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_array_and_linkedlist/array.assets/array_memory_location_calculation.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Calculating an array element's memory address</object><object data="/en/chapter_array_and_linkedlist/array.assets/array_memory_location_calculation.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Calculating an array element's memory address</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 4-2 Calculating an array element's memory address </p>
|
||||
<p><ha-svg-figure aria-label="Calculating an array element's memory address" class="animation-figure" data-book-key="en/chapter_array_and_linkedlist/array.assets/array_memory_location_calculation.png" data-dark-src="/assets/figure-svg/figures/3f3ad9fc8fd1d0ec4a9edcbc.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/c859ba77be30b329c85a6367.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_array_and_linkedlist/array.assets/array_memory_location_calculation.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Calculating an array element's memory address</object><object aria-hidden="true" data="/en/chapter_array_and_linkedlist/array.assets/array_memory_location_calculation.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Calculating an array element's memory address</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 4-2 Calculating an array element's memory address </p><div class="ha-sr-only figure-description"><h4>Description of Figure 4-2: Calculating an array element's memory address</h4><p>Compute an element's location directly from the starting address, fixed element size, and index.</p><p>The upper part arranges the array, indices, and memory addresses with the column at index 3 highlighted; the lower part gives the address formula and a worked substitution. Each element occupies 4 bytes; the upper address row uses two-digit notation (such as 00 and 12), while the formula below uses three-digit notation (000 and 012). Leading zeros are purely for alignment, and all numbers are decimal.</p><table><caption>Indices, values, and addresses shown in the figure</caption><thead><tr><th scope="col">Index</th><th scope="col">Element value</th><th scope="col">Memory address</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>Element address = address of the first element + element size × index. Indexing starts at 0, so index 3 is the 4th element, with value 5; it is 3 elements past the first element, giving a byte offset of 4 × 3 = 12 and an address of 000 + 4 × 3 = 012. This locates the element by index rather than searching for an element whose value is 3.</p><p>With contiguous storage and equal-sized elements, a single multiplication and addition gives the address, supporting O(1) random access.</p></div>
|
||||
<p>The first element has index <span class="arithmatex">\(0\)</span>, which may seem unintuitive because counting from <span class="arithmatex">\(1\)</span> feels more natural. However, <strong>an index essentially describes an offset from the starting memory address</strong>. The first element's offset is <span class="arithmatex">\(0\)</span>, so index <span class="arithmatex">\(0\)</span> makes sense.</p>
|
||||
<p>Array access is very efficient: any element can be accessed directly in <span class="arithmatex">\(O(1)\)</span> time.</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>
|
||||
@@ -2329,8 +2329,8 @@
|
||||
</details>
|
||||
<h3 id="3-inserting-an-element">3. Inserting an element<a class="headerlink" href="#3-inserting-an-element" title="Permanent link">¶</a></h3>
|
||||
<p>Array elements sit next to one another, with no room between them for more data. As shown in Figure 4-3, inserting into the middle requires shifting every element at or after the insertion point one position toward the end, then assigning the new element at the desired index.</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_array_and_linkedlist/array.assets/array_insert_element.png" data-dark-src="/assets/figure-svg/figures/d3d430ae5f5290ccb78a64e8.json" data-src="/assets/figure-svg/figures/c95261c7660a33d232c2c75c.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_array_and_linkedlist/array.assets/array_insert_element.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Inserting an element into an array</object><object data="/en/chapter_array_and_linkedlist/array.assets/array_insert_element.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Inserting an element into an array</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 4-3 Inserting an element into an array </p>
|
||||
<p><ha-svg-figure aria-label="Inserting an element into an array" class="animation-figure" data-book-key="en/chapter_array_and_linkedlist/array.assets/array_insert_element.png" data-dark-src="/assets/figure-svg/figures/d3d430ae5f5290ccb78a64e8.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/c95261c7660a33d232c2c75c.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_array_and_linkedlist/array.assets/array_insert_element.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Inserting an element into an array</object><object aria-hidden="true" data="/en/chapter_array_and_linkedlist/array.assets/array_insert_element.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Inserting an element into an array</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 4-3 Inserting an element into an array </p><div class="ha-sr-only figure-description"><h4>Description of Figure 4-3: Inserting an element into an array</h4><p>Insert element 3 at index 1: first copy existing elements one position to the right starting from the end, then write the new element.</p><p>Array nums has 5 slots at indices 0 through 4. Its initial storage is [1, 2, 5, 4, 0], where only the first 4 elements [1, 2, 5, 4] are valid and the trailing 0 at index 4 is an unused placeholder. The goal is to insert 3 at index 1 while preserving the relative order of the existing valid elements.</p><table><caption>Five slots after each assignment</caption><thead><tr><th scope="col">Operation</th><th scope="col">Index 0</th><th scope="col">Index 1</th><th scope="col">Index 2</th><th scope="col">Index 3</th><th scope="col">Index 4</th></tr></thead><tbody><tr><th scope="row">Initial state</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>Copying backward from the end toward the insertion point ensures that every element about to be overwritten and still needed has already been copied to the adjacent slot on its right. Copying does not clear the source slot, so duplicate values appear in intermediate states. Inserting into the middle of an array shifts O(n) elements on average.</p><p>After insertion, all 5 slots hold valid elements; the caller updates the valid length to 5 accordingly, whereas the sample insert function itself only assigns slot values. When the text later notes that the last element of a fixed-length array is "lost", in this example only the placeholder 0 in the last slot is overwritten, so none of the four original valid elements are lost. Had all 5 slots originally held valid elements, overwriting would lose the final valid element, requiring array expansion beforehand or rejecting the insertion.</p></div>
|
||||
<p>Because an array's length is fixed, inserting an element causes the last element to be lost. We will discuss the solution in the section on lists.</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">
|
||||
@@ -2498,8 +2498,8 @@
|
||||
</details>
|
||||
<h3 id="4-removing-an-element">4. Removing an element<a class="headerlink" href="#4-removing-an-element" title="Permanent link">¶</a></h3>
|
||||
<p>Similarly, as shown in Figure 4-4, removing the element at index <span class="arithmatex">\(i\)</span> requires shifting every element after <span class="arithmatex">\(i\)</span> one position toward the beginning.</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_array_and_linkedlist/array.assets/array_remove_element.png" data-dark-src="/assets/figure-svg/figures/549802dcc1ab99dab22b9853.json" data-src="/assets/figure-svg/figures/dfe5e725d0a09a6e77b6fd0a.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_array_and_linkedlist/array.assets/array_remove_element.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Removing an element from an array</object><object data="/en/chapter_array_and_linkedlist/array.assets/array_remove_element.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Removing an element from an array</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 4-4 Removing an element from an array </p>
|
||||
<p><ha-svg-figure aria-label="Removing an array element by shifting left" class="animation-figure" data-book-key="en/chapter_array_and_linkedlist/array.assets/array_remove_element.png" data-dark-src="/assets/figure-svg/figures/549802dcc1ab99dab22b9853.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/dfe5e725d0a09a6e77b6fd0a.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_array_and_linkedlist/array.assets/array_remove_element.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Removing an element from an array</object><object aria-hidden="true" data="/en/chapter_array_and_linkedlist/array.assets/array_remove_element.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">Removing an element from an array</object></ha-svg-figure></p>
|
||||
<p align="center"> Figure 4-4 Removing an element from an array </p><div class="ha-sr-only figure-description"><h4>Description of Figure 4-4: Removing an array element by shifting left</h4><p>Overwrite the removed position to preserve the order of the remaining elements while distinguishing valid length from total slot count.</p><p>Array nums has 5 slots, initialized to [1, 3, 2, 5, 4] with valid length 5 and 0-based indices. The goal is to remove 3 at index 1 while preserving the order of the remaining elements.</p><table><caption>Complete slot storage during leftward shifting</caption><thead><tr><th scope="col">State or assignment</th><th scope="col">Stored contents</th><th scope="col">Valid length or meaning</th></tr></thead><tbody><tr><th scope="row">Initial</th><td>[1,3,2,5,4]</td><td>5; 3 at index 1 is to be deleted</td></tr><tr><th scope="row">1st copy: nums[1]=nums[2]</th><td>[1,2,2,5,4]</td><td>In progress, recorded length 5</td></tr><tr><th scope="row">2nd copy: nums[2]=nums[3]</th><td>[1,2,5,5,4]</td><td>In progress, recorded length 5</td></tr><tr><th scope="row">3rd copy: nums[3]=nums[4]; caller updates valid length to 4</th><td>[1,2,5,4,4]</td><td>4; only the first 4 entries are valid, with trailing 4 left over</td></tr></tbody></table><p>Reading adjacent elements sequentially to the right of the removed position and overwriting one slot to the left ensures that every source value is read before it is overwritten. Copying modifies only the destination slot without clearing the source slot, so adjacent duplicate values appear along the way.</p><p>After three copies, the caller updates the valid length from 5 to 4, leaving only [1, 2, 5, 4] at indices 0 through 3 valid; the 4 in the final slot is an unused leftover and need not be zeroed out. The sample remove function only overwrites slots without automatically shortening a Python list; removing an element from the middle takes O(n) time on average.</p></div>
|
||||
<p>After removal, the old final entry is no longer meaningful, so we do not need to change it explicitly.</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">
|
||||
@@ -3367,5 +3367,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/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/bff17b8259339758cb1c4a06.js"></script></body>
|
||||
<script defer="" src="/assets/figure-svg/js/00174ff861e190f5c1f09e34.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/70ae82c6406c9bdbda8e8362.js"></script></body>
|
||||
</html>
|
||||
Binary file not shown.
Reference in New Issue
Block a user