This commit is contained in:
krahets
2025-12-31 19:38:45 +08:00
parent fdc032e300
commit f31cbc3f9f
474 changed files with 130810 additions and 103691 deletions

View File

@@ -37,7 +37,7 @@
<title>3.2 Basic data types - Hello Algo</title>
<title>3.2 Basic Data Types - Hello Algo</title>
@@ -58,8 +58,8 @@
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
<style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:300,300i,400,400i,700,700i%7CJetBrains+Mono:400,400i,700,700i&display=fallback">
<style>:root{--md-text-font:"Lato";--md-code-font:"JetBrains Mono"}</style>
@@ -154,7 +154,7 @@
<div class="md-header__topic" data-md-component="header-topic">
<span class="md-ellipsis">
3.2 Basic data types
3.2 Basic Data Types
</span>
</div>
@@ -371,7 +371,7 @@
<span class="md-ellipsis">
Before starting
Before Starting
@@ -388,7 +388,7 @@
<span class="md-nav__icon md-icon"></span>
Before starting
Before Starting
</label>
@@ -487,7 +487,7 @@
<span class="md-ellipsis">
0.1 About this book
0.1 About This Book
@@ -515,7 +515,7 @@
<span class="md-ellipsis">
0.2 How to read
0.2 How to Use This Book
@@ -604,7 +604,7 @@
<span class="md-ellipsis">
Chapter 1. Encounter with algorithms
Chapter 1. Encounter With Algorithms
@@ -626,7 +626,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 1. Encounter with algorithms
Chapter 1. Encounter With Algorithms
</label>
@@ -648,7 +648,7 @@
<span class="md-ellipsis">
1.1 Algorithms are everywhere
1.1 Algorithms Are Everywhere
@@ -676,7 +676,7 @@
<span class="md-ellipsis">
1.2 What is an algorithm
1.2 What Is an Algorithm
@@ -769,7 +769,7 @@
<span class="md-ellipsis">
Chapter 2. Complexity analysis
Chapter 2. Complexity Analysis
@@ -791,7 +791,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 2. Complexity analysis
Chapter 2. Complexity Analysis
</label>
@@ -813,7 +813,7 @@
<span class="md-ellipsis">
2.1 Algorithm efficiency assessment
2.1 Algorithm Efficiency Evaluation
@@ -841,7 +841,7 @@
<span class="md-ellipsis">
2.2 Iteration and recursion
2.2 Iteration and Recursion
@@ -869,7 +869,7 @@
<span class="md-ellipsis">
2.3 Time complexity
2.3 Time Complexity
@@ -897,7 +897,7 @@
<span class="md-ellipsis">
2.4 Space complexity
2.4 Space Complexity
@@ -992,7 +992,7 @@
<span class="md-ellipsis">
Chapter 3. Data structures
Chapter 3. Data Structures
@@ -1014,7 +1014,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 3. Data structures
Chapter 3. Data Structures
</label>
@@ -1036,7 +1036,7 @@
<span class="md-ellipsis">
3.1 Classification of data structures
3.1 Classification of Data Structures
@@ -1073,7 +1073,7 @@
<span class="md-ellipsis">
3.2 Basic data types
3.2 Basic Data Types
@@ -1102,7 +1102,7 @@
<span class="md-ellipsis">
3.3 Number encoding *
3.3 Number Encoding *
@@ -1130,7 +1130,7 @@
<span class="md-ellipsis">
3.4 Character encoding *
3.4 Character Encoding *
@@ -1223,7 +1223,7 @@
<span class="md-ellipsis">
Chapter 4. Array and linked list
Chapter 4. Array and Linked List
@@ -1245,7 +1245,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 4. Array and linked list
Chapter 4. Array and Linked List
</label>
@@ -1295,7 +1295,7 @@
<span class="md-ellipsis">
4.2 Linked list
4.2 Linked List
@@ -1351,7 +1351,7 @@
<span class="md-ellipsis">
4.4 Memory and cache *
4.4 Memory and Cache *
@@ -1442,7 +1442,7 @@
<span class="md-ellipsis">
Chapter 5. Stack and queue
Chapter 5. Stack and Queue
@@ -1464,7 +1464,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 5. Stack and queue
Chapter 5. Stack and Queue
</label>
@@ -1542,7 +1542,7 @@
<span class="md-ellipsis">
5.3 Double-ended queue
5.3 Double-Ended Queue
@@ -1633,7 +1633,7 @@
<span class="md-ellipsis">
Chapter 6. Hash table
Chapter 6. Hashing
@@ -1655,7 +1655,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 6. Hash table
Chapter 6. Hashing
</label>
@@ -1677,7 +1677,7 @@
<span class="md-ellipsis">
6.1 Hash table
6.1 Hash Table
@@ -1705,7 +1705,7 @@
<span class="md-ellipsis">
6.2 Hash collision
6.2 Hash Collision
@@ -1733,7 +1733,7 @@
<span class="md-ellipsis">
6.3 Hash algorithm
6.3 Hash Algorithm
@@ -1872,7 +1872,7 @@
<span class="md-ellipsis">
7.1 Binary tree
7.1 Binary Tree
@@ -1900,7 +1900,7 @@
<span class="md-ellipsis">
7.2 Binary tree traversal
7.2 Binary Tree Traversal
@@ -1928,7 +1928,7 @@
<span class="md-ellipsis">
7.3 Array Representation of tree
7.3 Array Representation of Tree
@@ -1956,7 +1956,7 @@
<span class="md-ellipsis">
7.4 Binary Search tree
7.4 Binary Search Tree
@@ -1984,7 +1984,7 @@
<span class="md-ellipsis">
7.5 AVL tree *
7.5 AVL Tree *
@@ -2147,7 +2147,7 @@
<span class="md-ellipsis">
8.2 Building a heap
8.2 Building a Heap
@@ -2175,7 +2175,7 @@
<span class="md-ellipsis">
8.3 Top-k problem
8.3 Top-K Problem
@@ -2338,7 +2338,7 @@
<span class="md-ellipsis">
9.2 Basic graph operations
9.2 Basic Operations on Graphs
@@ -2366,7 +2366,7 @@
<span class="md-ellipsis">
9.3 Graph traversal
9.3 Graph Traversal
@@ -2505,7 +2505,7 @@
<span class="md-ellipsis">
10.1 Binary search
10.1 Binary Search
@@ -2533,7 +2533,7 @@
<span class="md-ellipsis">
10.2 Binary search insertion
10.2 Binary Search Insertion
@@ -2561,7 +2561,7 @@
<span class="md-ellipsis">
10.3 Binary search boundaries
10.3 Binary Search Edge Cases
@@ -2589,7 +2589,7 @@
<span class="md-ellipsis">
10.4 Hashing optimization strategies
10.4 Hash Optimization Strategy
@@ -2617,7 +2617,7 @@
<span class="md-ellipsis">
10.5 Search algorithms revisited
10.5 Search Algorithms Revisited
@@ -2766,7 +2766,7 @@
<span class="md-ellipsis">
11.1 Sorting algorithms
11.1 Sorting Algorithms
@@ -2794,7 +2794,7 @@
<span class="md-ellipsis">
11.2 Selection sort
11.2 Selection Sort
@@ -2822,7 +2822,7 @@
<span class="md-ellipsis">
11.3 Bubble sort
11.3 Bubble Sort
@@ -2850,7 +2850,7 @@
<span class="md-ellipsis">
11.4 Insertion sort
11.4 Insertion Sort
@@ -2878,7 +2878,7 @@
<span class="md-ellipsis">
11.5 Quick sort
11.5 Quick Sort
@@ -2906,7 +2906,7 @@
<span class="md-ellipsis">
11.6 Merge sort
11.6 Merge Sort
@@ -2934,7 +2934,7 @@
<span class="md-ellipsis">
11.7 Heap sort
11.7 Heap Sort
@@ -2962,7 +2962,7 @@
<span class="md-ellipsis">
11.8 Bucket sort
11.8 Bucket Sort
@@ -2990,7 +2990,7 @@
<span class="md-ellipsis">
11.9 Counting sort
11.9 Counting Sort
@@ -3018,7 +3018,7 @@
<span class="md-ellipsis">
11.10 Radix sort
11.10 Radix Sort
@@ -3111,7 +3111,7 @@
<span class="md-ellipsis">
Chapter 12. Divide and conquer
Chapter 12. Divide and Conquer
@@ -3133,7 +3133,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 12. Divide and conquer
Chapter 12. Divide and Conquer
</label>
@@ -3155,7 +3155,7 @@
<span class="md-ellipsis">
12.1 Divide and conquer algorithms
12.1 Divide and Conquer Algorithms
@@ -3183,7 +3183,7 @@
<span class="md-ellipsis">
12.2 Divide and conquer search strategy
12.2 Divide and Conquer Search Strategy
@@ -3211,7 +3211,7 @@
<span class="md-ellipsis">
12.3 Building binary tree problem
12.3 Building a Binary Tree Problem
@@ -3239,7 +3239,7 @@
<span class="md-ellipsis">
12.4 Tower of Hanoi Problem
12.4 Hanoi Tower Problem
@@ -3376,7 +3376,7 @@
<span class="md-ellipsis">
13.1 Backtracking algorithms
13.1 Backtracking Algorithm
@@ -3404,7 +3404,7 @@
<span class="md-ellipsis">
13.2 Permutation problem
13.2 Permutations Problem
@@ -3432,7 +3432,7 @@
<span class="md-ellipsis">
13.3 Subset sum problem
13.3 Subset-Sum Problem
@@ -3460,7 +3460,7 @@
<span class="md-ellipsis">
13.4 n queens problem
13.4 N-Queens Problem
@@ -3557,7 +3557,7 @@
<span class="md-ellipsis">
Chapter 14. Dynamic programming
Chapter 14. Dynamic Programming
@@ -3579,7 +3579,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 14. Dynamic programming
Chapter 14. Dynamic Programming
</label>
@@ -3601,7 +3601,7 @@
<span class="md-ellipsis">
14.1 Introduction to dynamic programming
14.1 Introduction to Dynamic Programming
@@ -3629,7 +3629,7 @@
<span class="md-ellipsis">
14.2 Characteristics of DP problems
14.2 Characteristics of Dynamic Programming Problems
@@ -3657,7 +3657,7 @@
<span class="md-ellipsis">
14.3 DP problem-solving approach
14.3 Dynamic Programming Problem-Solving Approach
@@ -3685,7 +3685,7 @@
<span class="md-ellipsis">
14.4 0-1 Knapsack problem
14.4 0-1 Knapsack Problem
@@ -3713,7 +3713,7 @@
<span class="md-ellipsis">
14.5 Unbounded knapsack problem
14.5 Unbounded Knapsack Problem
@@ -3741,7 +3741,7 @@
<span class="md-ellipsis">
14.6 Edit distance problem
14.6 Edit Distance Problem
@@ -3878,7 +3878,7 @@
<span class="md-ellipsis">
15.1 Greedy algorithms
15.1 Greedy Algorithm
@@ -3906,7 +3906,7 @@
<span class="md-ellipsis">
15.2 Fractional knapsack problem
15.2 Fractional Knapsack Problem
@@ -3934,7 +3934,7 @@
<span class="md-ellipsis">
15.3 Maximum capacity problem
15.3 Maximum Capacity Problem
@@ -3962,7 +3962,7 @@
<span class="md-ellipsis">
15.4 Maximum product cutting problem
15.4 Maximum Product Cutting Problem
@@ -4095,7 +4095,7 @@
<span class="md-ellipsis">
16.1 Installation
16.1 Programming Environment Installation
@@ -4123,7 +4123,7 @@
<span class="md-ellipsis">
16.2 Contributing
16.2 Contributing Together
@@ -4151,7 +4151,7 @@
<span class="md-ellipsis">
16.3 Terminology
16.3 Terminology Table
@@ -4293,23 +4293,23 @@
<!-- Page content -->
<h1 id="32-basic-data-types">3.2 &nbsp; Basic data types<a class="headerlink" href="#32-basic-data-types" title="Permanent link">&para;</a></h1>
<p>When discussing data in computers, various forms like text, images, videos, voice and 3D models comes to mind. Despite their different organizational forms, they are all composed of various basic data types.</p>
<p><strong>Basic data types are those that the CPU can directly operate on</strong> and are directly used in algorithms, mainly including the following.</p>
<h1 id="32-basic-data-types">3.2 &nbsp; Basic Data Types<a class="headerlink" href="#32-basic-data-types" title="Permanent link">&para;</a></h1>
<p>When we talk about data in computers, we think of various forms such as text, images, videos, audio, 3D models, and more. Although these data are organized in different ways, they are all composed of various basic data types.</p>
<p><strong>Basic data types are types that the CPU can directly operate on</strong>, and they are directly used in algorithms, mainly including the following.</p>
<ul>
<li>Integer types: <code>byte</code>, <code>short</code>, <code>int</code>, <code>long</code>.</li>
<li>Floating-point types: <code>float</code>, <code>double</code>, used to represent decimals.</li>
<li>Character type: <code>char</code>, used to represent letters, punctuation, and even emojis in various languages.</li>
<li>Boolean type: <code>bool</code>, used to represent "yes" or "no" decisions.</li>
<li>Integer types <code>byte</code>, <code>short</code>, <code>int</code>, <code>long</code>.</li>
<li>Floating-point types <code>float</code>, <code>double</code>, used to represent decimal numbers.</li>
<li>Character type <code>char</code>, used to represent letters, punctuation marks, and even emojis in various languages.</li>
<li>Boolean type <code>bool</code>, used to represent "yes" and "no" judgments.</li>
</ul>
<p><strong>Basic data types are stored in computers in binary form</strong>. One binary digit is 1 bit. In most modern operating systems, 1 byte consists of 8 bits.</p>
<p>The range of values for basic data types depends on the size of the space they occupy. Below, we take Java as an example.</p>
<p><strong>Basic data types are stored in binary form in computers</strong>. One binary bit is <span class="arithmatex">\(1\)</span> bit. In most modern operating systems, <span class="arithmatex">\(1\)</span> byte consists of <span class="arithmatex">\(8\)</span> bits.</p>
<p>The range of values for basic data types depends on the size of the space they occupy. Below is an example using Java.</p>
<ul>
<li>The integer type <code>byte</code> occupies 1 byte = 8 bits and can represent <span class="arithmatex">\(2^8\)</span> numbers.</li>
<li>The integer type <code>int</code> occupies 4 bytes = 32 bits and can represent <span class="arithmatex">\(2^{32}\)</span> numbers.</li>
<li>Integer type <code>byte</code> occupies <span class="arithmatex">\(1\)</span> byte = <span class="arithmatex">\(8\)</span> bits, and can represent <span class="arithmatex">\(2^{8}\)</span> numbers.</li>
<li>Integer type <code>int</code> occupies <span class="arithmatex">\(4\)</span> bytes = <span class="arithmatex">\(32\)</span> bits, and can represent <span class="arithmatex">\(2^{32}\)</span> numbers.</li>
</ul>
<p>The following table lists the space occupied, value range, and default values of various basic data types in Java. While memorizing this table isn't necessary, having a general understanding of it and referencing it when required is recommended.</p>
<p align="center"> Table 3-1 &nbsp; Space occupied and value range of basic data types </p>
<p>The following table lists the space occupied, value ranges, and default values of various basic data types in Java. You don't need to memorize this table; a general understanding is sufficient, and you can refer to it when needed.</p>
<p align="center"> Table 3-1 &nbsp; Space occupied and value ranges of basic data types </p>
<div class="center-table">
<table>
@@ -4330,7 +4330,7 @@
<td>1 byte</td>
<td><span class="arithmatex">\(-2^7\)</span> (<span class="arithmatex">\(-128\)</span>)</td>
<td><span class="arithmatex">\(2^7 - 1\)</span> (<span class="arithmatex">\(127\)</span>)</td>
<td>0</td>
<td><span class="arithmatex">\(0\)</span></td>
</tr>
<tr>
<td></td>
@@ -4338,7 +4338,7 @@
<td>2 bytes</td>
<td><span class="arithmatex">\(-2^{15}\)</span></td>
<td><span class="arithmatex">\(2^{15} - 1\)</span></td>
<td>0</td>
<td><span class="arithmatex">\(0\)</span></td>
</tr>
<tr>
<td></td>
@@ -4346,7 +4346,7 @@
<td>4 bytes</td>
<td><span class="arithmatex">\(-2^{31}\)</span></td>
<td><span class="arithmatex">\(2^{31} - 1\)</span></td>
<td>0</td>
<td><span class="arithmatex">\(0\)</span></td>
</tr>
<tr>
<td></td>
@@ -4354,7 +4354,7 @@
<td>8 bytes</td>
<td><span class="arithmatex">\(-2^{63}\)</span></td>
<td><span class="arithmatex">\(2^{63} - 1\)</span></td>
<td>0</td>
<td><span class="arithmatex">\(0\)</span></td>
</tr>
<tr>
<td>Float</td>
@@ -4370,15 +4370,15 @@
<td>8 bytes</td>
<td><span class="arithmatex">\(2.225 \times 10^{-308}\)</span></td>
<td><span class="arithmatex">\(1.798 \times 10^{308}\)</span></td>
<td>0.0</td>
<td><span class="arithmatex">\(0.0\)</span></td>
</tr>
<tr>
<td>Char</td>
<td>Character</td>
<td><code>char</code></td>
<td>2 bytes</td>
<td>0</td>
<td><span class="arithmatex">\(0\)</span></td>
<td><span class="arithmatex">\(2^{16} - 1\)</span></td>
<td>0</td>
<td><span class="arithmatex">\(0\)</span></td>
</tr>
<tr>
<td>Boolean</td>
@@ -4391,31 +4391,31 @@
</tbody>
</table>
</div>
<p>Please note that the above table is specific to Java's basic data types. Every programming language has its own data type definitions, which might differ in space occupied, value ranges, and default values.</p>
<p>Please note that the above table is specific to Java's basic data types. Each programming language has its own data type definitions, and their space occupied, value ranges, and default values may vary.</p>
<ul>
<li>In Python, the integer type <code>int</code> can be of any size, limited only by available memory; the floating-point <code>float</code> is double precision 64-bit; there is no <code>char</code> type, as a single character is actually a string <code>str</code> of length 1.</li>
<li>C and C++ do not specify the size of basic data types, it varies with implementation and platform. The above table follows the LP64 <a href="https://en.cppreference.com/w/cpp/language/types#Properties">data model</a>, used for Unix 64-bit operating systems including Linux and macOS.</li>
<li>The size of <code>char</code> in C and C++ is 1 byte, while in most programming languages, it depends on the specific character encoding method, as detailed in the "Character Encoding" chapter.</li>
<li>Even though representing a boolean only requires 1 bit (0 or 1), it is usually stored in memory as 1 byte. This is because modern computer CPUs typically use 1 byte as the smallest addressable memory unit.</li>
<li>In Python, the integer type <code>int</code> can be of any size, limited only by available memory; the floating-point type <code>float</code> is double-precision 64-bit; there is no <code>char</code> type, a single character is actually a string <code>str</code> of length 1.</li>
<li>C and C++ do not explicitly specify the size of basic data types, which varies by implementation and platform. The above table follows the LP64 <a href="https://en.cppreference.com/w/cpp/language/types#Properties">data model</a>, which is used in Unix 64-bit operating systems including Linux and macOS.</li>
<li>The size of character <code>char</code> is 1 byte in C and C++, and in most programming languages it depends on the specific character encoding method, as detailed in the "Character Encoding" section.</li>
<li>Even though representing a boolean value requires only 1 bit (<span class="arithmatex">\(0\)</span> or <span class="arithmatex">\(1\)</span>), it is usually stored as 1 byte in memory. This is because modern computer CPUs typically use 1 byte as the minimum addressable memory unit.</li>
</ul>
<p>So, what is the connection between basic data types and data structures? We know that data structures are ways to organize and store data in computers. The focus here is on "structure" rather than "data".</p>
<p>If we want to represent "a row of numbers", we naturally think of using an array. This is because the linear structure of an array can represent the adjacency and the ordering of the numbers, but whether the stored content is an integer <code>int</code>, a decimal <code>float</code>, or a character <code>char</code>, is irrelevant to the "data structure".</p>
<p>In other words, <strong>basic data types provide the "content type" of data, while data structures provide the "way of organizing" data</strong>. For example, in the following code, we use the same data structure (array) to store and represent different basic data types, including <code>int</code>, <code>float</code>, <code>char</code>, <code>bool</code>, etc.</p>
<div class="tabbed-set tabbed-alternate" data-tabs="1:13"><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" /><input id="__tabbed_1_8" name="__tabbed_1" type="radio" /><input id="__tabbed_1_9" name="__tabbed_1" type="radio" /><input id="__tabbed_1_10" name="__tabbed_1" type="radio" /><input id="__tabbed_1_11" name="__tabbed_1" type="radio" /><input id="__tabbed_1_12" name="__tabbed_1" type="radio" /><input id="__tabbed_1_13" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Java</label><label for="__tabbed_1_4">C#</label><label for="__tabbed_1_5">Go</label><label for="__tabbed_1_6">Swift</label><label for="__tabbed_1_7">JS</label><label for="__tabbed_1_8">TS</label><label for="__tabbed_1_9">Dart</label><label for="__tabbed_1_10">Rust</label><label for="__tabbed_1_11">C</label><label for="__tabbed_1_12">Kotlin</label><label for="__tabbed_1_13">Zig</label></div>
<p>So, what is the relationship between basic data types and data structures? We know that data structures are ways of organizing and storing data in computers. The subject of this statement is "structure", not "data".</p>
<p>If we want to represent "a row of numbers", we naturally think of using an array. This is because the linear structure of an array can represent the adjacency and order relationships of numbers, but the content stored—whether integer <code>int</code>, floating-point <code>float</code>, or character <code>char</code>is unrelated to the "data structure".</p>
<p>In other words, <strong>basic data types provide the "content type" of data, while data structures provide the "organization method" of data</strong>. For example, in the following code, we use the same data structure (array) to store and represent different basic data types, including <code>int</code>, <code>float</code>, <code>char</code>, <code>bool</code>, etc.</p>
<div class="tabbed-set tabbed-alternate" data-tabs="1:13"><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" /><input id="__tabbed_1_8" name="__tabbed_1" type="radio" /><input id="__tabbed_1_9" name="__tabbed_1" type="radio" /><input id="__tabbed_1_10" name="__tabbed_1" type="radio" /><input id="__tabbed_1_11" name="__tabbed_1" type="radio" /><input id="__tabbed_1_12" name="__tabbed_1" type="radio" /><input id="__tabbed_1_13" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Java</label><label for="__tabbed_1_4">C#</label><label for="__tabbed_1_5">Go</label><label for="__tabbed_1_6">Swift</label><label for="__tabbed_1_7">JS</label><label for="__tabbed_1_8">TS</label><label for="__tabbed_1_9">Dart</label><label for="__tabbed_1_10">Rust</label><label for="__tabbed_1_11">C</label><label for="__tabbed_1_12">Kotlin</label><label for="__tabbed_1_13">Ruby</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="c1"># Using various basic data types to initialize arrays</span>
<div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="c1"># Initialize arrays using various basic data types</span>
<a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="n">numbers</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="mi">5</span>
<a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="n">decimals</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">float</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.0</span><span class="p">]</span> <span class="o">*</span> <span class="mi">5</span>
<a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="c1"># Python&#39;s characters are actually strings of length 1</span>
<a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="c1"># In Python, characters are actually strings of length 1</span>
<a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="n">characters</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;0&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="mi">5</span>
<a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="n">bools</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">bool</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="kc">False</span><span class="p">]</span> <span class="o">*</span> <span class="mi">5</span>
<a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="c1"># Python&#39;s lists can freely store various basic data types and object references</span>
<a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="c1"># Python lists can freely store various basic data types and object references</span>
<a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="n">data</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="n">ListNode</span><span class="p">(</span><span class="mi">0</span><span class="p">)]</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="c1">// Using various basic data types to initialize arrays</span>
<div class="highlight"><pre><span></span><code><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="c1">// Initialize arrays using various basic data types</span>
<a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="kt">int</span><span class="w"> </span><span class="n">numbers</span><span class="p">[</span><span class="mi">5</span><span class="p">];</span>
<a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="kt">float</span><span class="w"> </span><span class="n">decimals</span><span class="p">[</span><span class="mi">5</span><span class="p">];</span>
<a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="kt">char</span><span class="w"> </span><span class="n">characters</span><span class="p">[</span><span class="mi">5</span><span class="p">];</span>
@@ -4423,7 +4423,7 @@
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="c1">// Using various basic data types to initialize arrays</span>
<div class="highlight"><pre><span></span><code><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="c1">// Initialize arrays using various basic data types</span>
<a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="kt">int</span><span class="o">[]</span><span class="w"> </span><span class="n">numbers</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="kt">int</span><span class="o">[</span><span class="mi">5</span><span class="o">]</span><span class="p">;</span>
<a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="kt">float</span><span class="o">[]</span><span class="w"> </span><span class="n">decimals</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="kt">float</span><span class="o">[</span><span class="mi">5</span><span class="o">]</span><span class="p">;</span>
<a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="kt">char</span><span class="o">[]</span><span class="w"> </span><span class="n">characters</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="kt">char</span><span class="o">[</span><span class="mi">5</span><span class="o">]</span><span class="p">;</span>
@@ -4431,7 +4431,7 @@
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="c1">// Using various basic data types to initialize arrays</span>
<div class="highlight"><pre><span></span><code><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="c1">// Initialize arrays using various basic data types</span>
<a id="__codelineno-3-2" name="__codelineno-3-2" href="#__codelineno-3-2"></a><span class="kt">int</span><span class="p">[]</span><span class="w"> </span><span class="n">numbers</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="kt">int</span><span class="p">[</span><span class="mi">5</span><span class="p">];</span>
<a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a><span class="kt">float</span><span class="p">[]</span><span class="w"> </span><span class="n">decimals</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="kt">float</span><span class="p">[</span><span class="mi">5</span><span class="p">];</span>
<a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a><span class="kt">char</span><span class="p">[]</span><span class="w"> </span><span class="n">characters</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="kt">char</span><span class="p">[</span><span class="mi">5</span><span class="p">];</span>
@@ -4439,7 +4439,7 @@
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-4-1" name="__codelineno-4-1" href="#__codelineno-4-1"></a><span class="c1">// Using various basic data types to initialize arrays</span>
<div class="highlight"><pre><span></span><code><a id="__codelineno-4-1" name="__codelineno-4-1" href="#__codelineno-4-1"></a><span class="c1">// Initialize arrays using various basic data types</span>
<a id="__codelineno-4-2" name="__codelineno-4-2" href="#__codelineno-4-2"></a><span class="kd">var</span><span class="w"> </span><span class="nx">numbers</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="kt">int</span><span class="p">{}</span>
<a id="__codelineno-4-3" name="__codelineno-4-3" href="#__codelineno-4-3"></a><span class="kd">var</span><span class="w"> </span><span class="nx">decimals</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="kt">float64</span><span class="p">{}</span>
<a id="__codelineno-4-4" name="__codelineno-4-4" href="#__codelineno-4-4"></a><span class="kd">var</span><span class="w"> </span><span class="nx">characters</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="kt">byte</span><span class="p">{}</span>
@@ -4447,7 +4447,7 @@
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-5-1" name="__codelineno-5-1" href="#__codelineno-5-1"></a><span class="c1">// Using various basic data types to initialize arrays</span>
<div class="highlight"><pre><span></span><code><a id="__codelineno-5-1" name="__codelineno-5-1" href="#__codelineno-5-1"></a><span class="c1">// Initialize arrays using various basic data types</span>
<a id="__codelineno-5-2" name="__codelineno-5-2" href="#__codelineno-5-2"></a><span class="kd">let</span><span class="w"> </span><span class="nv">numbers</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">Array</span><span class="p">(</span><span class="n">repeating</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="bp">count</span><span class="p">:</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span>
<a id="__codelineno-5-3" name="__codelineno-5-3" href="#__codelineno-5-3"></a><span class="kd">let</span><span class="w"> </span><span class="nv">decimals</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">Array</span><span class="p">(</span><span class="n">repeating</span><span class="p">:</span><span class="w"> </span><span class="mf">0.0</span><span class="p">,</span><span class="w"> </span><span class="bp">count</span><span class="p">:</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span>
<a id="__codelineno-5-4" name="__codelineno-5-4" href="#__codelineno-5-4"></a><span class="kd">let</span><span class="w"> </span><span class="nv">characters</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="nb">Character</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">Array</span><span class="p">(</span><span class="n">repeating</span><span class="p">:</span><span class="w"> </span><span class="s">&quot;a&quot;</span><span class="p">,</span><span class="w"> </span><span class="bp">count</span><span class="p">:</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span>
@@ -4455,19 +4455,19 @@
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-6-1" name="__codelineno-6-1" href="#__codelineno-6-1"></a><span class="c1">// JavaScript&#39;s arrays can freely store various basic data types and objects</span>
<div class="highlight"><pre><span></span><code><a id="__codelineno-6-1" name="__codelineno-6-1" href="#__codelineno-6-1"></a><span class="c1">// JavaScript arrays can freely store various basic data types and objects</span>
<a id="__codelineno-6-2" name="__codelineno-6-2" href="#__codelineno-6-2"></a><span class="kd">const</span><span class="w"> </span><span class="nx">array</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="mf">0.0</span><span class="p">,</span><span class="w"> </span><span class="s1">&#39;a&#39;</span><span class="p">,</span><span class="w"> </span><span class="kc">false</span><span class="p">];</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-7-1" name="__codelineno-7-1" href="#__codelineno-7-1"></a><span class="c1">// Using various basic data types to initialize arrays</span>
<div class="highlight"><pre><span></span><code><a id="__codelineno-7-1" name="__codelineno-7-1" href="#__codelineno-7-1"></a><span class="c1">// Initialize arrays using various basic data types</span>
<a id="__codelineno-7-2" name="__codelineno-7-2" href="#__codelineno-7-2"></a><span class="kd">const</span><span class="w"> </span><span class="nx">numbers</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
<a id="__codelineno-7-3" name="__codelineno-7-3" href="#__codelineno-7-3"></a><span class="kd">const</span><span class="w"> </span><span class="nx">characters</span><span class="o">:</span><span class="w"> </span><span class="kt">string</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
<a id="__codelineno-7-4" name="__codelineno-7-4" href="#__codelineno-7-4"></a><span class="kd">const</span><span class="w"> </span><span class="nx">bools</span><span class="o">:</span><span class="w"> </span><span class="kt">boolean</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-8-1" name="__codelineno-8-1" href="#__codelineno-8-1"></a><span class="c1">// Using various basic data types to initialize arrays</span>
<div class="highlight"><pre><span></span><code><a id="__codelineno-8-1" name="__codelineno-8-1" href="#__codelineno-8-1"></a><span class="c1">// Initialize arrays using various basic data types</span>
<a id="__codelineno-8-2" name="__codelineno-8-2" href="#__codelineno-8-2"></a><span class="n">List</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="n">numbers</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">List</span><span class="p">.</span><span class="n">filled</span><span class="p">(</span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="m">0</span><span class="p">);</span>
<a id="__codelineno-8-3" name="__codelineno-8-3" href="#__codelineno-8-3"></a><span class="n">List</span><span class="o">&lt;</span><span class="kt">double</span><span class="o">&gt;</span><span class="w"> </span><span class="n">decimals</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">List</span><span class="p">.</span><span class="n">filled</span><span class="p">(</span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="m">0.0</span><span class="p">);</span>
<a id="__codelineno-8-4" name="__codelineno-8-4" href="#__codelineno-8-4"></a><span class="n">List</span><span class="o">&lt;</span><span class="kt">String</span><span class="o">&gt;</span><span class="w"> </span><span class="n">characters</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">List</span><span class="p">.</span><span class="n">filled</span><span class="p">(</span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="s1">&#39;a&#39;</span><span class="p">);</span>
@@ -4475,15 +4475,15 @@
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-9-1" name="__codelineno-9-1" href="#__codelineno-9-1"></a><span class="c1">// Using various basic data types to initialize arrays</span>
<div class="highlight"><pre><span></span><code><a id="__codelineno-9-1" name="__codelineno-9-1" href="#__codelineno-9-1"></a><span class="c1">// Initialize arrays using various basic data types</span>
<a id="__codelineno-9-2" name="__codelineno-9-2" href="#__codelineno-9-2"></a><span class="kd">let</span><span class="w"> </span><span class="n">numbers</span><span class="p">:</span><span class="w"> </span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">i32</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="mi">5</span><span class="p">];</span>
<a id="__codelineno-9-3" name="__codelineno-9-3" href="#__codelineno-9-3"></a><span class="kd">let</span><span class="w"> </span><span class="n">decimals</span><span class="p">:</span><span class="w"> </span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">f32</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mf">0.0</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">];</span>
<a id="__codelineno-9-3" name="__codelineno-9-3" href="#__codelineno-9-3"></a><span class="kd">let</span><span class="w"> </span><span class="n">decimals</span><span class="p">:</span><span class="w"> </span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">f32</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mf">0.0</span><span class="p">;</span><span class="w"> </span><span class="mi">5</span><span class="p">];</span>
<a id="__codelineno-9-4" name="__codelineno-9-4" href="#__codelineno-9-4"></a><span class="kd">let</span><span class="w"> </span><span class="n">characters</span><span class="p">:</span><span class="w"> </span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">char</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="sc">&#39;0&#39;</span><span class="p">;</span><span class="w"> </span><span class="mi">5</span><span class="p">];</span>
<a id="__codelineno-9-5" name="__codelineno-9-5" href="#__codelineno-9-5"></a><span class="kd">let</span><span class="w"> </span><span class="n">bools</span><span class="p">:</span><span class="w"> </span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">bool</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="kc">false</span><span class="p">;</span><span class="w"> </span><span class="mi">5</span><span class="p">];</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-10-1" name="__codelineno-10-1" href="#__codelineno-10-1"></a><span class="c1">// Using various basic data types to initialize arrays</span>
<div class="highlight"><pre><span></span><code><a id="__codelineno-10-1" name="__codelineno-10-1" href="#__codelineno-10-1"></a><span class="c1">// Initialize arrays using various basic data types</span>
<a id="__codelineno-10-2" name="__codelineno-10-2" href="#__codelineno-10-2"></a><span class="kt">int</span><span class="w"> </span><span class="n">numbers</span><span class="p">[</span><span class="mi">10</span><span class="p">];</span>
<a id="__codelineno-10-3" name="__codelineno-10-3" href="#__codelineno-10-3"></a><span class="kt">float</span><span class="w"> </span><span class="n">decimals</span><span class="p">[</span><span class="mi">10</span><span class="p">];</span>
<a id="__codelineno-10-4" name="__codelineno-10-4" href="#__codelineno-10-4"></a><span class="kt">char</span><span class="w"> </span><span class="n">characters</span><span class="p">[</span><span class="mi">10</span><span class="p">];</span>
@@ -4491,19 +4491,24 @@
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-11-1" name="__codelineno-11-1" href="#__codelineno-11-1"></a>
<div class="highlight"><pre><span></span><code><a id="__codelineno-11-1" name="__codelineno-11-1" href="#__codelineno-11-1"></a><span class="c1">// Initialize arrays using various basic data types</span>
<a id="__codelineno-11-2" name="__codelineno-11-2" href="#__codelineno-11-2"></a><span class="kd">val</span><span class="w"> </span><span class="nv">numbers</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">IntArray</span><span class="p">(</span><span class="m">5</span><span class="p">)</span>
<a id="__codelineno-11-3" name="__codelineno-11-3" href="#__codelineno-11-3"></a><span class="kd">val</span><span class="w"> </span><span class="nv">decinals</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">FloatArray</span><span class="p">(</span><span class="m">5</span><span class="p">)</span>
<a id="__codelineno-11-4" name="__codelineno-11-4" href="#__codelineno-11-4"></a><span class="kd">val</span><span class="w"> </span><span class="nv">characters</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">CharArray</span><span class="p">(</span><span class="m">5</span><span class="p">)</span>
<a id="__codelineno-11-5" name="__codelineno-11-5" href="#__codelineno-11-5"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bools</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">BooleanArray</span><span class="p">(</span><span class="m">5</span><span class="p">)</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="c1">// Using various basic data types to initialize arrays</span>
<a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a><span class="kr">var</span><span class="w"> </span><span class="n">numbers</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">undefined</span><span class="p">;</span>
<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a><span class="kr">var</span><span class="w"> </span><span class="n">decimals</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="kt">f32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">undefined</span><span class="p">;</span>
<a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a><span class="kr">var</span><span class="w"> </span><span class="n">characters</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="kt">u8</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">undefined</span><span class="p">;</span>
<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a><span class="kr">var</span><span class="w"> </span><span class="n">bools</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="kt">bool</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">undefined</span><span class="p">;</span>
<div class="highlight"><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="c1"># Ruby lists can freely store various basic data types and object references</span>
<a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="o">.</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="s1">&#39;a&#39;</span><span class="p">,</span><span class="w"> </span><span class="kp">false</span><span class="p">,</span><span class="w"> </span><span class="no">ListNode</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span><span class="o">]</span>
</code></pre></div>
</div>
</div>
</div>
<details class="pythontutor">
<summary>Visualized Execution</summary>
<p>https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E4%BD%BF%E7%94%A8%E5%A4%9A%E7%A7%8D%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E6%9D%A5%E5%88%9D%E5%A7%8B%E5%8C%96%E6%95%B0%E7%BB%84%0A%20%20%20%20numbers%20%3D%20%5B0%5D%20*%205%0A%20%20%20%20decimals%20%3D%20%5B0.0%5D%20*%205%0A%20%20%20%20%23%20Python%20%E7%9A%84%E5%AD%97%E7%AC%A6%E5%AE%9E%E9%99%85%E4%B8%8A%E6%98%AF%E9%95%BF%E5%BA%A6%E4%B8%BA%201%20%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2%0A%20%20%20%20characters%20%3D%20%5B'0'%5D%20*%205%0A%20%20%20%20bools%20%3D%20%5BFalse%5D%20*%205%0A%20%20%20%20%23%20Python%20%E7%9A%84%E5%88%97%E8%A1%A8%E5%8F%AF%E4%BB%A5%E8%87%AA%E7%94%B1%E5%AD%98%E5%82%A8%E5%90%84%E7%A7%8D%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%92%8C%E5%AF%B9%E8%B1%A1%E5%BC%95%E7%94%A8%0A%20%20%20%20data%20%3D%20%5B0,%200.0,%20'a',%20False,%20ListNode%280%29%5D&amp;cumulative=false&amp;curInstr=12&amp;heapPrimitives=nevernest&amp;mode=display&amp;origin=opt-frontend.js&amp;py=311&amp;rawInputLstJSON=%5B%5D&amp;textReferences=false</p>
</details>
<!-- Source file information -->
@@ -4526,7 +4531,7 @@ aria-label="Footer"
<a
href="../classification_of_data_structure/"
class="md-footer__link md-footer__link--prev"
aria-label="Previous: 3.1 Classification of data structures"
aria-label="Previous: 3.1 Classification of Data Structures"
rel="prev"
>
<div class="md-footer__button md-icon">
@@ -4538,7 +4543,7 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
3.1 Classification of data structures
3.1 Classification of Data Structures
</div>
</div>
</a>
@@ -4550,7 +4555,7 @@ aria-label="Footer"
<a
href="../number_encoding/"
class="md-footer__link md-footer__link--next"
aria-label="Next: 3.3 Number encoding *"
aria-label="Next: 3.3 Number Encoding *"
rel="next"
>
<div class="md-footer__title">
@@ -4558,7 +4563,7 @@ aria-label="Footer"
Next
</span>
<div class="md-ellipsis">
3.3 Number encoding *
3.3 Number Encoding *
</div>
</div>
<div class="md-footer__button md-icon">
@@ -4651,7 +4656,7 @@ aria-label="Footer"
<nav class="md-footer__inner md-grid" aria-label="Footer" >
<a href="../classification_of_data_structure/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 3.1 Classification of data structures">
<a href="../classification_of_data_structure/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 3.1 Classification of Data Structures">
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11z"/></svg>
@@ -4661,20 +4666,20 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
3.1 Classification of data structures
3.1 Classification of Data Structures
</div>
</div>
</a>
<a href="../number_encoding/" class="md-footer__link md-footer__link--next" aria-label="Next: 3.3 Number encoding *">
<a href="../number_encoding/" class="md-footer__link md-footer__link--next" aria-label="Next: 3.3 Number Encoding *">
<div class="md-footer__title">
<span class="md-footer__direction">
Next
</span>
<div class="md-ellipsis">
3.3 Number encoding *
3.3 Number Encoding *
</div>
</div>
<div class="md-footer__button md-icon">

View File

@@ -37,7 +37,7 @@
<title>3.4 Character encoding * - Hello Algo</title>
<title>3.4 Character Encoding * - Hello Algo</title>
@@ -58,8 +58,8 @@
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
<style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:300,300i,400,400i,700,700i%7CJetBrains+Mono:400,400i,700,700i&display=fallback">
<style>:root{--md-text-font:"Lato";--md-code-font:"JetBrains Mono"}</style>
@@ -154,7 +154,7 @@
<div class="md-header__topic" data-md-component="header-topic">
<span class="md-ellipsis">
3.4 Character encoding *
3.4 Character Encoding *
</span>
</div>
@@ -371,7 +371,7 @@
<span class="md-ellipsis">
Before starting
Before Starting
@@ -388,7 +388,7 @@
<span class="md-nav__icon md-icon"></span>
Before starting
Before Starting
</label>
@@ -487,7 +487,7 @@
<span class="md-ellipsis">
0.1 About this book
0.1 About This Book
@@ -515,7 +515,7 @@
<span class="md-ellipsis">
0.2 How to read
0.2 How to Use This Book
@@ -604,7 +604,7 @@
<span class="md-ellipsis">
Chapter 1. Encounter with algorithms
Chapter 1. Encounter With Algorithms
@@ -626,7 +626,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 1. Encounter with algorithms
Chapter 1. Encounter With Algorithms
</label>
@@ -648,7 +648,7 @@
<span class="md-ellipsis">
1.1 Algorithms are everywhere
1.1 Algorithms Are Everywhere
@@ -676,7 +676,7 @@
<span class="md-ellipsis">
1.2 What is an algorithm
1.2 What Is an Algorithm
@@ -769,7 +769,7 @@
<span class="md-ellipsis">
Chapter 2. Complexity analysis
Chapter 2. Complexity Analysis
@@ -791,7 +791,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 2. Complexity analysis
Chapter 2. Complexity Analysis
</label>
@@ -813,7 +813,7 @@
<span class="md-ellipsis">
2.1 Algorithm efficiency assessment
2.1 Algorithm Efficiency Evaluation
@@ -841,7 +841,7 @@
<span class="md-ellipsis">
2.2 Iteration and recursion
2.2 Iteration and Recursion
@@ -869,7 +869,7 @@
<span class="md-ellipsis">
2.3 Time complexity
2.3 Time Complexity
@@ -897,7 +897,7 @@
<span class="md-ellipsis">
2.4 Space complexity
2.4 Space Complexity
@@ -992,7 +992,7 @@
<span class="md-ellipsis">
Chapter 3. Data structures
Chapter 3. Data Structures
@@ -1014,7 +1014,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 3. Data structures
Chapter 3. Data Structures
</label>
@@ -1036,7 +1036,7 @@
<span class="md-ellipsis">
3.1 Classification of data structures
3.1 Classification of Data Structures
@@ -1064,7 +1064,7 @@
<span class="md-ellipsis">
3.2 Basic data types
3.2 Basic Data Types
@@ -1092,7 +1092,7 @@
<span class="md-ellipsis">
3.3 Number encoding *
3.3 Number Encoding *
@@ -1129,7 +1129,7 @@
<span class="md-ellipsis">
3.4 Character encoding *
3.4 Character Encoding *
@@ -1147,7 +1147,7 @@
<span class="md-ellipsis">
3.4 Character encoding *
3.4 Character Encoding *
@@ -1176,7 +1176,7 @@
<a href="#341-ascii-character-set" class="md-nav__link">
<span class="md-ellipsis">
3.4.1 &nbsp; ASCII character set
3.4.1 &nbsp; Ascii Character Set
</span>
</a>
@@ -1187,7 +1187,7 @@
<a href="#342-gbk-character-set" class="md-nav__link">
<span class="md-ellipsis">
3.4.2 &nbsp; GBK character set
3.4.2 &nbsp; Gbk Character Set
</span>
</a>
@@ -1198,7 +1198,7 @@
<a href="#343-unicode-character-set" class="md-nav__link">
<span class="md-ellipsis">
3.4.3 &nbsp; Unicode character set
3.4.3 &nbsp; Unicode Character Set
</span>
</a>
@@ -1209,7 +1209,7 @@
<a href="#344-utf-8-encoding" class="md-nav__link">
<span class="md-ellipsis">
3.4.4 &nbsp; UTF-8 encoding
3.4.4 &nbsp; Utf-8 Encoding
</span>
</a>
@@ -1220,7 +1220,7 @@
<a href="#345-character-encoding-in-programming-languages" class="md-nav__link">
<span class="md-ellipsis">
3.4.5 &nbsp; Character encoding in programming languages
3.4.5 &nbsp; Character Encoding in Programming Languages
</span>
</a>
@@ -1315,7 +1315,7 @@
<span class="md-ellipsis">
Chapter 4. Array and linked list
Chapter 4. Array and Linked List
@@ -1337,7 +1337,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 4. Array and linked list
Chapter 4. Array and Linked List
</label>
@@ -1387,7 +1387,7 @@
<span class="md-ellipsis">
4.2 Linked list
4.2 Linked List
@@ -1443,7 +1443,7 @@
<span class="md-ellipsis">
4.4 Memory and cache *
4.4 Memory and Cache *
@@ -1534,7 +1534,7 @@
<span class="md-ellipsis">
Chapter 5. Stack and queue
Chapter 5. Stack and Queue
@@ -1556,7 +1556,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 5. Stack and queue
Chapter 5. Stack and Queue
</label>
@@ -1634,7 +1634,7 @@
<span class="md-ellipsis">
5.3 Double-ended queue
5.3 Double-Ended Queue
@@ -1725,7 +1725,7 @@
<span class="md-ellipsis">
Chapter 6. Hash table
Chapter 6. Hashing
@@ -1747,7 +1747,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 6. Hash table
Chapter 6. Hashing
</label>
@@ -1769,7 +1769,7 @@
<span class="md-ellipsis">
6.1 Hash table
6.1 Hash Table
@@ -1797,7 +1797,7 @@
<span class="md-ellipsis">
6.2 Hash collision
6.2 Hash Collision
@@ -1825,7 +1825,7 @@
<span class="md-ellipsis">
6.3 Hash algorithm
6.3 Hash Algorithm
@@ -1964,7 +1964,7 @@
<span class="md-ellipsis">
7.1 Binary tree
7.1 Binary Tree
@@ -1992,7 +1992,7 @@
<span class="md-ellipsis">
7.2 Binary tree traversal
7.2 Binary Tree Traversal
@@ -2020,7 +2020,7 @@
<span class="md-ellipsis">
7.3 Array Representation of tree
7.3 Array Representation of Tree
@@ -2048,7 +2048,7 @@
<span class="md-ellipsis">
7.4 Binary Search tree
7.4 Binary Search Tree
@@ -2076,7 +2076,7 @@
<span class="md-ellipsis">
7.5 AVL tree *
7.5 AVL Tree *
@@ -2239,7 +2239,7 @@
<span class="md-ellipsis">
8.2 Building a heap
8.2 Building a Heap
@@ -2267,7 +2267,7 @@
<span class="md-ellipsis">
8.3 Top-k problem
8.3 Top-K Problem
@@ -2430,7 +2430,7 @@
<span class="md-ellipsis">
9.2 Basic graph operations
9.2 Basic Operations on Graphs
@@ -2458,7 +2458,7 @@
<span class="md-ellipsis">
9.3 Graph traversal
9.3 Graph Traversal
@@ -2597,7 +2597,7 @@
<span class="md-ellipsis">
10.1 Binary search
10.1 Binary Search
@@ -2625,7 +2625,7 @@
<span class="md-ellipsis">
10.2 Binary search insertion
10.2 Binary Search Insertion
@@ -2653,7 +2653,7 @@
<span class="md-ellipsis">
10.3 Binary search boundaries
10.3 Binary Search Edge Cases
@@ -2681,7 +2681,7 @@
<span class="md-ellipsis">
10.4 Hashing optimization strategies
10.4 Hash Optimization Strategy
@@ -2709,7 +2709,7 @@
<span class="md-ellipsis">
10.5 Search algorithms revisited
10.5 Search Algorithms Revisited
@@ -2858,7 +2858,7 @@
<span class="md-ellipsis">
11.1 Sorting algorithms
11.1 Sorting Algorithms
@@ -2886,7 +2886,7 @@
<span class="md-ellipsis">
11.2 Selection sort
11.2 Selection Sort
@@ -2914,7 +2914,7 @@
<span class="md-ellipsis">
11.3 Bubble sort
11.3 Bubble Sort
@@ -2942,7 +2942,7 @@
<span class="md-ellipsis">
11.4 Insertion sort
11.4 Insertion Sort
@@ -2970,7 +2970,7 @@
<span class="md-ellipsis">
11.5 Quick sort
11.5 Quick Sort
@@ -2998,7 +2998,7 @@
<span class="md-ellipsis">
11.6 Merge sort
11.6 Merge Sort
@@ -3026,7 +3026,7 @@
<span class="md-ellipsis">
11.7 Heap sort
11.7 Heap Sort
@@ -3054,7 +3054,7 @@
<span class="md-ellipsis">
11.8 Bucket sort
11.8 Bucket Sort
@@ -3082,7 +3082,7 @@
<span class="md-ellipsis">
11.9 Counting sort
11.9 Counting Sort
@@ -3110,7 +3110,7 @@
<span class="md-ellipsis">
11.10 Radix sort
11.10 Radix Sort
@@ -3203,7 +3203,7 @@
<span class="md-ellipsis">
Chapter 12. Divide and conquer
Chapter 12. Divide and Conquer
@@ -3225,7 +3225,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 12. Divide and conquer
Chapter 12. Divide and Conquer
</label>
@@ -3247,7 +3247,7 @@
<span class="md-ellipsis">
12.1 Divide and conquer algorithms
12.1 Divide and Conquer Algorithms
@@ -3275,7 +3275,7 @@
<span class="md-ellipsis">
12.2 Divide and conquer search strategy
12.2 Divide and Conquer Search Strategy
@@ -3303,7 +3303,7 @@
<span class="md-ellipsis">
12.3 Building binary tree problem
12.3 Building a Binary Tree Problem
@@ -3331,7 +3331,7 @@
<span class="md-ellipsis">
12.4 Tower of Hanoi Problem
12.4 Hanoi Tower Problem
@@ -3468,7 +3468,7 @@
<span class="md-ellipsis">
13.1 Backtracking algorithms
13.1 Backtracking Algorithm
@@ -3496,7 +3496,7 @@
<span class="md-ellipsis">
13.2 Permutation problem
13.2 Permutations Problem
@@ -3524,7 +3524,7 @@
<span class="md-ellipsis">
13.3 Subset sum problem
13.3 Subset-Sum Problem
@@ -3552,7 +3552,7 @@
<span class="md-ellipsis">
13.4 n queens problem
13.4 N-Queens Problem
@@ -3649,7 +3649,7 @@
<span class="md-ellipsis">
Chapter 14. Dynamic programming
Chapter 14. Dynamic Programming
@@ -3671,7 +3671,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 14. Dynamic programming
Chapter 14. Dynamic Programming
</label>
@@ -3693,7 +3693,7 @@
<span class="md-ellipsis">
14.1 Introduction to dynamic programming
14.1 Introduction to Dynamic Programming
@@ -3721,7 +3721,7 @@
<span class="md-ellipsis">
14.2 Characteristics of DP problems
14.2 Characteristics of Dynamic Programming Problems
@@ -3749,7 +3749,7 @@
<span class="md-ellipsis">
14.3 DP problem-solving approach
14.3 Dynamic Programming Problem-Solving Approach
@@ -3777,7 +3777,7 @@
<span class="md-ellipsis">
14.4 0-1 Knapsack problem
14.4 0-1 Knapsack Problem
@@ -3805,7 +3805,7 @@
<span class="md-ellipsis">
14.5 Unbounded knapsack problem
14.5 Unbounded Knapsack Problem
@@ -3833,7 +3833,7 @@
<span class="md-ellipsis">
14.6 Edit distance problem
14.6 Edit Distance Problem
@@ -3970,7 +3970,7 @@
<span class="md-ellipsis">
15.1 Greedy algorithms
15.1 Greedy Algorithm
@@ -3998,7 +3998,7 @@
<span class="md-ellipsis">
15.2 Fractional knapsack problem
15.2 Fractional Knapsack Problem
@@ -4026,7 +4026,7 @@
<span class="md-ellipsis">
15.3 Maximum capacity problem
15.3 Maximum Capacity Problem
@@ -4054,7 +4054,7 @@
<span class="md-ellipsis">
15.4 Maximum product cutting problem
15.4 Maximum Product Cutting Problem
@@ -4187,7 +4187,7 @@
<span class="md-ellipsis">
16.1 Installation
16.1 Programming Environment Installation
@@ -4215,7 +4215,7 @@
<span class="md-ellipsis">
16.2 Contributing
16.2 Contributing Together
@@ -4243,7 +4243,7 @@
<span class="md-ellipsis">
16.3 Terminology
16.3 Terminology Table
@@ -4360,7 +4360,7 @@
<a href="#341-ascii-character-set" class="md-nav__link">
<span class="md-ellipsis">
3.4.1 &nbsp; ASCII character set
3.4.1 &nbsp; Ascii Character Set
</span>
</a>
@@ -4371,7 +4371,7 @@
<a href="#342-gbk-character-set" class="md-nav__link">
<span class="md-ellipsis">
3.4.2 &nbsp; GBK character set
3.4.2 &nbsp; Gbk Character Set
</span>
</a>
@@ -4382,7 +4382,7 @@
<a href="#343-unicode-character-set" class="md-nav__link">
<span class="md-ellipsis">
3.4.3 &nbsp; Unicode character set
3.4.3 &nbsp; Unicode Character Set
</span>
</a>
@@ -4393,7 +4393,7 @@
<a href="#344-utf-8-encoding" class="md-nav__link">
<span class="md-ellipsis">
3.4.4 &nbsp; UTF-8 encoding
3.4.4 &nbsp; Utf-8 Encoding
</span>
</a>
@@ -4404,7 +4404,7 @@
<a href="#345-character-encoding-in-programming-languages" class="md-nav__link">
<span class="md-ellipsis">
3.4.5 &nbsp; Character encoding in programming languages
3.4.5 &nbsp; Character Encoding in Programming Languages
</span>
</a>
@@ -4448,70 +4448,70 @@
<!-- Page content -->
<h1 id="34-character-encoding">3.4 &nbsp; Character encoding *<a class="headerlink" href="#34-character-encoding" title="Permanent link">&para;</a></h1>
<p>In the computer system, all data is stored in binary form, and <code>char</code> is no exception. To represent characters, we need to develop a "character set" that defines a one-to-one mapping between each character and binary numbers. With the character set, computers can convert binary numbers to characters by looking up the table.</p>
<h2 id="341-ascii-character-set">3.4.1 &nbsp; ASCII character set<a class="headerlink" href="#341-ascii-character-set" title="Permanent link">&para;</a></h2>
<p>The <u>ASCII code</u> is one of the earliest character sets, officially known as the American Standard Code for Information Interchange. It uses 7 binary digits (the lower 7 bits of a byte) to represent a character, allowing for a maximum of 128 different characters. As shown in Figure 3-6, ASCII includes uppercase and lowercase English letters, numbers 0 ~ 9, various punctuation marks, and certain control characters (such as newline and tab).</p>
<h1 id="34-character-encoding">3.4 &nbsp; Character Encoding *<a class="headerlink" href="#34-character-encoding" title="Permanent link">&para;</a></h1>
<p>In computers, all data is stored in binary form, and character <code>char</code> is no exception. To represent characters, we need to establish a "character set" that defines a one-to-one correspondence between each character and binary numbers. With a character set, computers can convert binary numbers to characters by looking up the table.</p>
<h2 id="341-ascii-character-set">3.4.1 &nbsp; Ascii Character Set<a class="headerlink" href="#341-ascii-character-set" title="Permanent link">&para;</a></h2>
<p><u>ASCII code</u> is the earliest character set, with the full name American Standard Code for Information Interchange. It uses 7 binary bits (the lower 7 bits of one byte) to represent a character, and can represent a maximum of 128 different characters. As shown in Figure 3-6, ASCII code includes uppercase and lowercase English letters, numbers 0 ~ 9, some punctuation marks, and some control characters (such as newline and tab).</p>
<p><a class="glightbox" href="../character_encoding.assets/ascii_table.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="ASCII code" class="animation-figure" src="../character_encoding.assets/ascii_table.png" /></a></p>
<p align="center"> Figure 3-6 &nbsp; ASCII code </p>
<p>However, <strong>ASCII can only represent English characters</strong>. With the globalization of computers, a character set called <u>EASCII</u> was developed to represent more languages. It expands from the 7-bit structure of ASCII to 8 bits, enabling the representation of 256 characters.</p>
<p>Globally, various region-specific EASCII character sets have been introduced. The first 128 characters of these sets are consistent with the ASCII, while the remaining 128 characters are defined differently to accommodate the requirements of different languages.</p>
<h2 id="342-gbk-character-set">3.4.2 &nbsp; GBK character set<a class="headerlink" href="#342-gbk-character-set" title="Permanent link">&para;</a></h2>
<p>Later, it was found that <strong>EASCII still could not meet the character requirements of many languages</strong>. For instance, there are nearly a hundred thousand Chinese characters, with several thousand used regularly. In 1980, the Standardization Administration of China released the <u>GB2312</u> character set, which included 6763 Chinese characters, essentially fulfilling the computer processing needs for the Chinese language.</p>
<p>However, GB2312 could not handle some rare and traditional characters. The <u>GBK</u> character set expands GB2312 and includes 21886 Chinese characters. In the GBK encoding scheme, ASCII characters are represented with one byte, while Chinese characters use two bytes.</p>
<h2 id="343-unicode-character-set">3.4.3 &nbsp; Unicode character set<a class="headerlink" href="#343-unicode-character-set" title="Permanent link">&para;</a></h2>
<p>With the rapid evolution of computer technology and a plethora of character sets and encoding standards, numerous problems arose. On the one hand, these character sets generally only defined characters for specific languages and could not function properly in multilingual environments. On the other hand, the existence of multiple character set standards for the same language caused garbled text when information was exchanged between computers using different encoding standards.</p>
<p>Researchers of that era thought: <strong>What if a comprehensive character set encompassing all global languages and symbols was developed? Wouldn't this resolve the issues associated with cross-linguistic environments and garbled text?</strong> Inspired by this idea, the extensive character set, Unicode, was born.</p>
<p><u>Unicode</u> is referred to as "统一码" (Unified Code) in Chinese, theoretically capable of accommodating over a million characters. It aims to incorporate characters from all over the world into a single set, providing a universal character set for processing and displaying various languages and reducing the issues of garbled text due to different encoding standards.</p>
<p>Since its release in 1991, Unicode has continually expanded to include new languages and characters. As of September 2022, Unicode contains 149,186 characters, including characters, symbols, and even emojis from various languages. In the vast Unicode character set, commonly used characters occupy 2 bytes, while some rare characters may occupy 3 or even 4 bytes.</p>
<p>Unicode is a universal character set that assigns a number (called a "code point") to each character, <strong>but it does not specify how these character code points should be stored in a computer system</strong>. One might ask: How does a system interpret Unicode code points of varying lengths within a text? For example, given a 2-byte code, how does the system determine if it represents a single 2-byte character or two 1-byte characters?</p>
<p><strong>A straightforward solution to this problem is to store all characters as equal-length encodings</strong>. As shown in Figure 3-7, each character in "Hello" occupies 1 byte, while each character in "算法" (algorithm) occupies 2 bytes. We could encode all characters in "Hello 算法" as 2 bytes by padding the higher bits with zeros. This method would enable the system to interpret a character every 2 bytes, recovering the content of the phrase.</p>
<p>However, <strong>ASCII code can only represent English</strong>. With the globalization of computers, a character set called <u>EASCII</u> that can represent more languages emerged. It expands from the 7-bit basis of ASCII to 8 bits, and can represent 256 different characters.</p>
<p>Worldwide, a batch of EASCII character sets suitable for different regions have appeared successively. The first 128 characters of these character sets are unified as ASCII code, and the last 128 characters are defined differently to adapt to the needs of different languages.</p>
<h2 id="342-gbk-character-set">3.4.2 &nbsp; Gbk Character Set<a class="headerlink" href="#342-gbk-character-set" title="Permanent link">&para;</a></h2>
<p>Later, people found that <strong>EASCII code still cannot meet the character quantity requirements of many languages</strong>. For example, there are nearly one hundred thousand Chinese characters, and several thousand are used daily. In 1980, the China National Standardization Administration released the <u>GB2312</u> character set, which included 6,763 Chinese characters, basically meeting the needs for computer processing of Chinese characters.</p>
<p>However, GB2312 cannot handle some rare characters and traditional Chinese characters. The <u>GBK</u> character set is an extension based on GB2312, which includes a total of 21,886 Chinese characters. In the GBK encoding scheme, ASCII characters are represented using one byte, and Chinese characters are represented using two bytes.</p>
<h2 id="343-unicode-character-set">3.4.3 &nbsp; Unicode Character Set<a class="headerlink" href="#343-unicode-character-set" title="Permanent link">&para;</a></h2>
<p>With the vigorous development of computer technology, character sets and encoding standards flourished, which brought many problems. On the one hand, these character sets generally only define characters for specific languages and cannot work normally in multilingual environments. On the other hand, multiple character set standards exist for the same language, and if two computers use different encoding standards, garbled characters will appear during information transmission.</p>
<p>Researchers of that era thought: <strong>If a sufficiently complete character set is released that includes all languages and symbols in the world, wouldn't it be possible to solve cross-language environment and garbled character problems</strong>? Driven by this idea, a large and comprehensive character set, Unicode, was born.</p>
<p><u>Unicode</u> is called "统一码" (Unified Code) in Chinese and can theoretically accommodate over one million characters. It is committed to including characters from around the world into a unified character set, providing a universal character set to handle and display various language texts, reducing garbled character problems caused by different encoding standards.</p>
<p>Since its release in 1991, Unicode has continuously expanded to include new languages and characters. As of September 2022, Unicode has included 149,186 characters, including characters, symbols, and even emojis from various languages. In the vast Unicode character set, commonly used characters occupy 2 bytes, and some rare characters occupy 3 bytes or even 4 bytes.</p>
<p>Unicode is a universal character set that essentially assigns a number (called a "code point") to each character, <strong>but it does not specify how to store these character code points in computers</strong>. We can't help but ask: when Unicode code points of multiple lengths appear simultaneously in a text, how does the system parse the characters? For example, given an encoding with a length of 2 bytes, how does the system determine whether it is one 2-byte character or two 1-byte characters?</p>
<p>For the above problem, <strong>a straightforward solution is to store all characters as equal-length encodings</strong>. As shown in Figure 3-7, each character in "Hello" occupies 1 byte, and each character in "算法" (algorithm) occupies 2 bytes. We can encode all characters in "Hello 算法" as 2 bytes in length by padding the high bits with 0. In this way, the system can parse one character every 2 bytes and restore the content of this phrase.</p>
<p><a class="glightbox" href="../character_encoding.assets/unicode_hello_algo.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Unicode encoding example" class="animation-figure" src="../character_encoding.assets/unicode_hello_algo.png" /></a></p>
<p align="center"> Figure 3-7 &nbsp; Unicode encoding example </p>
<p>However, as ASCII has shown us, encoding English only requires 1 byte. Using the above approach would double the space occupied by English text compared to ASCII encoding, which is a waste of memory space. Therefore, a more efficient Unicode encoding method is needed.</p>
<h2 id="344-utf-8-encoding">3.4.4 &nbsp; UTF-8 encoding<a class="headerlink" href="#344-utf-8-encoding" title="Permanent link">&para;</a></h2>
<p>Currently, UTF-8 has become the most widely used Unicode encoding method internationally. <strong>It is a variable-length encoding</strong>, using 1 to 4 bytes to represent a character, depending on the complexity of the character. ASCII characters need only 1 byte, Latin and Greek letters require 2 bytes, commonly used Chinese characters need 3 bytes, and some other rare characters need 4 bytes.</p>
<p>The encoding rules for UTF-8 are not complex and can be divided into two cases:</p>
<p>However, ASCII code has already proven to us that encoding English only requires 1 byte. If the above scheme is adopted, the size of English text will be twice that under ASCII encoding, which is very wasteful of memory space. Therefore, we need a more efficient Unicode encoding method.</p>
<h2 id="344-utf-8-encoding">3.4.4 &nbsp; Utf-8 Encoding<a class="headerlink" href="#344-utf-8-encoding" title="Permanent link">&para;</a></h2>
<p>Currently, UTF-8 has become the most widely used Unicode encoding method internationally. <strong>It is a variable-length encoding</strong> that uses 1 to 4 bytes to represent a character, depending on the complexity of the character. ASCII characters only require 1 byte, Latin and Greek letters require 2 bytes, commonly used Chinese characters require 3 bytes, and some other rare characters require 4 bytes.</p>
<p>The encoding rules of UTF-8 are not complicated and can be divided into the following two cases.</p>
<ul>
<li>For 1-byte characters, set the highest bit to <span class="arithmatex">\(0\)</span>, and the remaining 7 bits to the Unicode code point. Notably, ASCII characters occupy the first 128 code points in the Unicode set. This means that <strong>UTF-8 encoding is backward compatible with ASCII</strong>. This implies that UTF-8 can be used to parse ancient ASCII text.</li>
<li>For characters of length <span class="arithmatex">\(n\)</span> bytes (where <span class="arithmatex">\(n &gt; 1\)</span>), set the highest <span class="arithmatex">\(n\)</span> bits of the first byte to <span class="arithmatex">\(1\)</span>, and the <span class="arithmatex">\((n + 1)^{\text{th}}\)</span> bit to <span class="arithmatex">\(0\)</span>; starting from the second byte, set the highest 2 bits of each byte to <span class="arithmatex">\(10\)</span>; the rest of the bits are used to fill the Unicode code point.</li>
<li>For 1-byte characters, set the highest bit to <span class="arithmatex">\(0\)</span>, and set the remaining 7 bits to the Unicode code point. It is worth noting that ASCII characters occupy the first 128 code points in the Unicode character set. That is to say, <strong>UTF-8 encoding is backward compatible with ASCII code</strong>. This means we can use UTF-8 to parse very old ASCII code text.</li>
<li>For characters with a length of <span class="arithmatex">\(n\)</span> bytes (where <span class="arithmatex">\(n &gt; 1\)</span>), set the highest <span class="arithmatex">\(n\)</span> bits of the first byte to <span class="arithmatex">\(1\)</span>, and set the <span class="arithmatex">\((n + 1)\)</span>-th bit to <span class="arithmatex">\(0\)</span>; starting from the second byte, set the highest 2 bits of each byte to <span class="arithmatex">\(10\)</span>; use all remaining bits to fill in the Unicode code point of the character.</li>
</ul>
<p>Figure 3-8 shows the UTF-8 encoding for "Hello算法". It can be observed that since the highest <span class="arithmatex">\(n\)</span> bits are set to <span class="arithmatex">\(1\)</span>, the system can determine the length of the character as <span class="arithmatex">\(n\)</span> by counting the number of highest bits set to <span class="arithmatex">\(1\)</span>.</p>
<p>But why set the highest 2 bits of the remaining bytes to <span class="arithmatex">\(10\)</span>? Actually, this <span class="arithmatex">\(10\)</span> serves as a kind of checksum. If the system starts parsing text from an incorrect byte, the <span class="arithmatex">\(10\)</span> at the beginning of the byte can help the system quickly detect anomalies.</p>
<p>The reason for using <span class="arithmatex">\(10\)</span> as a checksum is that, under UTF-8 encoding rules, it's impossible for the highest two bits of a character to be <span class="arithmatex">\(10\)</span>. This can be proven by contradiction: If the highest two bits of a character are <span class="arithmatex">\(10\)</span>, it indicates that the character's length is <span class="arithmatex">\(1\)</span>, corresponding to ASCII. However, the highest bit of an ASCII character should be <span class="arithmatex">\(0\)</span>, which contradicts the assumption.</p>
<p>Figure 3-8 shows the UTF-8 encoding corresponding to "Hello算法". It can be observed that since the highest <span class="arithmatex">\(n\)</span> bits are all set to <span class="arithmatex">\(1\)</span>, the system can parse the length of the character as <span class="arithmatex">\(n\)</span> by reading the number of highest bits that are <span class="arithmatex">\(1\)</span>.</p>
<p>But why set the highest 2 bits of all other bytes to <span class="arithmatex">\(10\)</span>? In fact, this <span class="arithmatex">\(10\)</span> can serve as a check symbol. Assuming the system starts parsing text from an incorrect byte, the <span class="arithmatex">\(10\)</span> at the beginning of the byte can help the system quickly determine an anomaly.</p>
<p>The reason for using <span class="arithmatex">\(10\)</span> as a check symbol is that under UTF-8 encoding rules, it is impossible for a character's highest two bits to be <span class="arithmatex">\(10\)</span>. This conclusion can be proven by contradiction: assuming the highest two bits of a character are <span class="arithmatex">\(10\)</span>, it means the length of the character is <span class="arithmatex">\(1\)</span>, corresponding to ASCII code. However, the highest bit of ASCII code should be <span class="arithmatex">\(0\)</span>, which contradicts the assumption.</p>
<p><a class="glightbox" href="../character_encoding.assets/utf-8_hello_algo.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="UTF-8 encoding example" class="animation-figure" src="../character_encoding.assets/utf-8_hello_algo.png" /></a></p>
<p align="center"> Figure 3-8 &nbsp; UTF-8 encoding example </p>
<p>Apart from UTF-8, other common encoding methods include:</p>
<p>In addition to UTF-8, common encoding methods also include the following two.</p>
<ul>
<li><strong>UTF-16 encoding</strong>: Uses 2 or 4 bytes to represent a character. All ASCII characters and commonly used non-English characters are represented with 2 bytes; a few characters require 4 bytes. For 2-byte characters, the UTF-16 encoding equals the Unicode code point.</li>
<li><strong>UTF-32 encoding</strong>: Every character uses 4 bytes. This means UTF-32 occupies more space than UTF-8 and UTF-16, especially for texts with a high proportion of ASCII characters.</li>
<li><strong>UTF-16 encoding</strong>: Uses 2 or 4 bytes to represent a character. All ASCII characters and commonly used non-English characters are represented with 2 bytes; a few characters need to use 4 bytes. For 2-byte characters, UTF-16 encoding is equal to the Unicode code point.</li>
<li><strong>UTF-32 encoding</strong>: Every character uses 4 bytes. This means that UTF-32 takes up more space than UTF-8 and UTF-16, especially for text with a high proportion of ASCII characters.</li>
</ul>
<p>From the perspective of storage space, using UTF-8 to represent English characters is very efficient because it only requires 1 byte; using UTF-16 to encode some non-English characters (such as Chinese) can be more efficient because it only requires 2 bytes, while UTF-8 might need 3 bytes.</p>
<p>From a compatibility perspective, UTF-8 is the most versatile, with many tools and libraries supporting UTF-8 as a priority.</p>
<h2 id="345-character-encoding-in-programming-languages">3.4.5 &nbsp; Character encoding in programming languages<a class="headerlink" href="#345-character-encoding-in-programming-languages" title="Permanent link">&para;</a></h2>
<p>Historically, many programming languages utilized fixed-length encodings such as UTF-16 or UTF-32 for processing strings during program execution. This allows strings to be handled as arrays, offering several advantages:</p>
<p>From the perspective of storage space occupation, using UTF-8 to represent English characters is very efficient because it only requires 1 byte; using UTF-16 encoding for some non-English characters (such as Chinese) will be more efficient because it only requires 2 bytes, while UTF-8 may require 3 bytes.</p>
<p>From a compatibility perspective, UTF-8 has the best universality, and many tools and libraries support UTF-8 first.</p>
<h2 id="345-character-encoding-in-programming-languages">3.4.5 &nbsp; Character Encoding in Programming Languages<a class="headerlink" href="#345-character-encoding-in-programming-languages" title="Permanent link">&para;</a></h2>
<p>For most past programming languages, strings during program execution use fixed-length encodings such as UTF-16 or UTF-32. Under fixed-length encoding, we can treat strings as arrays for processing, and this approach has the following advantages.</p>
<ul>
<li><strong>Random access</strong>: Strings encoded in UTF-16 can be accessed randomly with ease. For UTF-8, which is a variable-length encoding, locating the <span class="arithmatex">\(i^{th}\)</span> character requires traversing the string from the start to the <span class="arithmatex">\(i^{th}\)</span> position, taking <span class="arithmatex">\(O(n)\)</span> time.</li>
<li><strong>Character counting</strong>: Similar to random access, counting the number of characters in a UTF-16 encoded string is an <span class="arithmatex">\(O(1)\)</span> operation. However, counting characters in a UTF-8 encoded string requires traversing the entire string.</li>
<li><strong>String operations</strong>: Many string operations like splitting, concatenating, inserting, and deleting are easier on UTF-16 encoded strings. These operations generally require additional computation on UTF-8 encoded strings to ensure the validity of the UTF-8 encoding.</li>
<li><strong>Random access</strong>: UTF-16 encoded strings can be easily accessed randomly. UTF-8 is a variable-length encoding. To find the <span class="arithmatex">\(i\)</span>-th character, we need to traverse from the beginning of the string to the <span class="arithmatex">\(i\)</span>-th character, which requires <span class="arithmatex">\(O(n)\)</span> time.</li>
<li><strong>Character counting</strong>: Similar to random access, calculating the length of a UTF-16 encoded string is also an <span class="arithmatex">\(O(1)\)</span> operation. However, calculating the length of a UTF-8 encoded string requires traversing the entire string.</li>
<li><strong>String operations</strong>: Many string operations (such as splitting, joining, inserting, deleting, etc.) on UTF-16 encoded strings are easier to perform. Performing these operations on UTF-8 encoded strings usually requires additional calculations to ensure that invalid UTF-8 encoding is not generated.</li>
</ul>
<p>The design of character encoding schemes in programming languages is an interesting topic involving various factors:</p>
<p>In fact, the design of character encoding schemes for programming languages is a very interesting topic involving many factors.</p>
<ul>
<li>Javas <code>String</code> type uses UTF-16 encoding, with each character occupying 2 bytes. This was based on the initial belief that 16 bits were sufficient to represent all possible characters and proven incorrect later. As the Unicode standard expanded beyond 16 bits, characters in Java may now be represented by a pair of 16-bit values, known as “surrogate pairs.</li>
<li>JavaScript and TypeScript use UTF-16 encoding for similar reasons as Java. When JavaScript was first introduced by Netscape in 1995, Unicode was still in its early stages, and 16-bit encoding was sufficient to represent all Unicode characters.</li>
<li>C# uses UTF-16 encoding, largely because the .NET platform, designed by Microsoft, and many Microsoft technologies, including the Windows operating system, extensively use UTF-16 encoding.</li>
<li>Java's <code>String</code> type uses UTF-16 encoding, with each character occupying 2 bytes. This is because at the beginning of Java language design, people believed that 16 bits were sufficient to represent all possible characters. However, this was an incorrect judgment. Later, the Unicode specification expanded beyond 16 bits, so characters in Java may now be represented by a pair of 16-bit values (called "surrogate pairs").</li>
<li>The strings of JavaScript and TypeScript use UTF-16 encoding for reasons similar to Java. When Netscape first introduced the JavaScript language in 1995, Unicode was still in its early stages of development, and at that time, using 16-bit encoding was sufficient to represent all Unicode characters.</li>
<li>C# uses UTF-16 encoding mainly because the .NET platform was designed by Microsoft, and many of Microsoft's technologies (including the Windows operating system) extensively use UTF-16 encoding.</li>
</ul>
<p>Due to the underestimation of character counts, these languages had to use "surrogate pairs" to represent Unicode characters exceeding 16 bits. This approach has its drawbacks: strings containing surrogate pairs may have characters occupying 2 or 4 bytes, losing the advantage of fixed-length encoding. Additionally, handling surrogate pairs adds complexity and debugging difficulty to programming.</p>
<p>Addressing these challenges, some languages have adopted alternative encoding strategies:</p>
<p>Due to the underestimation of character quantities by the above programming languages, they had to adopt the "surrogate pair" method to represent Unicode characters with lengths exceeding 16 bits. This is a reluctant compromise. On the one hand, in strings containing surrogate pairs, one character may occupy 2 bytes or 4 bytes, thus losing the advantage of fixed-length encoding. On the other hand, handling surrogate pairs requires additional code, which increases the complexity and difficulty of debugging in programming.</p>
<p>For the above reasons, some programming languages have proposed different encoding schemes.</p>
<ul>
<li>Pythons <code>str</code> type uses Unicode encoding with a flexible representation where the storage length of characters depends on the largest Unicode code point in the string. If all characters are ASCII, each character occupies 1 byte, 2 bytes for characters within the Basic Multilingual Plane (BMP), and 4 bytes for characters beyond the BMP.</li>
<li>Gos <code>string</code> type internally uses UTF-8 encoding. Go also provides the <code>rune</code> type for representing individual Unicode code points.</li>
<li>Rusts <code>str</code> and <code>String</code> types use UTF-8 encoding internally. Rust also offers the <code>char</code> type for individual Unicode code points.</li>
<li>Python's <code>str</code> uses Unicode encoding and adopts a flexible string representation where the stored character length depends on the largest Unicode code point in the string. If all characters in the string are ASCII characters, each character occupies 1 byte; if there are characters exceeding the ASCII range but all within the Basic Multilingual Plane (BMP), each character occupies 2 bytes; if there are characters exceeding the BMP, each character occupies 4 bytes.</li>
<li>Go language's <code>string</code> type uses UTF-8 encoding internally. Go language also provides the <code>rune</code> type, which is used to represent a single Unicode code point.</li>
<li>Rust language's <code>str</code> and <code>String</code> types use UTF-8 encoding internally. Rust also provides the <code>char</code> type for representing a single Unicode code point.</li>
</ul>
<p>Its important to note that the above discussion pertains to how strings are stored in programming languages, <strong>which is different from how strings are stored in files or transmitted over networks</strong>. For file storage or network transmission, strings are usually encoded in UTF-8 format for optimal compatibility and space efficiency.</p>
<p>It should be noted that the above discussion is about how strings are stored in programming languages, <strong>which is different from how strings are stored in files or transmitted over networks</strong>. In file storage or network transmission, we usually encode strings into UTF-8 format to achieve optimal compatibility and space efficiency.</p>
<!-- Source file information -->
@@ -4534,7 +4534,7 @@ aria-label="Footer"
<a
href="../number_encoding/"
class="md-footer__link md-footer__link--prev"
aria-label="Previous: 3.3 Number encoding *"
aria-label="Previous: 3.3 Number Encoding *"
rel="prev"
>
<div class="md-footer__button md-icon">
@@ -4546,7 +4546,7 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
3.3 Number encoding *
3.3 Number Encoding *
</div>
</div>
</a>
@@ -4659,7 +4659,7 @@ aria-label="Footer"
<nav class="md-footer__inner md-grid" aria-label="Footer" >
<a href="../number_encoding/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 3.3 Number encoding *">
<a href="../number_encoding/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 3.3 Number Encoding *">
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11z"/></svg>
@@ -4669,7 +4669,7 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
3.3 Number encoding *
3.3 Number Encoding *
</div>
</div>
</a>

View File

@@ -37,7 +37,7 @@
<title>3.1 Classification of data structures - Hello Algo</title>
<title>3.1 Classification of Data Structures - Hello Algo</title>
@@ -58,8 +58,8 @@
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
<style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:300,300i,400,400i,700,700i%7CJetBrains+Mono:400,400i,700,700i&display=fallback">
<style>:root{--md-text-font:"Lato";--md-code-font:"JetBrains Mono"}</style>
@@ -154,7 +154,7 @@
<div class="md-header__topic" data-md-component="header-topic">
<span class="md-ellipsis">
3.1 Classification of data structures
3.1 Classification of Data Structures
</span>
</div>
@@ -371,7 +371,7 @@
<span class="md-ellipsis">
Before starting
Before Starting
@@ -388,7 +388,7 @@
<span class="md-nav__icon md-icon"></span>
Before starting
Before Starting
</label>
@@ -487,7 +487,7 @@
<span class="md-ellipsis">
0.1 About this book
0.1 About This Book
@@ -515,7 +515,7 @@
<span class="md-ellipsis">
0.2 How to read
0.2 How to Use This Book
@@ -604,7 +604,7 @@
<span class="md-ellipsis">
Chapter 1. Encounter with algorithms
Chapter 1. Encounter With Algorithms
@@ -626,7 +626,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 1. Encounter with algorithms
Chapter 1. Encounter With Algorithms
</label>
@@ -648,7 +648,7 @@
<span class="md-ellipsis">
1.1 Algorithms are everywhere
1.1 Algorithms Are Everywhere
@@ -676,7 +676,7 @@
<span class="md-ellipsis">
1.2 What is an algorithm
1.2 What Is an Algorithm
@@ -769,7 +769,7 @@
<span class="md-ellipsis">
Chapter 2. Complexity analysis
Chapter 2. Complexity Analysis
@@ -791,7 +791,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 2. Complexity analysis
Chapter 2. Complexity Analysis
</label>
@@ -813,7 +813,7 @@
<span class="md-ellipsis">
2.1 Algorithm efficiency assessment
2.1 Algorithm Efficiency Evaluation
@@ -841,7 +841,7 @@
<span class="md-ellipsis">
2.2 Iteration and recursion
2.2 Iteration and Recursion
@@ -869,7 +869,7 @@
<span class="md-ellipsis">
2.3 Time complexity
2.3 Time Complexity
@@ -897,7 +897,7 @@
<span class="md-ellipsis">
2.4 Space complexity
2.4 Space Complexity
@@ -992,7 +992,7 @@
<span class="md-ellipsis">
Chapter 3. Data structures
Chapter 3. Data Structures
@@ -1014,7 +1014,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 3. Data structures
Chapter 3. Data Structures
</label>
@@ -1045,7 +1045,7 @@
<span class="md-ellipsis">
3.1 Classification of data structures
3.1 Classification of Data Structures
@@ -1063,7 +1063,7 @@
<span class="md-ellipsis">
3.1 Classification of data structures
3.1 Classification of Data Structures
@@ -1092,7 +1092,7 @@
<a href="#311-logical-structure-linear-and-non-linear" class="md-nav__link">
<span class="md-ellipsis">
3.1.1 &nbsp; Logical structure: linear and non-linear
3.1.1 &nbsp; Logical Structure: Linear and Non-Linear
</span>
</a>
@@ -1103,7 +1103,7 @@
<a href="#312-physical-structure-contiguous-and-dispersed" class="md-nav__link">
<span class="md-ellipsis">
3.1.2 &nbsp; Physical structure: contiguous and dispersed
3.1.2 &nbsp; Physical Structure: Contiguous and Dispersed
</span>
</a>
@@ -1133,7 +1133,7 @@
<span class="md-ellipsis">
3.2 Basic data types
3.2 Basic Data Types
@@ -1161,7 +1161,7 @@
<span class="md-ellipsis">
3.3 Number encoding *
3.3 Number Encoding *
@@ -1189,7 +1189,7 @@
<span class="md-ellipsis">
3.4 Character encoding *
3.4 Character Encoding *
@@ -1282,7 +1282,7 @@
<span class="md-ellipsis">
Chapter 4. Array and linked list
Chapter 4. Array and Linked List
@@ -1304,7 +1304,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 4. Array and linked list
Chapter 4. Array and Linked List
</label>
@@ -1354,7 +1354,7 @@
<span class="md-ellipsis">
4.2 Linked list
4.2 Linked List
@@ -1410,7 +1410,7 @@
<span class="md-ellipsis">
4.4 Memory and cache *
4.4 Memory and Cache *
@@ -1501,7 +1501,7 @@
<span class="md-ellipsis">
Chapter 5. Stack and queue
Chapter 5. Stack and Queue
@@ -1523,7 +1523,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 5. Stack and queue
Chapter 5. Stack and Queue
</label>
@@ -1601,7 +1601,7 @@
<span class="md-ellipsis">
5.3 Double-ended queue
5.3 Double-Ended Queue
@@ -1692,7 +1692,7 @@
<span class="md-ellipsis">
Chapter 6. Hash table
Chapter 6. Hashing
@@ -1714,7 +1714,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 6. Hash table
Chapter 6. Hashing
</label>
@@ -1736,7 +1736,7 @@
<span class="md-ellipsis">
6.1 Hash table
6.1 Hash Table
@@ -1764,7 +1764,7 @@
<span class="md-ellipsis">
6.2 Hash collision
6.2 Hash Collision
@@ -1792,7 +1792,7 @@
<span class="md-ellipsis">
6.3 Hash algorithm
6.3 Hash Algorithm
@@ -1931,7 +1931,7 @@
<span class="md-ellipsis">
7.1 Binary tree
7.1 Binary Tree
@@ -1959,7 +1959,7 @@
<span class="md-ellipsis">
7.2 Binary tree traversal
7.2 Binary Tree Traversal
@@ -1987,7 +1987,7 @@
<span class="md-ellipsis">
7.3 Array Representation of tree
7.3 Array Representation of Tree
@@ -2015,7 +2015,7 @@
<span class="md-ellipsis">
7.4 Binary Search tree
7.4 Binary Search Tree
@@ -2043,7 +2043,7 @@
<span class="md-ellipsis">
7.5 AVL tree *
7.5 AVL Tree *
@@ -2206,7 +2206,7 @@
<span class="md-ellipsis">
8.2 Building a heap
8.2 Building a Heap
@@ -2234,7 +2234,7 @@
<span class="md-ellipsis">
8.3 Top-k problem
8.3 Top-K Problem
@@ -2397,7 +2397,7 @@
<span class="md-ellipsis">
9.2 Basic graph operations
9.2 Basic Operations on Graphs
@@ -2425,7 +2425,7 @@
<span class="md-ellipsis">
9.3 Graph traversal
9.3 Graph Traversal
@@ -2564,7 +2564,7 @@
<span class="md-ellipsis">
10.1 Binary search
10.1 Binary Search
@@ -2592,7 +2592,7 @@
<span class="md-ellipsis">
10.2 Binary search insertion
10.2 Binary Search Insertion
@@ -2620,7 +2620,7 @@
<span class="md-ellipsis">
10.3 Binary search boundaries
10.3 Binary Search Edge Cases
@@ -2648,7 +2648,7 @@
<span class="md-ellipsis">
10.4 Hashing optimization strategies
10.4 Hash Optimization Strategy
@@ -2676,7 +2676,7 @@
<span class="md-ellipsis">
10.5 Search algorithms revisited
10.5 Search Algorithms Revisited
@@ -2825,7 +2825,7 @@
<span class="md-ellipsis">
11.1 Sorting algorithms
11.1 Sorting Algorithms
@@ -2853,7 +2853,7 @@
<span class="md-ellipsis">
11.2 Selection sort
11.2 Selection Sort
@@ -2881,7 +2881,7 @@
<span class="md-ellipsis">
11.3 Bubble sort
11.3 Bubble Sort
@@ -2909,7 +2909,7 @@
<span class="md-ellipsis">
11.4 Insertion sort
11.4 Insertion Sort
@@ -2937,7 +2937,7 @@
<span class="md-ellipsis">
11.5 Quick sort
11.5 Quick Sort
@@ -2965,7 +2965,7 @@
<span class="md-ellipsis">
11.6 Merge sort
11.6 Merge Sort
@@ -2993,7 +2993,7 @@
<span class="md-ellipsis">
11.7 Heap sort
11.7 Heap Sort
@@ -3021,7 +3021,7 @@
<span class="md-ellipsis">
11.8 Bucket sort
11.8 Bucket Sort
@@ -3049,7 +3049,7 @@
<span class="md-ellipsis">
11.9 Counting sort
11.9 Counting Sort
@@ -3077,7 +3077,7 @@
<span class="md-ellipsis">
11.10 Radix sort
11.10 Radix Sort
@@ -3170,7 +3170,7 @@
<span class="md-ellipsis">
Chapter 12. Divide and conquer
Chapter 12. Divide and Conquer
@@ -3192,7 +3192,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 12. Divide and conquer
Chapter 12. Divide and Conquer
</label>
@@ -3214,7 +3214,7 @@
<span class="md-ellipsis">
12.1 Divide and conquer algorithms
12.1 Divide and Conquer Algorithms
@@ -3242,7 +3242,7 @@
<span class="md-ellipsis">
12.2 Divide and conquer search strategy
12.2 Divide and Conquer Search Strategy
@@ -3270,7 +3270,7 @@
<span class="md-ellipsis">
12.3 Building binary tree problem
12.3 Building a Binary Tree Problem
@@ -3298,7 +3298,7 @@
<span class="md-ellipsis">
12.4 Tower of Hanoi Problem
12.4 Hanoi Tower Problem
@@ -3435,7 +3435,7 @@
<span class="md-ellipsis">
13.1 Backtracking algorithms
13.1 Backtracking Algorithm
@@ -3463,7 +3463,7 @@
<span class="md-ellipsis">
13.2 Permutation problem
13.2 Permutations Problem
@@ -3491,7 +3491,7 @@
<span class="md-ellipsis">
13.3 Subset sum problem
13.3 Subset-Sum Problem
@@ -3519,7 +3519,7 @@
<span class="md-ellipsis">
13.4 n queens problem
13.4 N-Queens Problem
@@ -3616,7 +3616,7 @@
<span class="md-ellipsis">
Chapter 14. Dynamic programming
Chapter 14. Dynamic Programming
@@ -3638,7 +3638,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 14. Dynamic programming
Chapter 14. Dynamic Programming
</label>
@@ -3660,7 +3660,7 @@
<span class="md-ellipsis">
14.1 Introduction to dynamic programming
14.1 Introduction to Dynamic Programming
@@ -3688,7 +3688,7 @@
<span class="md-ellipsis">
14.2 Characteristics of DP problems
14.2 Characteristics of Dynamic Programming Problems
@@ -3716,7 +3716,7 @@
<span class="md-ellipsis">
14.3 DP problem-solving approach
14.3 Dynamic Programming Problem-Solving Approach
@@ -3744,7 +3744,7 @@
<span class="md-ellipsis">
14.4 0-1 Knapsack problem
14.4 0-1 Knapsack Problem
@@ -3772,7 +3772,7 @@
<span class="md-ellipsis">
14.5 Unbounded knapsack problem
14.5 Unbounded Knapsack Problem
@@ -3800,7 +3800,7 @@
<span class="md-ellipsis">
14.6 Edit distance problem
14.6 Edit Distance Problem
@@ -3937,7 +3937,7 @@
<span class="md-ellipsis">
15.1 Greedy algorithms
15.1 Greedy Algorithm
@@ -3965,7 +3965,7 @@
<span class="md-ellipsis">
15.2 Fractional knapsack problem
15.2 Fractional Knapsack Problem
@@ -3993,7 +3993,7 @@
<span class="md-ellipsis">
15.3 Maximum capacity problem
15.3 Maximum Capacity Problem
@@ -4021,7 +4021,7 @@
<span class="md-ellipsis">
15.4 Maximum product cutting problem
15.4 Maximum Product Cutting Problem
@@ -4154,7 +4154,7 @@
<span class="md-ellipsis">
16.1 Installation
16.1 Programming Environment Installation
@@ -4182,7 +4182,7 @@
<span class="md-ellipsis">
16.2 Contributing
16.2 Contributing Together
@@ -4210,7 +4210,7 @@
<span class="md-ellipsis">
16.3 Terminology
16.3 Terminology Table
@@ -4327,7 +4327,7 @@
<a href="#311-logical-structure-linear-and-non-linear" class="md-nav__link">
<span class="md-ellipsis">
3.1.1 &nbsp; Logical structure: linear and non-linear
3.1.1 &nbsp; Logical Structure: Linear and Non-Linear
</span>
</a>
@@ -4338,7 +4338,7 @@
<a href="#312-physical-structure-contiguous-and-dispersed" class="md-nav__link">
<span class="md-ellipsis">
3.1.2 &nbsp; Physical structure: contiguous and dispersed
3.1.2 &nbsp; Physical Structure: Contiguous and Dispersed
</span>
</a>
@@ -4382,47 +4382,47 @@
<!-- Page content -->
<h1 id="31-classification-of-data-structures">3.1 &nbsp; Classification of data structures<a class="headerlink" href="#31-classification-of-data-structures" title="Permanent link">&para;</a></h1>
<p>Common data structures include arrays, linked lists, stacks, queues, hash tables, trees, heaps, and graphs. They can be classified into "logical structure" and "physical structure".</p>
<h2 id="311-logical-structure-linear-and-non-linear">3.1.1 &nbsp; Logical structure: linear and non-linear<a class="headerlink" href="#311-logical-structure-linear-and-non-linear" title="Permanent link">&para;</a></h2>
<p><strong>The logical structures reveal the logical relationships between data elements</strong>. In arrays and linked lists, data are arranged in a specific sequence, demonstrating the linear relationship between data; while in trees, data are arranged hierarchically from the top down, showing the derived relationship between "ancestors" and "descendants"; and graphs are composed of nodes and edges, reflecting the intricate network relationship.</p>
<p>As shown in Figure 3-1, logical structures can be divided into two major categories: "linear" and "non-linear". Linear structures are more intuitive, indicating data is arranged linearly in logical relationships; non-linear structures, conversely, are arranged non-linearly.</p>
<h1 id="31-classification-of-data-structures">3.1 &nbsp; Classification of Data Structures<a class="headerlink" href="#31-classification-of-data-structures" title="Permanent link">&para;</a></h1>
<p>Common data structures include arrays, linked lists, stacks, queues, hash tables, trees, heaps, and graphs. They can be classified from two dimensions: "logical structure" and "physical structure".</p>
<h2 id="311-logical-structure-linear-and-non-linear">3.1.1 &nbsp; Logical Structure: Linear and Non-Linear<a class="headerlink" href="#311-logical-structure-linear-and-non-linear" title="Permanent link">&para;</a></h2>
<p><strong>Logical structure reveals the logical relationships between data elements</strong>. In arrays and linked lists, data is arranged in a certain order, embodying the linear relationship between data; while in trees, data is arranged hierarchically from top to bottom, showing the derived relationship between "ancestors" and "descendants"; graphs are composed of nodes and edges, reflecting complex network relationships.</p>
<p>As shown in Figure 3-1, logical structures can be divided into two major categories: "linear" and "non-linear". Linear structures are more intuitive, indicating that data is linearly arranged in logical relationships; non-linear structures are the opposite, arranged non-linearly.</p>
<ul>
<li><strong>Linear data structures</strong>: Arrays, Linked Lists, Stacks, Queues, Hash Tables, where elements have a one-to-one sequential relationship.</li>
<li><strong>Non-linear data structures</strong>: Trees, Heaps, Graphs, Hash Tables.</li>
<li><strong>Linear data structures</strong>: Arrays, linked lists, stacks, queues, hash tables, where elements have a one-to-one sequential relationship.</li>
<li><strong>Non-linear data structures</strong>: Trees, heaps, graphs, hash tables.</li>
</ul>
<p>Non-linear data structures can be further divided into tree structures and network structures.</p>
<ul>
<li><strong>Tree structures</strong>: Trees, Heaps, Hash Tables, where elements have a one-to-many relationship.</li>
<li><strong>Network structures</strong>: Graphs, where elements have a many-to-many relationships.</li>
<li><strong>Tree structures</strong>: Trees, heaps, hash tables, where elements have a one-to-many relationship.</li>
<li><strong>Network structures</strong>: Graphs, where elements have a many-to-many relationship.</li>
</ul>
<p><a class="glightbox" href="../classification_of_data_structure.assets/classification_logic_structure.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Linear and non-linear data structures" class="animation-figure" src="../classification_of_data_structure.assets/classification_logic_structure.png" /></a></p>
<p align="center"> Figure 3-1 &nbsp; Linear and non-linear data structures </p>
<h2 id="312-physical-structure-contiguous-and-dispersed">3.1.2 &nbsp; Physical structure: contiguous and dispersed<a class="headerlink" href="#312-physical-structure-contiguous-and-dispersed" title="Permanent link">&para;</a></h2>
<p><strong>During the execution of an algorithm, the data being processed is stored in memory</strong>. Figure 3-2 shows a computer memory stick where each black square is a physical memory space. We can think of memory as a vast Excel spreadsheet, with each cell capable of storing a certain amount of data.</p>
<p><strong>The system accesses the data at the target location by means of a memory address</strong>. As shown in Figure 3-2, the computer assigns a unique identifier to each cell in the table according to specific rules, ensuring that each memory space has a unique memory address. With these addresses, the program can access the data stored in memory.</p>
<p><a class="glightbox" href="../classification_of_data_structure.assets/computer_memory_location.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Memory stick, memory spaces, memory addresses" class="animation-figure" src="../classification_of_data_structure.assets/computer_memory_location.png" /></a></p>
<p align="center"> Figure 3-2 &nbsp; Memory stick, memory spaces, memory addresses </p>
<h2 id="312-physical-structure-contiguous-and-dispersed">3.1.2 &nbsp; Physical Structure: Contiguous and Dispersed<a class="headerlink" href="#312-physical-structure-contiguous-and-dispersed" title="Permanent link">&para;</a></h2>
<p><strong>When an algorithm program runs, the data being processed is mainly stored in memory</strong>. Figure 3-2 shows a computer memory stick, where each black square contains a memory space. We can imagine memory as a huge Excel spreadsheet, where each cell can store a certain amount of data.</p>
<p><strong>The system accesses data at the target location through memory addresses</strong>. As shown in Figure 3-2, the computer assigns a number to each cell in the spreadsheet according to specific rules, ensuring that each memory space has a unique memory address. With these addresses, the program can access data in memory.</p>
<p><a class="glightbox" href="../classification_of_data_structure.assets/computer_memory_location.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Memory stick, memory space, memory address" class="animation-figure" src="../classification_of_data_structure.assets/computer_memory_location.png" /></a></p>
<p align="center"> Figure 3-2 &nbsp; Memory stick, memory space, memory address </p>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>It's worth noting that comparing memory to an Excel spreadsheet is a simplified analogy. The actual working mechanism of memory is more complex, involving concepts like address space, memory management, cache mechanisms, virtual memory, and physical memory.</p>
<p>It is worth noting that comparing memory to an Excel spreadsheet is a simplified analogy. The actual working mechanism of memory is quite complex, involving concepts such as address space, memory management, cache mechanisms, virtual memory, and physical memory.</p>
</div>
<p>Memory is a shared resource for all programs. When a block of memory is occupied by one program, it cannot be simultaneously used by other programs. <strong>Therefore, memory resources are an important consideration in the design of data structures and algorithms</strong>. For instance, the algorithm's peak memory usage should not exceed the remaining free memory of the system; if there is a lack of contiguous memory blocks, then the data structure chosen must be able to be stored in non-contiguous memory blocks.</p>
<p>As illustrated in Figure 3-3, <strong>the physical structure reflects the way data is stored in computer memory</strong> and it can be divided into contiguous space storage (arrays) and non-contiguous space storage (linked lists). The two types of physical structures exhibit complementary characteristics in terms of time efficiency and space efficiency.</p>
<p>Memory is a shared resource for all programs. When a block of memory is occupied by a program, it usually cannot be used by other programs at the same time. <strong>Therefore, in the design of data structures and algorithms, memory resources are an important consideration</strong>. For example, the peak memory occupied by an algorithm should not exceed the remaining free memory of the system; if there is a lack of contiguous large memory blocks, then the data structure chosen must be able to be stored in dispersed memory spaces.</p>
<p>As shown in Figure 3-3, <strong>physical structure reflects the way data is stored in computer memory</strong>, and can be divided into contiguous space storage (arrays) and dispersed space storage (linked lists). The two physical structures exhibit complementary characteristics in terms of time efficiency and space efficiency.</p>
<p><a class="glightbox" href="../classification_of_data_structure.assets/classification_phisical_structure.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Contiguous space storage and dispersed space storage" class="animation-figure" src="../classification_of_data_structure.assets/classification_phisical_structure.png" /></a></p>
<p align="center"> Figure 3-3 &nbsp; Contiguous space storage and dispersed space storage </p>
<p><strong>It is worth noting that all data structures are implemented based on arrays, linked lists, or a combination of both</strong>. For example, stacks and queues can be implemented using either arrays or linked lists; while implementations of hash tables may involve both arrays and linked lists.</p>
<p>It is worth noting that <strong>all data structures are implemented based on arrays, linked lists, or a combination of both</strong>. For example, stacks and queues can be implemented using either arrays or linked lists; while the implementation of hash tables may include both arrays and linked lists.</p>
<ul>
<li><strong>Array-based implementations</strong>: Stacks, Queues, Hash Tables, Trees, Heaps, Graphs, Matrices, Tensors (arrays with dimensions <span class="arithmatex">\(\geq 3\)</span>).</li>
<li><strong>Linked-list-based implementations</strong>: Stacks, Queues, Hash Tables, Trees, Heaps, Graphs, etc.</li>
<li><strong>Can be implemented based on arrays</strong>: Stacks, queues, hash tables, trees, heaps, graphs, matrices, tensors (arrays with dimensions <span class="arithmatex">\(\geq 3\)</span>), etc.</li>
<li><strong>Can be implemented based on linked lists</strong>: Stacks, queues, hash tables, trees, heaps, graphs, etc.</li>
</ul>
<p>Data structures implemented based on arrays are also called “Static Data Structures,” meaning their length cannot be changed after initialization. Conversely, those based on linked lists are called “Dynamic Data Structures,” which can still adjust their size during program execution.</p>
<p>After initialization, linked lists can still adjust their length during program execution, so they are also called "dynamic data structures". After initialization, the length of arrays cannot be changed, so they are also called "static data structures". It is worth noting that arrays can achieve length changes by reallocating memory, thus possessing a certain degree of "dynamism".</p>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>If you find it challenging to comprehend the physical structure, it is recommended that you read the next chapter, "Arrays and Linked Lists," and revisit this section later.</p>
<p>If you find it difficult to understand physical structure, it is recommended to read the next chapter first, and then review this section.</p>
</div>
<!-- Source file information -->
@@ -4446,7 +4446,7 @@ aria-label="Footer"
<a
href="../"
class="md-footer__link md-footer__link--prev"
aria-label="Previous: Chapter 3. &amp;nbsp; Data structures"
aria-label="Previous: Chapter 3. &amp;nbsp; Data Structures"
rel="prev"
>
<div class="md-footer__button md-icon">
@@ -4458,7 +4458,7 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
Chapter 3. &nbsp; Data structures
Chapter 3. &nbsp; Data Structures
</div>
</div>
</a>
@@ -4470,7 +4470,7 @@ aria-label="Footer"
<a
href="../basic_data_types/"
class="md-footer__link md-footer__link--next"
aria-label="Next: 3.2 Basic data types"
aria-label="Next: 3.2 Basic Data Types"
rel="next"
>
<div class="md-footer__title">
@@ -4478,7 +4478,7 @@ aria-label="Footer"
Next
</span>
<div class="md-ellipsis">
3.2 Basic data types
3.2 Basic Data Types
</div>
</div>
<div class="md-footer__button md-icon">
@@ -4571,7 +4571,7 @@ aria-label="Footer"
<nav class="md-footer__inner md-grid" aria-label="Footer" >
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="Previous: Chapter 3. &amp;nbsp; Data structures">
<a href="../" class="md-footer__link md-footer__link--prev" aria-label="Previous: Chapter 3. &amp;nbsp; Data Structures">
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11z"/></svg>
@@ -4581,20 +4581,20 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
Chapter 3. &nbsp; Data structures
Chapter 3. &nbsp; Data Structures
</div>
</div>
</a>
<a href="../basic_data_types/" class="md-footer__link md-footer__link--next" aria-label="Next: 3.2 Basic data types">
<a href="../basic_data_types/" class="md-footer__link md-footer__link--next" aria-label="Next: 3.2 Basic Data Types">
<div class="md-footer__title">
<span class="md-footer__direction">
Next
</span>
<div class="md-ellipsis">
3.2 Basic data types
3.2 Basic Data Types
</div>
</div>
<div class="md-footer__button md-icon">

View File

@@ -37,7 +37,7 @@
<title>Chapter 3.   Data structures - Hello Algo</title>
<title>Chapter 3.   Data Structures - Hello Algo</title>
@@ -58,8 +58,8 @@
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
<style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:300,300i,400,400i,700,700i%7CJetBrains+Mono:400,400i,700,700i&display=fallback">
<style>:root{--md-text-font:"Lato";--md-code-font:"JetBrains Mono"}</style>
@@ -154,7 +154,7 @@
<div class="md-header__topic" data-md-component="header-topic">
<span class="md-ellipsis">
Chapter 3. &nbsp; Data structures
Chapter 3. &nbsp; Data Structures
</span>
</div>
@@ -371,7 +371,7 @@
<span class="md-ellipsis">
Before starting
Before Starting
@@ -388,7 +388,7 @@
<span class="md-nav__icon md-icon"></span>
Before starting
Before Starting
</label>
@@ -487,7 +487,7 @@
<span class="md-ellipsis">
0.1 About this book
0.1 About This Book
@@ -515,7 +515,7 @@
<span class="md-ellipsis">
0.2 How to read
0.2 How to Use This Book
@@ -604,7 +604,7 @@
<span class="md-ellipsis">
Chapter 1. Encounter with algorithms
Chapter 1. Encounter With Algorithms
@@ -626,7 +626,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 1. Encounter with algorithms
Chapter 1. Encounter With Algorithms
</label>
@@ -648,7 +648,7 @@
<span class="md-ellipsis">
1.1 Algorithms are everywhere
1.1 Algorithms Are Everywhere
@@ -676,7 +676,7 @@
<span class="md-ellipsis">
1.2 What is an algorithm
1.2 What Is an Algorithm
@@ -769,7 +769,7 @@
<span class="md-ellipsis">
Chapter 2. Complexity analysis
Chapter 2. Complexity Analysis
@@ -791,7 +791,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 2. Complexity analysis
Chapter 2. Complexity Analysis
</label>
@@ -813,7 +813,7 @@
<span class="md-ellipsis">
2.1 Algorithm efficiency assessment
2.1 Algorithm Efficiency Evaluation
@@ -841,7 +841,7 @@
<span class="md-ellipsis">
2.2 Iteration and recursion
2.2 Iteration and Recursion
@@ -869,7 +869,7 @@
<span class="md-ellipsis">
2.3 Time complexity
2.3 Time Complexity
@@ -897,7 +897,7 @@
<span class="md-ellipsis">
2.4 Space complexity
2.4 Space Complexity
@@ -992,7 +992,7 @@
<span class="md-ellipsis">
Chapter 3. Data structures
Chapter 3. Data Structures
@@ -1014,7 +1014,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 3. Data structures
Chapter 3. Data Structures
</label>
@@ -1036,7 +1036,7 @@
<span class="md-ellipsis">
3.1 Classification of data structures
3.1 Classification of Data Structures
@@ -1064,7 +1064,7 @@
<span class="md-ellipsis">
3.2 Basic data types
3.2 Basic Data Types
@@ -1092,7 +1092,7 @@
<span class="md-ellipsis">
3.3 Number encoding *
3.3 Number Encoding *
@@ -1120,7 +1120,7 @@
<span class="md-ellipsis">
3.4 Character encoding *
3.4 Character Encoding *
@@ -1213,7 +1213,7 @@
<span class="md-ellipsis">
Chapter 4. Array and linked list
Chapter 4. Array and Linked List
@@ -1235,7 +1235,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 4. Array and linked list
Chapter 4. Array and Linked List
</label>
@@ -1285,7 +1285,7 @@
<span class="md-ellipsis">
4.2 Linked list
4.2 Linked List
@@ -1341,7 +1341,7 @@
<span class="md-ellipsis">
4.4 Memory and cache *
4.4 Memory and Cache *
@@ -1432,7 +1432,7 @@
<span class="md-ellipsis">
Chapter 5. Stack and queue
Chapter 5. Stack and Queue
@@ -1454,7 +1454,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 5. Stack and queue
Chapter 5. Stack and Queue
</label>
@@ -1532,7 +1532,7 @@
<span class="md-ellipsis">
5.3 Double-ended queue
5.3 Double-Ended Queue
@@ -1623,7 +1623,7 @@
<span class="md-ellipsis">
Chapter 6. Hash table
Chapter 6. Hashing
@@ -1645,7 +1645,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 6. Hash table
Chapter 6. Hashing
</label>
@@ -1667,7 +1667,7 @@
<span class="md-ellipsis">
6.1 Hash table
6.1 Hash Table
@@ -1695,7 +1695,7 @@
<span class="md-ellipsis">
6.2 Hash collision
6.2 Hash Collision
@@ -1723,7 +1723,7 @@
<span class="md-ellipsis">
6.3 Hash algorithm
6.3 Hash Algorithm
@@ -1862,7 +1862,7 @@
<span class="md-ellipsis">
7.1 Binary tree
7.1 Binary Tree
@@ -1890,7 +1890,7 @@
<span class="md-ellipsis">
7.2 Binary tree traversal
7.2 Binary Tree Traversal
@@ -1918,7 +1918,7 @@
<span class="md-ellipsis">
7.3 Array Representation of tree
7.3 Array Representation of Tree
@@ -1946,7 +1946,7 @@
<span class="md-ellipsis">
7.4 Binary Search tree
7.4 Binary Search Tree
@@ -1974,7 +1974,7 @@
<span class="md-ellipsis">
7.5 AVL tree *
7.5 AVL Tree *
@@ -2137,7 +2137,7 @@
<span class="md-ellipsis">
8.2 Building a heap
8.2 Building a Heap
@@ -2165,7 +2165,7 @@
<span class="md-ellipsis">
8.3 Top-k problem
8.3 Top-K Problem
@@ -2328,7 +2328,7 @@
<span class="md-ellipsis">
9.2 Basic graph operations
9.2 Basic Operations on Graphs
@@ -2356,7 +2356,7 @@
<span class="md-ellipsis">
9.3 Graph traversal
9.3 Graph Traversal
@@ -2495,7 +2495,7 @@
<span class="md-ellipsis">
10.1 Binary search
10.1 Binary Search
@@ -2523,7 +2523,7 @@
<span class="md-ellipsis">
10.2 Binary search insertion
10.2 Binary Search Insertion
@@ -2551,7 +2551,7 @@
<span class="md-ellipsis">
10.3 Binary search boundaries
10.3 Binary Search Edge Cases
@@ -2579,7 +2579,7 @@
<span class="md-ellipsis">
10.4 Hashing optimization strategies
10.4 Hash Optimization Strategy
@@ -2607,7 +2607,7 @@
<span class="md-ellipsis">
10.5 Search algorithms revisited
10.5 Search Algorithms Revisited
@@ -2756,7 +2756,7 @@
<span class="md-ellipsis">
11.1 Sorting algorithms
11.1 Sorting Algorithms
@@ -2784,7 +2784,7 @@
<span class="md-ellipsis">
11.2 Selection sort
11.2 Selection Sort
@@ -2812,7 +2812,7 @@
<span class="md-ellipsis">
11.3 Bubble sort
11.3 Bubble Sort
@@ -2840,7 +2840,7 @@
<span class="md-ellipsis">
11.4 Insertion sort
11.4 Insertion Sort
@@ -2868,7 +2868,7 @@
<span class="md-ellipsis">
11.5 Quick sort
11.5 Quick Sort
@@ -2896,7 +2896,7 @@
<span class="md-ellipsis">
11.6 Merge sort
11.6 Merge Sort
@@ -2924,7 +2924,7 @@
<span class="md-ellipsis">
11.7 Heap sort
11.7 Heap Sort
@@ -2952,7 +2952,7 @@
<span class="md-ellipsis">
11.8 Bucket sort
11.8 Bucket Sort
@@ -2980,7 +2980,7 @@
<span class="md-ellipsis">
11.9 Counting sort
11.9 Counting Sort
@@ -3008,7 +3008,7 @@
<span class="md-ellipsis">
11.10 Radix sort
11.10 Radix Sort
@@ -3101,7 +3101,7 @@
<span class="md-ellipsis">
Chapter 12. Divide and conquer
Chapter 12. Divide and Conquer
@@ -3123,7 +3123,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 12. Divide and conquer
Chapter 12. Divide and Conquer
</label>
@@ -3145,7 +3145,7 @@
<span class="md-ellipsis">
12.1 Divide and conquer algorithms
12.1 Divide and Conquer Algorithms
@@ -3173,7 +3173,7 @@
<span class="md-ellipsis">
12.2 Divide and conquer search strategy
12.2 Divide and Conquer Search Strategy
@@ -3201,7 +3201,7 @@
<span class="md-ellipsis">
12.3 Building binary tree problem
12.3 Building a Binary Tree Problem
@@ -3229,7 +3229,7 @@
<span class="md-ellipsis">
12.4 Tower of Hanoi Problem
12.4 Hanoi Tower Problem
@@ -3366,7 +3366,7 @@
<span class="md-ellipsis">
13.1 Backtracking algorithms
13.1 Backtracking Algorithm
@@ -3394,7 +3394,7 @@
<span class="md-ellipsis">
13.2 Permutation problem
13.2 Permutations Problem
@@ -3422,7 +3422,7 @@
<span class="md-ellipsis">
13.3 Subset sum problem
13.3 Subset-Sum Problem
@@ -3450,7 +3450,7 @@
<span class="md-ellipsis">
13.4 n queens problem
13.4 N-Queens Problem
@@ -3547,7 +3547,7 @@
<span class="md-ellipsis">
Chapter 14. Dynamic programming
Chapter 14. Dynamic Programming
@@ -3569,7 +3569,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 14. Dynamic programming
Chapter 14. Dynamic Programming
</label>
@@ -3591,7 +3591,7 @@
<span class="md-ellipsis">
14.1 Introduction to dynamic programming
14.1 Introduction to Dynamic Programming
@@ -3619,7 +3619,7 @@
<span class="md-ellipsis">
14.2 Characteristics of DP problems
14.2 Characteristics of Dynamic Programming Problems
@@ -3647,7 +3647,7 @@
<span class="md-ellipsis">
14.3 DP problem-solving approach
14.3 Dynamic Programming Problem-Solving Approach
@@ -3675,7 +3675,7 @@
<span class="md-ellipsis">
14.4 0-1 Knapsack problem
14.4 0-1 Knapsack Problem
@@ -3703,7 +3703,7 @@
<span class="md-ellipsis">
14.5 Unbounded knapsack problem
14.5 Unbounded Knapsack Problem
@@ -3731,7 +3731,7 @@
<span class="md-ellipsis">
14.6 Edit distance problem
14.6 Edit Distance Problem
@@ -3868,7 +3868,7 @@
<span class="md-ellipsis">
15.1 Greedy algorithms
15.1 Greedy Algorithm
@@ -3896,7 +3896,7 @@
<span class="md-ellipsis">
15.2 Fractional knapsack problem
15.2 Fractional Knapsack Problem
@@ -3924,7 +3924,7 @@
<span class="md-ellipsis">
15.3 Maximum capacity problem
15.3 Maximum Capacity Problem
@@ -3952,7 +3952,7 @@
<span class="md-ellipsis">
15.4 Maximum product cutting problem
15.4 Maximum Product Cutting Problem
@@ -4085,7 +4085,7 @@
<span class="md-ellipsis">
16.1 Installation
16.1 Programming Environment Installation
@@ -4113,7 +4113,7 @@
<span class="md-ellipsis">
16.2 Contributing
16.2 Contributing Together
@@ -4141,7 +4141,7 @@
<span class="md-ellipsis">
16.3 Terminology
16.3 Terminology Table
@@ -4302,19 +4302,19 @@
<!-- Page content -->
<h1 id="chapter-3-data-structures">Chapter 3. &nbsp; Data structures<a class="headerlink" href="#chapter-3-data-structures" title="Permanent link">&para;</a></h1>
<h1 id="chapter-3-data-structures">Chapter 3. &nbsp; Data Structures<a class="headerlink" href="#chapter-3-data-structures" title="Permanent link">&para;</a></h1>
<p><a class="glightbox" href="../assets/covers/chapter_data_structure.jpg" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Data structures" class="cover-image" src="../assets/covers/chapter_data_structure.jpg" /></a></p>
<div class="admonition abstract">
<p class="admonition-title">Abstract</p>
<p>Data structures serve as a robust and diverse framework.</p>
<p>They offer a blueprint for the orderly organization of data, upon which algorithms come to life.</p>
<p>Data structure is like a sturdy and diverse framework.</p>
<p>It provides a blueprint for the orderly organization of data, upon which algorithms come to life.</p>
</div>
<h2 id="chapter-contents">Chapter contents<a class="headerlink" href="#chapter-contents" title="Permanent link">&para;</a></h2>
<ul>
<li><a href="classification_of_data_structure/">3.1 &nbsp; Classification of data structures</a></li>
<li><a href="basic_data_types/">3.2 &nbsp; Basic data types</a></li>
<li><a href="number_encoding/">3.3 &nbsp; Number encoding *</a></li>
<li><a href="character_encoding/">3.4 &nbsp; Character encoding *</a></li>
<li><a href="classification_of_data_structure/">3.1 &nbsp; Classification of Data Structures</a></li>
<li><a href="basic_data_types/">3.2 &nbsp; Basic Data Types</a></li>
<li><a href="number_encoding/">3.3 &nbsp; Number Encoding *</a></li>
<li><a href="character_encoding/">3.4 &nbsp; Character Encoding *</a></li>
<li><a href="summary/">3.5 &nbsp; Summary</a></li>
</ul>
@@ -4363,7 +4363,7 @@ aria-label="Footer"
<a
href="classification_of_data_structure/"
class="md-footer__link md-footer__link--next"
aria-label="Next: 3.1 Classification of data structures"
aria-label="Next: 3.1 Classification of Data Structures"
rel="next"
>
<div class="md-footer__title">
@@ -4371,7 +4371,7 @@ aria-label="Footer"
Next
</span>
<div class="md-ellipsis">
3.1 Classification of data structures
3.1 Classification of Data Structures
</div>
</div>
<div class="md-footer__button md-icon">
@@ -4481,13 +4481,13 @@ aria-label="Footer"
<a href="classification_of_data_structure/" class="md-footer__link md-footer__link--next" aria-label="Next: 3.1 Classification of data structures">
<a href="classification_of_data_structure/" class="md-footer__link md-footer__link--next" aria-label="Next: 3.1 Classification of Data Structures">
<div class="md-footer__title">
<span class="md-footer__direction">
Next
</span>
<div class="md-ellipsis">
3.1 Classification of data structures
3.1 Classification of Data Structures
</div>
</div>
<div class="md-footer__button md-icon">

View File

@@ -37,7 +37,7 @@
<title>3.3 Number encoding * - Hello Algo</title>
<title>3.3 Number Encoding * - Hello Algo</title>
@@ -58,8 +58,8 @@
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
<style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:300,300i,400,400i,700,700i%7CJetBrains+Mono:400,400i,700,700i&display=fallback">
<style>:root{--md-text-font:"Lato";--md-code-font:"JetBrains Mono"}</style>
@@ -154,7 +154,7 @@
<div class="md-header__topic" data-md-component="header-topic">
<span class="md-ellipsis">
3.3 Number encoding *
3.3 Number Encoding *
</span>
</div>
@@ -371,7 +371,7 @@
<span class="md-ellipsis">
Before starting
Before Starting
@@ -388,7 +388,7 @@
<span class="md-nav__icon md-icon"></span>
Before starting
Before Starting
</label>
@@ -487,7 +487,7 @@
<span class="md-ellipsis">
0.1 About this book
0.1 About This Book
@@ -515,7 +515,7 @@
<span class="md-ellipsis">
0.2 How to read
0.2 How to Use This Book
@@ -604,7 +604,7 @@
<span class="md-ellipsis">
Chapter 1. Encounter with algorithms
Chapter 1. Encounter With Algorithms
@@ -626,7 +626,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 1. Encounter with algorithms
Chapter 1. Encounter With Algorithms
</label>
@@ -648,7 +648,7 @@
<span class="md-ellipsis">
1.1 Algorithms are everywhere
1.1 Algorithms Are Everywhere
@@ -676,7 +676,7 @@
<span class="md-ellipsis">
1.2 What is an algorithm
1.2 What Is an Algorithm
@@ -769,7 +769,7 @@
<span class="md-ellipsis">
Chapter 2. Complexity analysis
Chapter 2. Complexity Analysis
@@ -791,7 +791,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 2. Complexity analysis
Chapter 2. Complexity Analysis
</label>
@@ -813,7 +813,7 @@
<span class="md-ellipsis">
2.1 Algorithm efficiency assessment
2.1 Algorithm Efficiency Evaluation
@@ -841,7 +841,7 @@
<span class="md-ellipsis">
2.2 Iteration and recursion
2.2 Iteration and Recursion
@@ -869,7 +869,7 @@
<span class="md-ellipsis">
2.3 Time complexity
2.3 Time Complexity
@@ -897,7 +897,7 @@
<span class="md-ellipsis">
2.4 Space complexity
2.4 Space Complexity
@@ -992,7 +992,7 @@
<span class="md-ellipsis">
Chapter 3. Data structures
Chapter 3. Data Structures
@@ -1014,7 +1014,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 3. Data structures
Chapter 3. Data Structures
</label>
@@ -1036,7 +1036,7 @@
<span class="md-ellipsis">
3.1 Classification of data structures
3.1 Classification of Data Structures
@@ -1064,7 +1064,7 @@
<span class="md-ellipsis">
3.2 Basic data types
3.2 Basic Data Types
@@ -1101,7 +1101,7 @@
<span class="md-ellipsis">
3.3 Number encoding *
3.3 Number Encoding *
@@ -1119,7 +1119,7 @@
<span class="md-ellipsis">
3.3 Number encoding *
3.3 Number Encoding *
@@ -1145,10 +1145,10 @@
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
<li class="md-nav__item">
<a href="#331-integer-encoding" class="md-nav__link">
<a href="#331-sign-magnitude-1s-complement-and-2s-complement" class="md-nav__link">
<span class="md-ellipsis">
3.3.1 &nbsp; Integer encoding
3.3.1 &nbsp; Sign-Magnitude, 1's Complement, and 2's Complement
</span>
</a>
@@ -1159,7 +1159,7 @@
<a href="#332-floating-point-number-encoding" class="md-nav__link">
<span class="md-ellipsis">
3.3.2 &nbsp; Floating-point number encoding
3.3.2 &nbsp; Floating-Point Number Encoding
</span>
</a>
@@ -1189,7 +1189,7 @@
<span class="md-ellipsis">
3.4 Character encoding *
3.4 Character Encoding *
@@ -1282,7 +1282,7 @@
<span class="md-ellipsis">
Chapter 4. Array and linked list
Chapter 4. Array and Linked List
@@ -1304,7 +1304,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 4. Array and linked list
Chapter 4. Array and Linked List
</label>
@@ -1354,7 +1354,7 @@
<span class="md-ellipsis">
4.2 Linked list
4.2 Linked List
@@ -1410,7 +1410,7 @@
<span class="md-ellipsis">
4.4 Memory and cache *
4.4 Memory and Cache *
@@ -1501,7 +1501,7 @@
<span class="md-ellipsis">
Chapter 5. Stack and queue
Chapter 5. Stack and Queue
@@ -1523,7 +1523,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 5. Stack and queue
Chapter 5. Stack and Queue
</label>
@@ -1601,7 +1601,7 @@
<span class="md-ellipsis">
5.3 Double-ended queue
5.3 Double-Ended Queue
@@ -1692,7 +1692,7 @@
<span class="md-ellipsis">
Chapter 6. Hash table
Chapter 6. Hashing
@@ -1714,7 +1714,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 6. Hash table
Chapter 6. Hashing
</label>
@@ -1736,7 +1736,7 @@
<span class="md-ellipsis">
6.1 Hash table
6.1 Hash Table
@@ -1764,7 +1764,7 @@
<span class="md-ellipsis">
6.2 Hash collision
6.2 Hash Collision
@@ -1792,7 +1792,7 @@
<span class="md-ellipsis">
6.3 Hash algorithm
6.3 Hash Algorithm
@@ -1931,7 +1931,7 @@
<span class="md-ellipsis">
7.1 Binary tree
7.1 Binary Tree
@@ -1959,7 +1959,7 @@
<span class="md-ellipsis">
7.2 Binary tree traversal
7.2 Binary Tree Traversal
@@ -1987,7 +1987,7 @@
<span class="md-ellipsis">
7.3 Array Representation of tree
7.3 Array Representation of Tree
@@ -2015,7 +2015,7 @@
<span class="md-ellipsis">
7.4 Binary Search tree
7.4 Binary Search Tree
@@ -2043,7 +2043,7 @@
<span class="md-ellipsis">
7.5 AVL tree *
7.5 AVL Tree *
@@ -2206,7 +2206,7 @@
<span class="md-ellipsis">
8.2 Building a heap
8.2 Building a Heap
@@ -2234,7 +2234,7 @@
<span class="md-ellipsis">
8.3 Top-k problem
8.3 Top-K Problem
@@ -2397,7 +2397,7 @@
<span class="md-ellipsis">
9.2 Basic graph operations
9.2 Basic Operations on Graphs
@@ -2425,7 +2425,7 @@
<span class="md-ellipsis">
9.3 Graph traversal
9.3 Graph Traversal
@@ -2564,7 +2564,7 @@
<span class="md-ellipsis">
10.1 Binary search
10.1 Binary Search
@@ -2592,7 +2592,7 @@
<span class="md-ellipsis">
10.2 Binary search insertion
10.2 Binary Search Insertion
@@ -2620,7 +2620,7 @@
<span class="md-ellipsis">
10.3 Binary search boundaries
10.3 Binary Search Edge Cases
@@ -2648,7 +2648,7 @@
<span class="md-ellipsis">
10.4 Hashing optimization strategies
10.4 Hash Optimization Strategy
@@ -2676,7 +2676,7 @@
<span class="md-ellipsis">
10.5 Search algorithms revisited
10.5 Search Algorithms Revisited
@@ -2825,7 +2825,7 @@
<span class="md-ellipsis">
11.1 Sorting algorithms
11.1 Sorting Algorithms
@@ -2853,7 +2853,7 @@
<span class="md-ellipsis">
11.2 Selection sort
11.2 Selection Sort
@@ -2881,7 +2881,7 @@
<span class="md-ellipsis">
11.3 Bubble sort
11.3 Bubble Sort
@@ -2909,7 +2909,7 @@
<span class="md-ellipsis">
11.4 Insertion sort
11.4 Insertion Sort
@@ -2937,7 +2937,7 @@
<span class="md-ellipsis">
11.5 Quick sort
11.5 Quick Sort
@@ -2965,7 +2965,7 @@
<span class="md-ellipsis">
11.6 Merge sort
11.6 Merge Sort
@@ -2993,7 +2993,7 @@
<span class="md-ellipsis">
11.7 Heap sort
11.7 Heap Sort
@@ -3021,7 +3021,7 @@
<span class="md-ellipsis">
11.8 Bucket sort
11.8 Bucket Sort
@@ -3049,7 +3049,7 @@
<span class="md-ellipsis">
11.9 Counting sort
11.9 Counting Sort
@@ -3077,7 +3077,7 @@
<span class="md-ellipsis">
11.10 Radix sort
11.10 Radix Sort
@@ -3170,7 +3170,7 @@
<span class="md-ellipsis">
Chapter 12. Divide and conquer
Chapter 12. Divide and Conquer
@@ -3192,7 +3192,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 12. Divide and conquer
Chapter 12. Divide and Conquer
</label>
@@ -3214,7 +3214,7 @@
<span class="md-ellipsis">
12.1 Divide and conquer algorithms
12.1 Divide and Conquer Algorithms
@@ -3242,7 +3242,7 @@
<span class="md-ellipsis">
12.2 Divide and conquer search strategy
12.2 Divide and Conquer Search Strategy
@@ -3270,7 +3270,7 @@
<span class="md-ellipsis">
12.3 Building binary tree problem
12.3 Building a Binary Tree Problem
@@ -3298,7 +3298,7 @@
<span class="md-ellipsis">
12.4 Tower of Hanoi Problem
12.4 Hanoi Tower Problem
@@ -3435,7 +3435,7 @@
<span class="md-ellipsis">
13.1 Backtracking algorithms
13.1 Backtracking Algorithm
@@ -3463,7 +3463,7 @@
<span class="md-ellipsis">
13.2 Permutation problem
13.2 Permutations Problem
@@ -3491,7 +3491,7 @@
<span class="md-ellipsis">
13.3 Subset sum problem
13.3 Subset-Sum Problem
@@ -3519,7 +3519,7 @@
<span class="md-ellipsis">
13.4 n queens problem
13.4 N-Queens Problem
@@ -3616,7 +3616,7 @@
<span class="md-ellipsis">
Chapter 14. Dynamic programming
Chapter 14. Dynamic Programming
@@ -3638,7 +3638,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 14. Dynamic programming
Chapter 14. Dynamic Programming
</label>
@@ -3660,7 +3660,7 @@
<span class="md-ellipsis">
14.1 Introduction to dynamic programming
14.1 Introduction to Dynamic Programming
@@ -3688,7 +3688,7 @@
<span class="md-ellipsis">
14.2 Characteristics of DP problems
14.2 Characteristics of Dynamic Programming Problems
@@ -3716,7 +3716,7 @@
<span class="md-ellipsis">
14.3 DP problem-solving approach
14.3 Dynamic Programming Problem-Solving Approach
@@ -3744,7 +3744,7 @@
<span class="md-ellipsis">
14.4 0-1 Knapsack problem
14.4 0-1 Knapsack Problem
@@ -3772,7 +3772,7 @@
<span class="md-ellipsis">
14.5 Unbounded knapsack problem
14.5 Unbounded Knapsack Problem
@@ -3800,7 +3800,7 @@
<span class="md-ellipsis">
14.6 Edit distance problem
14.6 Edit Distance Problem
@@ -3937,7 +3937,7 @@
<span class="md-ellipsis">
15.1 Greedy algorithms
15.1 Greedy Algorithm
@@ -3965,7 +3965,7 @@
<span class="md-ellipsis">
15.2 Fractional knapsack problem
15.2 Fractional Knapsack Problem
@@ -3993,7 +3993,7 @@
<span class="md-ellipsis">
15.3 Maximum capacity problem
15.3 Maximum Capacity Problem
@@ -4021,7 +4021,7 @@
<span class="md-ellipsis">
15.4 Maximum product cutting problem
15.4 Maximum Product Cutting Problem
@@ -4154,7 +4154,7 @@
<span class="md-ellipsis">
16.1 Installation
16.1 Programming Environment Installation
@@ -4182,7 +4182,7 @@
<span class="md-ellipsis">
16.2 Contributing
16.2 Contributing Together
@@ -4210,7 +4210,7 @@
<span class="md-ellipsis">
16.3 Terminology
16.3 Terminology Table
@@ -4324,10 +4324,10 @@
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
<li class="md-nav__item">
<a href="#331-integer-encoding" class="md-nav__link">
<a href="#331-sign-magnitude-1s-complement-and-2s-complement" class="md-nav__link">
<span class="md-ellipsis">
3.3.1 &nbsp; Integer encoding
3.3.1 &nbsp; Sign-Magnitude, 1's Complement, and 2's Complement
</span>
</a>
@@ -4338,7 +4338,7 @@
<a href="#332-floating-point-number-encoding" class="md-nav__link">
<span class="md-ellipsis">
3.3.2 &nbsp; Floating-point number encoding
3.3.2 &nbsp; Floating-Point Number Encoding
</span>
</a>
@@ -4382,24 +4382,24 @@
<!-- Page content -->
<h1 id="33-number-encoding">3.3 &nbsp; Number encoding *<a class="headerlink" href="#33-number-encoding" title="Permanent link">&para;</a></h1>
<h1 id="33-number-encoding">3.3 &nbsp; Number Encoding *<a class="headerlink" href="#33-number-encoding" title="Permanent link">&para;</a></h1>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>In this book, chapters marked with an asterisk '*' are optional readings. If you are short on time or find them challenging, you may skip these initially and return to them after completing the essential chapters.</p>
<p>In this book, chapters marked with an asterisk * are optional readings. If you are short on time or find them challenging, you may skip these initially and return to them after completing the essential chapters.</p>
</div>
<h2 id="331-integer-encoding">3.3.1 &nbsp; Integer encoding<a class="headerlink" href="#331-integer-encoding" title="Permanent link">&para;</a></h2>
<p>In the table from the previous section, we observed that all integer types can represent one more negative number than positive numbers, such as the <code>byte</code> range of <span class="arithmatex">\([-128, 127]\)</span>. This phenomenon seems counterintuitive, and its underlying reason involves knowledge of sign-magnitude, one's complement, and two's complement encoding.</p>
<p>Firstly, it's important to note that <strong>numbers are stored in computers using the two's complement form</strong>. Before analyzing why this is the case, let's define these three encoding methods:</p>
<h2 id="331-sign-magnitude-1s-complement-and-2s-complement">3.3.1 &nbsp; Sign-Magnitude, 1's Complement, and 2's Complement<a class="headerlink" href="#331-sign-magnitude-1s-complement-and-2s-complement" title="Permanent link">&para;</a></h2>
<p>In the table from the previous section, we found that all integer types can represent one more negative number than positive numbers. For example, the <code>byte</code> range is <span class="arithmatex">\([-128, 127]\)</span>. This phenomenon is counterintuitive, and its underlying reason involves knowledge of sign-magnitude, 1's complement, and 2's complement.</p>
<p>First, it should be noted that <strong>numbers are stored in computers in the form of "2's complement"</strong>. Before analyzing the reasons for this, let's first define these three concepts.</p>
<ul>
<li><strong>Sign-magnitude</strong>: The highest bit of a binary representation of a number is considered the sign bit, where <span class="arithmatex">\(0\)</span> represents a positive number and <span class="arithmatex">\(1\)</span> represents a negative number. The remaining bits represent the value of the number.</li>
<li><strong>One's complement</strong>: The one's complement of a positive number is the same as its sign-magnitude. For negative numbers, it's obtained by inverting all bits except the sign bit.</li>
<li><strong>Two's complement</strong>: The two's complement of a positive number is the same as its sign-magnitude. For negative numbers, it's obtained by adding <span class="arithmatex">\(1\)</span> to their one's complement.</li>
<li><strong>Sign-magnitude</strong>: We treat the highest bit of the binary representation of a number as the sign bit, where <span class="arithmatex">\(0\)</span> represents a positive number and <span class="arithmatex">\(1\)</span> represents a negative number, and the remaining bits represent the value of the number.</li>
<li><strong>1's complement</strong>: The 1's complement of a positive number is the same as its sign-magnitude. For a negative number, the 1's complement is obtained by inverting all bits except the sign bit of its sign-magnitude.</li>
<li><strong>2's complement</strong>: The 2's complement of a positive number is the same as its sign-magnitude. For a negative number, the 2's complement is obtained by adding <span class="arithmatex">\(1\)</span> to its 1's complement.</li>
</ul>
<p>Figure 3-4 illustrates the conversions among sign-magnitude, one's complement, and two's complement:</p>
<p><a class="glightbox" href="../number_encoding.assets/1s_2s_complement.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Conversions between sign-magnitude, one's complement, and two's complement" class="animation-figure" src="../number_encoding.assets/1s_2s_complement.png" /></a></p>
<p align="center"> Figure 3-4 &nbsp; Conversions between sign-magnitude, one's complement, and two's complement </p>
<p>Figure 3-4 shows the conversion methods among sign-magnitude, 1's complement, and 2's complement.</p>
<p><a class="glightbox" href="../number_encoding.assets/1s_2s_complement.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Conversions among sign-magnitude, 1's complement, and 2's complement" class="animation-figure" src="../number_encoding.assets/1s_2s_complement.png" /></a></p>
<p align="center"> Figure 3-4 &nbsp; Conversions among sign-magnitude, 1's complement, and 2's complement </p>
<p>Although <u>sign-magnitude</u> is the most intuitive, it has limitations. For one, <strong>negative numbers in sign-magnitude cannot be directly used in calculations</strong>. For example, in sign-magnitude, calculating <span class="arithmatex">\(1 + (-2)\)</span> results in <span class="arithmatex">\(-3\)</span>, which is incorrect.</p>
<p><u>Sign-magnitude</u>, although the most intuitive, has some limitations. On one hand, <strong>the sign-magnitude of negative numbers cannot be directly used in operations</strong>. For example, calculating <span class="arithmatex">\(1 + (-2)\)</span> in sign-magnitude yields <span class="arithmatex">\(-3\)</span>, which is clearly incorrect.</p>
<div class="arithmatex">\[
\begin{aligned}
&amp; 1 + (-2) \newline
@@ -4408,85 +4408,85 @@
&amp; \rightarrow -3
\end{aligned}
\]</div>
<p>To address this, computers introduced the <u>one's complement</u>. If we convert to one's complement and calculate <span class="arithmatex">\(1 + (-2)\)</span>, then convert the result back to sign-magnitude, we get the correct result of <span class="arithmatex">\(-1\)</span>.</p>
<p>To solve this problem, computers introduced <u>1's complement</u>. If we first convert sign-magnitude to 1's complement and calculate <span class="arithmatex">\(1 + (-2)\)</span> in 1's complement, then convert the result back to sign-magnitude, we can obtain the correct result of <span class="arithmatex">\(-1\)</span>.</p>
<div class="arithmatex">\[
\begin{aligned}
&amp; 1 + (-2) \newline
&amp; \rightarrow 0000 \; 0001 \; \text{(Sign-magnitude)} + 1000 \; 0010 \; \text{(Sign-magnitude)} \newline
&amp; = 0000 \; 0001 \; \text{(One's complement)} + 1111 \; 1101 \; \text{(One's complement)} \newline
&amp; = 1111 \; 1110 \; \text{(One's complement)} \newline
&amp; = 0000 \; 0001 \; \text{(1's complement)} + 1111 \; 1101 \; \text{(1's complement)} \newline
&amp; = 1111 \; 1110 \; \text{(1's complement)} \newline
&amp; = 1000 \; 0001 \; \text{(Sign-magnitude)} \newline
&amp; \rightarrow -1
\end{aligned}
\]</div>
<p>Additionally, <strong>there are two representations of zero in sign-magnitude</strong>: <span class="arithmatex">\(+0\)</span> and <span class="arithmatex">\(-0\)</span>. This means two different binary encodings for zero, which could lead to ambiguity. For example, in conditional checks, not differentiating between positive and negative zero might result in incorrect outcomes. Addressing this ambiguity would require additional checks, potentially reducing computational efficiency.</p>
<p>On the other hand, <strong>the sign-magnitude of the number zero has two representations, <span class="arithmatex">\(+0\)</span> and <span class="arithmatex">\(-0\)</span></strong>. This means that the number zero corresponds to two different binary encodings, which may cause ambiguity. For example, in conditional judgments, if we don't distinguish between positive zero and negative zero, it may lead to incorrect judgment results. If we want to handle the ambiguity of positive and negative zero, we need to introduce additional judgment operations, which may reduce the computational efficiency of the computer.</p>
<div class="arithmatex">\[
\begin{aligned}
+0 &amp; \rightarrow 0000 \; 0000 \newline
-0 &amp; \rightarrow 1000 \; 0000
\end{aligned}
\]</div>
<p>Like sign-magnitude, one's complement also suffers from the positive and negative zero ambiguity. Therefore, computers further introduced the <u>two's complement</u>. Let's observe the conversion process for negative zero in sign-magnitude, one's complement, and two's complement:</p>
<p>Like sign-magnitude, 1's complement also has the problem of positive and negative zero ambiguity. Therefore, computers further introduced <u>2's complement</u>. Let's first observe the conversion process of negative zero from sign-magnitude to 1's complement to 2's complement:</p>
<div class="arithmatex">\[
\begin{aligned}
-0 \rightarrow \; &amp; 1000 \; 0000 \; \text{(Sign-magnitude)} \newline
= \; &amp; 1111 \; 1111 \; \text{(One's complement)} \newline
= 1 \; &amp; 0000 \; 0000 \; \text{(Two's complement)} \newline
= \; &amp; 1111 \; 1111 \; \text{(1's complement)} \newline
= 1 \; &amp; 0000 \; 0000 \; \text{(2's complement)} \newline
\end{aligned}
\]</div>
<p>Adding <span class="arithmatex">\(1\)</span> to the one's complement of negative zero produces a carry, but with <code>byte</code> length being only 8 bits, the carried-over <span class="arithmatex">\(1\)</span> to the 9<sup>th</sup> bit is discarded. Therefore, <strong>the two's complement of negative zero is <span class="arithmatex">\(0000 \; 0000\)</span></strong>, the same as positive zero, thus resolving the ambiguity.</p>
<p>One last puzzle is the <span class="arithmatex">\([-128, 127]\)</span> range for <code>byte</code>, with an additional negative number, <span class="arithmatex">\(-128\)</span>. We observe that for the interval <span class="arithmatex">\([-127, +127]\)</span>, all integers have corresponding sign-magnitude, one's complement, and two's complement, allowing for mutual conversion between them.</p>
<p>However, <strong>the two's complement <span class="arithmatex">\(1000 \; 0000\)</span> is an exception without a corresponding sign-magnitude</strong>. According to the conversion method, its sign-magnitude would be <span class="arithmatex">\(0000 \; 0000\)</span>, indicating zero. This presents a contradiction because its two's complement should represent itself. Computers designate this special two's complement <span class="arithmatex">\(1000 \; 0000\)</span> as representing <span class="arithmatex">\(-128\)</span>. In fact, the calculation of <span class="arithmatex">\((-1) + (-127)\)</span> in two's complement results in <span class="arithmatex">\(-128\)</span>.</p>
<p>Adding <span class="arithmatex">\(1\)</span> to the 1's complement of negative zero produces a carry, but since the <code>byte</code> type has a length of only 8 bits, the <span class="arithmatex">\(1\)</span> that overflows to the 9<sup>th</sup> bit is discarded. That is to say, <strong>the 2's complement of negative zero is <span class="arithmatex">\(0000 \; 0000\)</span>, which is the same as the 2's complement of positive zero</strong>. This means that in 2's complement representation, there is only one zero, and the positive and negative zero ambiguity is thus resolved.</p>
<p>One last question remains: the range of the <code>byte</code> type is <span class="arithmatex">\([-128, 127]\)</span>, and how is the extra negative number <span class="arithmatex">\(-128\)</span> obtained? We notice that all integers in the interval <span class="arithmatex">\([-127, +127]\)</span> have corresponding sign-magnitude, 1's complement, and 2's complement, and sign-magnitude and 2's complement can be converted to each other.</p>
<p>However, <strong>the 2's complement <span class="arithmatex">\(1000 \; 0000\)</span> is an exception, and it does not have a corresponding sign-magnitude</strong>. According to the conversion method, we get that the sign-magnitude of this 2's complement is <span class="arithmatex">\(0000 \; 0000\)</span>. This is clearly contradictory because this sign-magnitude represents the number <span class="arithmatex">\(0\)</span>, and its 2's complement should be itself. The computer specifies that this special 2's complement <span class="arithmatex">\(1000 \; 0000\)</span> represents <span class="arithmatex">\(-128\)</span>. In fact, the result of calculating <span class="arithmatex">\((-1) + (-127)\)</span> in 2's complement is <span class="arithmatex">\(-128\)</span>.</p>
<div class="arithmatex">\[
\begin{aligned}
&amp; (-127) + (-1) \newline
&amp; \rightarrow 1111 \; 1111 \; \text{(Sign-magnitude)} + 1000 \; 0001 \; \text{(Sign-magnitude)} \newline
&amp; = 1000 \; 0000 \; \text{(One's complement)} + 1111 \; 1110 \; \text{(One's complement)} \newline
&amp; = 1000 \; 0001 \; \text{(Two's complement)} + 1111 \; 1111 \; \text{(Two's complement)} \newline
&amp; = 1000 \; 0000 \; \text{(Two's complement)} \newline
&amp; = 1000 \; 0000 \; \text{(1's complement)} + 1111 \; 1110 \; \text{(1's complement)} \newline
&amp; = 1000 \; 0001 \; \text{(2's complement)} + 1111 \; 1111 \; \text{(2's complement)} \newline
&amp; = 1000 \; 0000 \; \text{(2's complement)} \newline
&amp; \rightarrow -128
\end{aligned}
\]</div>
<p>As you might have noticed, all these calculations are additions, hinting at an important fact: <strong>computers' internal hardware circuits are primarily designed around addition operations</strong>. This is because addition is simpler to implement in hardware compared to other operations like multiplication, division, and subtraction, allowing for easier parallelization and faster computation.</p>
<p>It's important to note that this doesn't mean computers can only perform addition. <strong>By combining addition with basic logical operations, computers can execute a variety of other mathematical operations</strong>. For example, the subtraction <span class="arithmatex">\(a - b\)</span> can be translated into <span class="arithmatex">\(a + (-b)\)</span>; multiplication and division can be translated into multiple additions or subtractions.</p>
<p>We can now summarize the reason for using two's complement in computers: with two's complement representation, computers can use the same circuits and operations to handle both positive and negative number addition, eliminating the need for special hardware circuits for subtraction and avoiding the ambiguity of positive and negative zero. This greatly simplifies hardware design and enhances computational efficiency.</p>
<p>The design of two's complement is quite ingenious, and due to space constraints, we'll stop here. Interested readers are encouraged to explore further.</p>
<h2 id="332-floating-point-number-encoding">3.3.2 &nbsp; Floating-point number encoding<a class="headerlink" href="#332-floating-point-number-encoding" title="Permanent link">&para;</a></h2>
<p>You might have noticed something intriguing: despite having the same length of 4 bytes, why does a <code>float</code> have a much larger range of values compared to an <code>int</code>? This seems counterintuitive, as one would expect the range to shrink for <code>float</code> since it needs to represent fractions.</p>
<p>In fact, <strong>this is due to the different representation method used by floating-point numbers (<code>float</code>)</strong>. Let's consider a 32-bit binary number as:</p>
<p>You may have noticed that all the above calculations are addition operations. This hints at an important fact: <strong>the hardware circuits inside computers are mainly designed based on addition operations</strong>. This is because addition operations are simpler to implement in hardware compared to other operations (such as multiplication, division, and subtraction), easier to parallelize, and have faster operation speeds.</p>
<p>Please note that this does not mean that computers can only perform addition. <strong>By combining addition with some basic logical operations, computers can implement various other mathematical operations</strong>. For example, calculating the subtraction <span class="arithmatex">\(a - b\)</span> can be converted to calculating the addition <span class="arithmatex">\(a + (-b)\)</span>; calculating multiplication and division can be converted to calculating multiple additions or subtractions.</p>
<p>Now we can summarize the reasons why computers use 2's complement: based on 2's complement representation, computers can use the same circuits and operations to handle the addition of positive and negative numbers, without the need to design special hardware circuits to handle subtraction, and without the need to specially handle the ambiguity problem of positive and negative zero. This greatly simplifies hardware design and improves operational efficiency.</p>
<p>The design of 2's complement is very ingenious. Due to space limitations, we will stop here. Interested readers are encouraged to explore further.</p>
<h2 id="332-floating-point-number-encoding">3.3.2 &nbsp; Floating-Point Number Encoding<a class="headerlink" href="#332-floating-point-number-encoding" title="Permanent link">&para;</a></h2>
<p>Careful readers may have noticed: <code>int</code> and <code>float</code> have the same length, both are 4 bytes, but why does <code>float</code> have a much larger range than <code>int</code>? This is very counterintuitive because it stands to reason that <code>float</code> needs to represent decimals, so the range should be smaller.</p>
<p>In fact, <strong>this is because floating-point number <code>float</code> uses a different representation method</strong>. Let's denote a 32-bit binary number as:</p>
<div class="arithmatex">\[
b_{31} b_{30} b_{29} \ldots b_2 b_1 b_0
\]</div>
<p>According to the IEEE 754 standard, a 32-bit <code>float</code> consists of the following three parts:</p>
<p>According to the IEEE 754 standard, a 32-bit <code>float</code> consists of the following three parts.</p>
<ul>
<li>Sign bit <span class="arithmatex">\(\mathrm{S}\)</span>: Occupies 1 bit, corresponding to <span class="arithmatex">\(b_{31}\)</span>.</li>
<li>Exponent bit <span class="arithmatex">\(\mathrm{E}\)</span>: Occupies 8 bits, corresponding to <span class="arithmatex">\(b_{30} b_{29} \ldots b_{23}\)</span>.</li>
<li>Fraction bit <span class="arithmatex">\(\mathrm{N}\)</span>: Occupies 23 bits, corresponding to <span class="arithmatex">\(b_{22} b_{21} \ldots b_0\)</span>.</li>
<li>Sign bit <span class="arithmatex">\(\mathrm{S}\)</span>: occupies 1 bit, corresponding to <span class="arithmatex">\(b_{31}\)</span>.</li>
<li>Exponent bit <span class="arithmatex">\(\mathrm{E}\)</span>: occupies 8 bits, corresponding to <span class="arithmatex">\(b_{30} b_{29} \ldots b_{23}\)</span>.</li>
<li>Fraction bit <span class="arithmatex">\(\mathrm{N}\)</span>: occupies 23 bits, corresponding to <span class="arithmatex">\(b_{22} b_{21} \ldots b_0\)</span>.</li>
</ul>
<p>The value of a binary <code>float</code> number is calculated as:</p>
<p>The calculation method for the value corresponding to the binary <code>float</code> is:</p>
<div class="arithmatex">\[
\text{val} = (-1)^{b_{31}} \times 2^{\left(b_{30} b_{29} \ldots b_{23}\right)_2 - 127} \times \left(1 . b_{22} b_{21} \ldots b_0\right)_2
\text {val} = (-1)^{b_{31}} \times 2^{\left(b_{30} b_{29} \ldots b_{23}\right)_2-127} \times\left(1 . b_{22} b_{21} \ldots b_0\right)_2
\]</div>
<p>Converted to a decimal formula, this becomes:</p>
<p>Converted to decimal, the calculation formula is:</p>
<div class="arithmatex">\[
\text{val} = (-1)^{\mathrm{S}} \times 2^{\mathrm{E} - 127} \times (1 + \mathrm{N})
\text {val}=(-1)^{\mathrm{S}} \times 2^{\mathrm{E} -127} \times (1 + \mathrm{N})
\]</div>
<p>The range of each component is:</p>
<div class="arithmatex">\[
\begin{aligned}
\mathrm{S} \in &amp; \{ 0, 1\}, \quad \mathrm{E} \in \{ 1, 2, \dots, 254 \} \newline
(1 + \mathrm{N}) = &amp; (1 + \sum_{i=1}^{23} b_{23-i} \times 2^{-i}) \subset [1, 2 - 2^{-23}]
(1 + \mathrm{N}) = &amp; (1 + \sum_{i=1}^{23} b_{23-i} 2^{-i}) \subset [1, 2 - 2^{-23}]
\end{aligned}
\]</div>
<p><a class="glightbox" href="../number_encoding.assets/ieee_754_float.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Example calculation of a float in IEEE 754 standard" class="animation-figure" src="../number_encoding.assets/ieee_754_float.png" /></a></p>
<p align="center"> Figure 3-5 &nbsp; Example calculation of a float in IEEE 754 standard </p>
<p><a class="glightbox" href="../number_encoding.assets/ieee_754_float.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Calculation example of float under IEEE 754 standard" class="animation-figure" src="../number_encoding.assets/ieee_754_float.png" /></a></p>
<p align="center"> Figure 3-5 &nbsp; Calculation example of float under IEEE 754 standard </p>
<p>Observing Figure 3-5, given an example data <span class="arithmatex">\(\mathrm{S} = 0\)</span>, <span class="arithmatex">\(\mathrm{E} = 124\)</span>, <span class="arithmatex">\(\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\)</span>, we have:</p>
<p>Observing Figure 3-5, given example data <span class="arithmatex">\(\mathrm{S} = 0\)</span>, <span class="arithmatex">\(\mathrm{E} = 124\)</span>, <span class="arithmatex">\(\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\)</span>, we have:</p>
<div class="arithmatex">\[
\text{val} = (-1)^0 \times 2^{124 - 127} \times (1 + 0.375) = 0.171875
\text { val } = (-1)^0 \times 2^{124 - 127} \times (1 + 0.375) = 0.171875
\]</div>
<p>Now we can answer the initial question: <strong>The representation of <code>float</code> includes an exponent bit, leading to a much larger range than <code>int</code></strong>. Based on the above calculation, the maximum positive number representable by <code>float</code> is approximately <span class="arithmatex">\(2^{254 - 127} \times (2 - 2^{-23}) \approx 3.4 \times 10^{38}\)</span>, and the minimum negative number is obtained by switching the sign bit.</p>
<p><strong>However, the trade-off for <code>float</code>'s expanded range is a sacrifice in precision</strong>. The integer type <code>int</code> uses all 32 bits to represent the number, with values evenly distributed; but due to the exponent bit, the larger the value of a <code>float</code>, the greater the difference between adjacent numbers.</p>
<p>Now we can answer the initial question: <strong>the representation of <code>float</code> includes an exponent bit, resulting in a range far greater than <code>int</code></strong>. According to the above calculation, the maximum positive number that <code>float</code> can represent is <span class="arithmatex">\(2^{254 - 127} \times (2 - 2^{-23}) \approx 3.4 \times 10^{38}\)</span>, and the minimum negative number can be obtained by switching the sign bit.</p>
<p><strong>Although floating-point number <code>float</code> expands the range, its side effect is sacrificing precision</strong>. The integer type <code>int</code> uses all 32 bits to represent numbers, and the numbers are evenly distributed; however, due to the existence of the exponent bit, the larger the value of floating-point number <code>float</code>, the larger the difference between two adjacent numbers tends to be.</p>
<p>As shown in Table 3-2, exponent bits <span class="arithmatex">\(\mathrm{E} = 0\)</span> and <span class="arithmatex">\(\mathrm{E} = 255\)</span> have special meanings, <strong>used to represent zero, infinity, <span class="arithmatex">\(\mathrm{NaN}\)</span>, etc.</strong></p>
<p align="center"> Table 3-2 &nbsp; Meaning of exponent bits </p>
@@ -4504,13 +4504,13 @@ b_{31} b_{30} b_{29} \ldots b_2 b_1 b_0
<tr>
<td><span class="arithmatex">\(0\)</span></td>
<td><span class="arithmatex">\(\pm 0\)</span></td>
<td>Subnormal Numbers</td>
<td>Subnormal Number</td>
<td><span class="arithmatex">\((-1)^{\mathrm{S}} \times 2^{-126} \times (0.\mathrm{N})\)</span></td>
</tr>
<tr>
<td><span class="arithmatex">\(1, 2, \dots, 254\)</span></td>
<td>Normal Numbers</td>
<td>Normal Numbers</td>
<td>Normal Number</td>
<td>Normal Number</td>
<td><span class="arithmatex">\((-1)^{\mathrm{S}} \times 2^{(\mathrm{E} -127)} \times (1.\mathrm{N})\)</span></td>
</tr>
<tr>
@@ -4522,8 +4522,8 @@ b_{31} b_{30} b_{29} \ldots b_2 b_1 b_0
</tbody>
</table>
</div>
<p>It's worth noting that subnormal numbers significantly improve the precision of floating-point numbers. The smallest positive normal number is <span class="arithmatex">\(2^{-126}\)</span>, and the smallest positive subnormal number is <span class="arithmatex">\(2^{-126} \times 2^{-23}\)</span>.</p>
<p>Double-precision <code>double</code> also uses a similar representation method to <code>float</code>, which is not elaborated here for brevity.</p>
<p>It is worth noting that subnormal numbers significantly improve the precision of floating-point numbers. The smallest positive normal number is <span class="arithmatex">\(2^{-126}\)</span>, and the smallest positive subnormal number is <span class="arithmatex">\(2^{-126} \times 2^{-23}\)</span>.</p>
<p>Double-precision <code>double</code> also uses a representation method similar to <code>float</code>, which will not be elaborated here.</p>
<!-- Source file information -->
@@ -4546,7 +4546,7 @@ aria-label="Footer"
<a
href="../basic_data_types/"
class="md-footer__link md-footer__link--prev"
aria-label="Previous: 3.2 Basic data types"
aria-label="Previous: 3.2 Basic Data Types"
rel="prev"
>
<div class="md-footer__button md-icon">
@@ -4558,7 +4558,7 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
3.2 Basic data types
3.2 Basic Data Types
</div>
</div>
</a>
@@ -4570,7 +4570,7 @@ aria-label="Footer"
<a
href="../character_encoding/"
class="md-footer__link md-footer__link--next"
aria-label="Next: 3.4 Character encoding *"
aria-label="Next: 3.4 Character Encoding *"
rel="next"
>
<div class="md-footer__title">
@@ -4578,7 +4578,7 @@ aria-label="Footer"
Next
</span>
<div class="md-ellipsis">
3.4 Character encoding *
3.4 Character Encoding *
</div>
</div>
<div class="md-footer__button md-icon">
@@ -4671,7 +4671,7 @@ aria-label="Footer"
<nav class="md-footer__inner md-grid" aria-label="Footer" >
<a href="../basic_data_types/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 3.2 Basic data types">
<a href="../basic_data_types/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 3.2 Basic Data Types">
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11z"/></svg>
@@ -4681,20 +4681,20 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
3.2 Basic data types
3.2 Basic Data Types
</div>
</div>
</a>
<a href="../character_encoding/" class="md-footer__link md-footer__link--next" aria-label="Next: 3.4 Character encoding *">
<a href="../character_encoding/" class="md-footer__link md-footer__link--next" aria-label="Next: 3.4 Character Encoding *">
<div class="md-footer__title">
<span class="md-footer__direction">
Next
</span>
<div class="md-ellipsis">
3.4 Character encoding *
3.4 Character Encoding *
</div>
</div>
<div class="md-footer__button md-icon">

View File

@@ -58,8 +58,8 @@
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
<style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:300,300i,400,400i,700,700i%7CJetBrains+Mono:400,400i,700,700i&display=fallback">
<style>:root{--md-text-font:"Lato";--md-code-font:"JetBrains Mono"}</style>
@@ -371,7 +371,7 @@
<span class="md-ellipsis">
Before starting
Before Starting
@@ -388,7 +388,7 @@
<span class="md-nav__icon md-icon"></span>
Before starting
Before Starting
</label>
@@ -487,7 +487,7 @@
<span class="md-ellipsis">
0.1 About this book
0.1 About This Book
@@ -515,7 +515,7 @@
<span class="md-ellipsis">
0.2 How to read
0.2 How to Use This Book
@@ -604,7 +604,7 @@
<span class="md-ellipsis">
Chapter 1. Encounter with algorithms
Chapter 1. Encounter With Algorithms
@@ -626,7 +626,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 1. Encounter with algorithms
Chapter 1. Encounter With Algorithms
</label>
@@ -648,7 +648,7 @@
<span class="md-ellipsis">
1.1 Algorithms are everywhere
1.1 Algorithms Are Everywhere
@@ -676,7 +676,7 @@
<span class="md-ellipsis">
1.2 What is an algorithm
1.2 What Is an Algorithm
@@ -769,7 +769,7 @@
<span class="md-ellipsis">
Chapter 2. Complexity analysis
Chapter 2. Complexity Analysis
@@ -791,7 +791,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 2. Complexity analysis
Chapter 2. Complexity Analysis
</label>
@@ -813,7 +813,7 @@
<span class="md-ellipsis">
2.1 Algorithm efficiency assessment
2.1 Algorithm Efficiency Evaluation
@@ -841,7 +841,7 @@
<span class="md-ellipsis">
2.2 Iteration and recursion
2.2 Iteration and Recursion
@@ -869,7 +869,7 @@
<span class="md-ellipsis">
2.3 Time complexity
2.3 Time Complexity
@@ -897,7 +897,7 @@
<span class="md-ellipsis">
2.4 Space complexity
2.4 Space Complexity
@@ -992,7 +992,7 @@
<span class="md-ellipsis">
Chapter 3. Data structures
Chapter 3. Data Structures
@@ -1014,7 +1014,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 3. Data structures
Chapter 3. Data Structures
</label>
@@ -1036,7 +1036,7 @@
<span class="md-ellipsis">
3.1 Classification of data structures
3.1 Classification of Data Structures
@@ -1064,7 +1064,7 @@
<span class="md-ellipsis">
3.2 Basic data types
3.2 Basic Data Types
@@ -1092,7 +1092,7 @@
<span class="md-ellipsis">
3.3 Number encoding *
3.3 Number Encoding *
@@ -1120,7 +1120,7 @@
<span class="md-ellipsis">
3.4 Character encoding *
3.4 Character Encoding *
@@ -1204,7 +1204,7 @@
<a href="#1-key-review" class="md-nav__link">
<span class="md-ellipsis">
1. &nbsp; Key review
1. &nbsp; Key Review
</span>
</a>
@@ -1282,7 +1282,7 @@
<span class="md-ellipsis">
Chapter 4. Array and linked list
Chapter 4. Array and Linked List
@@ -1304,7 +1304,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 4. Array and linked list
Chapter 4. Array and Linked List
</label>
@@ -1354,7 +1354,7 @@
<span class="md-ellipsis">
4.2 Linked list
4.2 Linked List
@@ -1410,7 +1410,7 @@
<span class="md-ellipsis">
4.4 Memory and cache *
4.4 Memory and Cache *
@@ -1501,7 +1501,7 @@
<span class="md-ellipsis">
Chapter 5. Stack and queue
Chapter 5. Stack and Queue
@@ -1523,7 +1523,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 5. Stack and queue
Chapter 5. Stack and Queue
</label>
@@ -1601,7 +1601,7 @@
<span class="md-ellipsis">
5.3 Double-ended queue
5.3 Double-Ended Queue
@@ -1692,7 +1692,7 @@
<span class="md-ellipsis">
Chapter 6. Hash table
Chapter 6. Hashing
@@ -1714,7 +1714,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 6. Hash table
Chapter 6. Hashing
</label>
@@ -1736,7 +1736,7 @@
<span class="md-ellipsis">
6.1 Hash table
6.1 Hash Table
@@ -1764,7 +1764,7 @@
<span class="md-ellipsis">
6.2 Hash collision
6.2 Hash Collision
@@ -1792,7 +1792,7 @@
<span class="md-ellipsis">
6.3 Hash algorithm
6.3 Hash Algorithm
@@ -1931,7 +1931,7 @@
<span class="md-ellipsis">
7.1 Binary tree
7.1 Binary Tree
@@ -1959,7 +1959,7 @@
<span class="md-ellipsis">
7.2 Binary tree traversal
7.2 Binary Tree Traversal
@@ -1987,7 +1987,7 @@
<span class="md-ellipsis">
7.3 Array Representation of tree
7.3 Array Representation of Tree
@@ -2015,7 +2015,7 @@
<span class="md-ellipsis">
7.4 Binary Search tree
7.4 Binary Search Tree
@@ -2043,7 +2043,7 @@
<span class="md-ellipsis">
7.5 AVL tree *
7.5 AVL Tree *
@@ -2206,7 +2206,7 @@
<span class="md-ellipsis">
8.2 Building a heap
8.2 Building a Heap
@@ -2234,7 +2234,7 @@
<span class="md-ellipsis">
8.3 Top-k problem
8.3 Top-K Problem
@@ -2397,7 +2397,7 @@
<span class="md-ellipsis">
9.2 Basic graph operations
9.2 Basic Operations on Graphs
@@ -2425,7 +2425,7 @@
<span class="md-ellipsis">
9.3 Graph traversal
9.3 Graph Traversal
@@ -2564,7 +2564,7 @@
<span class="md-ellipsis">
10.1 Binary search
10.1 Binary Search
@@ -2592,7 +2592,7 @@
<span class="md-ellipsis">
10.2 Binary search insertion
10.2 Binary Search Insertion
@@ -2620,7 +2620,7 @@
<span class="md-ellipsis">
10.3 Binary search boundaries
10.3 Binary Search Edge Cases
@@ -2648,7 +2648,7 @@
<span class="md-ellipsis">
10.4 Hashing optimization strategies
10.4 Hash Optimization Strategy
@@ -2676,7 +2676,7 @@
<span class="md-ellipsis">
10.5 Search algorithms revisited
10.5 Search Algorithms Revisited
@@ -2825,7 +2825,7 @@
<span class="md-ellipsis">
11.1 Sorting algorithms
11.1 Sorting Algorithms
@@ -2853,7 +2853,7 @@
<span class="md-ellipsis">
11.2 Selection sort
11.2 Selection Sort
@@ -2881,7 +2881,7 @@
<span class="md-ellipsis">
11.3 Bubble sort
11.3 Bubble Sort
@@ -2909,7 +2909,7 @@
<span class="md-ellipsis">
11.4 Insertion sort
11.4 Insertion Sort
@@ -2937,7 +2937,7 @@
<span class="md-ellipsis">
11.5 Quick sort
11.5 Quick Sort
@@ -2965,7 +2965,7 @@
<span class="md-ellipsis">
11.6 Merge sort
11.6 Merge Sort
@@ -2993,7 +2993,7 @@
<span class="md-ellipsis">
11.7 Heap sort
11.7 Heap Sort
@@ -3021,7 +3021,7 @@
<span class="md-ellipsis">
11.8 Bucket sort
11.8 Bucket Sort
@@ -3049,7 +3049,7 @@
<span class="md-ellipsis">
11.9 Counting sort
11.9 Counting Sort
@@ -3077,7 +3077,7 @@
<span class="md-ellipsis">
11.10 Radix sort
11.10 Radix Sort
@@ -3170,7 +3170,7 @@
<span class="md-ellipsis">
Chapter 12. Divide and conquer
Chapter 12. Divide and Conquer
@@ -3192,7 +3192,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 12. Divide and conquer
Chapter 12. Divide and Conquer
</label>
@@ -3214,7 +3214,7 @@
<span class="md-ellipsis">
12.1 Divide and conquer algorithms
12.1 Divide and Conquer Algorithms
@@ -3242,7 +3242,7 @@
<span class="md-ellipsis">
12.2 Divide and conquer search strategy
12.2 Divide and Conquer Search Strategy
@@ -3270,7 +3270,7 @@
<span class="md-ellipsis">
12.3 Building binary tree problem
12.3 Building a Binary Tree Problem
@@ -3298,7 +3298,7 @@
<span class="md-ellipsis">
12.4 Tower of Hanoi Problem
12.4 Hanoi Tower Problem
@@ -3435,7 +3435,7 @@
<span class="md-ellipsis">
13.1 Backtracking algorithms
13.1 Backtracking Algorithm
@@ -3463,7 +3463,7 @@
<span class="md-ellipsis">
13.2 Permutation problem
13.2 Permutations Problem
@@ -3491,7 +3491,7 @@
<span class="md-ellipsis">
13.3 Subset sum problem
13.3 Subset-Sum Problem
@@ -3519,7 +3519,7 @@
<span class="md-ellipsis">
13.4 n queens problem
13.4 N-Queens Problem
@@ -3616,7 +3616,7 @@
<span class="md-ellipsis">
Chapter 14. Dynamic programming
Chapter 14. Dynamic Programming
@@ -3638,7 +3638,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 14. Dynamic programming
Chapter 14. Dynamic Programming
</label>
@@ -3660,7 +3660,7 @@
<span class="md-ellipsis">
14.1 Introduction to dynamic programming
14.1 Introduction to Dynamic Programming
@@ -3688,7 +3688,7 @@
<span class="md-ellipsis">
14.2 Characteristics of DP problems
14.2 Characteristics of Dynamic Programming Problems
@@ -3716,7 +3716,7 @@
<span class="md-ellipsis">
14.3 DP problem-solving approach
14.3 Dynamic Programming Problem-Solving Approach
@@ -3744,7 +3744,7 @@
<span class="md-ellipsis">
14.4 0-1 Knapsack problem
14.4 0-1 Knapsack Problem
@@ -3772,7 +3772,7 @@
<span class="md-ellipsis">
14.5 Unbounded knapsack problem
14.5 Unbounded Knapsack Problem
@@ -3800,7 +3800,7 @@
<span class="md-ellipsis">
14.6 Edit distance problem
14.6 Edit Distance Problem
@@ -3937,7 +3937,7 @@
<span class="md-ellipsis">
15.1 Greedy algorithms
15.1 Greedy Algorithm
@@ -3965,7 +3965,7 @@
<span class="md-ellipsis">
15.2 Fractional knapsack problem
15.2 Fractional Knapsack Problem
@@ -3993,7 +3993,7 @@
<span class="md-ellipsis">
15.3 Maximum capacity problem
15.3 Maximum Capacity Problem
@@ -4021,7 +4021,7 @@
<span class="md-ellipsis">
15.4 Maximum product cutting problem
15.4 Maximum Product Cutting Problem
@@ -4154,7 +4154,7 @@
<span class="md-ellipsis">
16.1 Installation
16.1 Programming Environment Installation
@@ -4182,7 +4182,7 @@
<span class="md-ellipsis">
16.2 Contributing
16.2 Contributing Together
@@ -4210,7 +4210,7 @@
<span class="md-ellipsis">
16.3 Terminology
16.3 Terminology Table
@@ -4327,7 +4327,7 @@
<a href="#1-key-review" class="md-nav__link">
<span class="md-ellipsis">
1. &nbsp; Key review
1. &nbsp; Key Review
</span>
</a>
@@ -4383,49 +4383,48 @@
<!-- Page content -->
<h1 id="35-summary">3.5 &nbsp; Summary<a class="headerlink" href="#35-summary" title="Permanent link">&para;</a></h1>
<h3 id="1-key-review">1. &nbsp; Key review<a class="headerlink" href="#1-key-review" title="Permanent link">&para;</a></h3>
<h3 id="1-key-review">1. &nbsp; Key Review<a class="headerlink" href="#1-key-review" title="Permanent link">&para;</a></h3>
<ul>
<li>Data structures can be categorized from two perspectives: logical structure and physical structure. Logical structure describes the logical relationships between data, while physical structure describes how data is stored in memory.</li>
<li>Frequently used logical structures include linear structures, trees, and networks. We usually divide data structures into linear (arrays, linked lists, stacks, queues) and non-linear (trees, graphs, heaps) based on their logical structure. The implementation of hash tables may involve both linear and non-linear data structures.</li>
<li>When a program is running, data is stored in memory. Each memory space has a corresponding address, and the program accesses data through these addresses.</li>
<li>Physical structures can be divided into continuous space storage (arrays) and discrete space storage (linked lists). All data structures are implemented using arrays, linked lists, or a combination of both.</li>
<li>The basic data types in computers include integers (<code>byte</code>, <code>short</code>, <code>int</code>, <code>long</code>), floating-point numbers (<code>float</code>, <code>double</code>), characters (<code>char</code>), and booleans (<code>bool</code>). The value range of a data type depends on its size and representation.</li>
<li>Sign-magnitude, 1's complement, 2's complement are three methods of encoding integers in computers, and they can be converted into each other. The most significant bit of the sign-magnitude is the sign bit, and the remaining bits represent the value of the number.</li>
<li>Integers are encoded by 2's complement in computers. The benefits of this representation include (i) the computer can unify the addition of positive and negative integers, (ii) no need to design special hardware circuits for subtraction, and (iii) no ambiguity of positive and negative zero.</li>
<li>The encoding of floating-point numbers consists of 1 sign bit, 8 exponent bits, and 23 fraction bits. Due to the exponent bit, the range of floating-point numbers is much greater than that of integers, but at the cost of precision.</li>
<li>ASCII is the earliest English character set, with 1 byte in length and a total of 127 characters. GBK is a popular Chinese character set, which includes more than 20,000 Chinese characters. Unicode aims to provide a complete character set standard that includes characters from various languages in the world, thus solving the garbled character problem caused by inconsistent character encoding methods.</li>
<li>UTF-8 is the most popular and general Unicode encoding method. It is a variable-length encoding method with good scalability and space efficiency. UTF-16 and UTF-32 are fixed-length encoding methods. When encoding Chinese characters, UTF-16 takes up less space than UTF-8. Programming languages like Java and C# use UTF-16 encoding by default.</li>
<li>Data structures can be classified from two perspectives: logical structure and physical structure. Logical structure describes the logical relationships between data elements, while physical structure describes how data is stored in computer memory.</li>
<li>Common logical structures include linear, tree, and network structures. We typically classify data structures as linear (arrays, linked lists, stacks, queues) and non-linear (trees, graphs, heaps) based on their logical structure. The implementation of hash tables may involve both linear and non-linear data structures.</li>
<li>When a program runs, data is stored in computer memory. Each memory space has a corresponding memory address, and the program accesses data through these memory addresses.</li>
<li>Physical structures are primarily divided into contiguous space storage (arrays) and dispersed space storage (linked lists). All data structures are implemented using arrays, linked lists, or a combination of both.</li>
<li>Basic data types in computers include integers <code>byte</code>, <code>short</code>, <code>int</code>, <code>long</code>, floating-point numbers <code>float</code>, <code>double</code>, characters <code>char</code>, and booleans <code>bool</code>. Their value ranges depend on the size of space they occupy and their representation method.</li>
<li>Sign-magnitude, 1's complement, and 2's complement are three methods for encoding numbers in computers, and they can be converted into each other. The most significant bit of sign-magnitude is the sign bit, and the remaining bits represent the value of the number.</li>
<li>Integers are stored in computers in 2's complement form. Under 2's complement representation, computers can treat the addition of positive and negative numbers uniformly, without needing to design special hardware circuits for subtraction, and there is no ambiguity of positive and negative zero.</li>
<li>The encoding of floating-point numbers consists of 1 sign bit, 8 exponent bits, and 23 fraction bits. Due to the exponent bits, the range of floating-point numbers is much larger than that of integers, at the cost of sacrificing precision.</li>
<li>ASCII is the earliest English character set, with a length of 1 byte, containing a total of 127 characters. GBK is a commonly used Chinese character set, containing over 20,000 Chinese characters. Unicode is committed to providing a complete character set standard, collecting characters from various languages around the world, thereby solving the garbled text problem caused by inconsistent character encoding methods.</li>
<li>UTF-8 is the most popular Unicode encoding method, with excellent universality. It is a variable-length encoding method with good scalability, effectively improving storage space efficiency. UTF-16 and UTF-32 are fixed-length encoding methods. When encoding Chinese characters, UTF-16 occupies less space than UTF-8. Programming languages such as Java and C# use UTF-16 encoding by default.</li>
</ul>
<h3 id="2-q-a">2. &nbsp; Q &amp; A<a class="headerlink" href="#2-q-a" title="Permanent link">&para;</a></h3>
<p><strong>Q</strong>: Why does a hash table contain both linear and non-linear data structures?</p>
<p>The underlying structure of a hash table is an array. To resolve hash collisions, we may use "chaining" (discussed in a later section, "Hash collision"): each bucket in the array points to a linked list, which may transform into a tree (usually a red-black tree) when its length is larger than a certain threshold.
From a storage perspective, the underlying structure of a hash table is an array, where each bucket might contain a value, a linked list, or a tree. Therefore, hash tables may contain both linear data structures (arrays, linked lists) and non-linear data structures (trees).</p>
<p><strong>Q</strong>: Why do hash tables contain both linear and non-linear data structures?</p>
<p>The underlying structure of a hash table is an array. To resolve hash collisions, we may use "chaining" (discussed in the subsequent "Hash Collision" section): each bucket in the array points to a linked list, which may be converted to a tree (usually a red-black tree) when the list length exceeds a certain threshold.</p>
<p>From a storage perspective, the underlying structure of a hash table is an array, where each bucket slot may contain a value, a linked list, or a tree. Therefore, hash tables may contain both linear data structures (arrays, linked lists) and non-linear data structures (trees).</p>
<p><strong>Q</strong>: Is the length of the <code>char</code> type 1 byte?</p>
<p>The length of the <code>char</code> type is determined by the encoding method of the programming language. For example, Java, JavaScript, TypeScript, and C# all use UTF-16 encoding (to save Unicode code points), so the length of the <code>char</code> type is 2 bytes.</p>
<p><strong>Q</strong>: Is there any ambiguity when we refer to array-based data structures as "static data structures"? The stack can also perform "dynamic" operations such as popping and pushing.</p>
<p>The stack can implement dynamic data operations, but the data structure is still "static" (the length is fixed). Although array-based data structures can dynamically add or remove elements, their capacity is fixed. If the stack size exceeds the pre-allocated size, then the old array will be copied into a newly created and larger array.</p>
<p><strong>Q</strong>: When building a stack (queue), its size is not specified, so why are they "static data structures"?</p>
<p>In high-level programming languages, we do not need to manually specify the initial capacity of stacks (queues); this task is automatically completed within the class. For example, the initial capacity of Java's <code>ArrayList</code> is usually 10. Furthermore, the expansion operation is also completed automatically. See the subsequent "List" chapter for details.</p>
<p><strong>Q</strong>The method of converting the sign-magnitude to the 2's complement is "first negate and then add 1", so converting the 2's complement to the sign-magnitude should be its inverse operation "first subtract 1 and then negate".
However, the 2's complement can also be converted to the sign-magnitude through "first negate and then add 1", why is this?</p>
<p><strong>A</strong>This is because the mutual conversion between the sign-magnitude and the 2's complement is equivalent to computing the "complement". We first define the complement: assuming <span class="arithmatex">\(a + b = c\)</span>, then we say that <span class="arithmatex">\(a\)</span> is the complement of <span class="arithmatex">\(b\)</span> to <span class="arithmatex">\(c\)</span>, and vice versa, <span class="arithmatex">\(b\)</span> is the complement of <span class="arithmatex">\(a\)</span> to <span class="arithmatex">\(c\)</span>.</p>
<p>Given a binary number <span class="arithmatex">\(0010\)</span> with length <span class="arithmatex">\(n = 4\)</span>, if this number is the sign-magnitude (ignoring the sign bit), then its 2's complement can be obtained by "first negating and then adding 1":</p>
<p>The length of the <code>char</code> type is determined by the encoding method used by the programming language. For example, Java, JavaScript, TypeScript, and C# all use UTF-16 encoding (to store Unicode code points), so the <code>char</code> type has a length of 2 bytes.</p>
<p><strong>Q</strong>: Is there ambiguity in referring to array-based data structures as "static data structures"? Stacks can also perform "dynamic" operations such as push and pop.</p>
<p>Stacks can indeed implement dynamic data operations, but the data structure is still "static" (fixed length). Although array-based data structures can dynamically add or remove elements, their capacity is fixed. If the data volume exceeds the pre-allocated size, a new larger array needs to be created, and the contents of the old array must be copied to the new array.</p>
<p><strong>Q</strong>: When constructing a stack (queue), its size is not specified. Why are they "static data structures"?</p>
<p>In high-level programming languages, we do not need to manually specify the initial capacity of a stack (queue); this work is automatically completed within the class. For example, the initial capacity of Java's <code>ArrayList</code> is typically 10. Additionally, the expansion operation is also automatically implemented. See the subsequent "List" section for details.</p>
<p><strong>Q</strong>: The method of converting sign-magnitude to 2's complement is "first negate then add 1". So converting 2's complement to sign-magnitude should be the inverse operation "first subtract 1 then negate". However, 2's complement can also be converted to sign-magnitude through "first negate then add 1". Why is this?</p>
<p>This is because the mutual conversion between sign-magnitude and 2's complement is actually the process of computing the "complement". Let us first define the complement: assuming <span class="arithmatex">\(a + b = c\)</span>, then we say that <span class="arithmatex">\(a\)</span> is the complement of <span class="arithmatex">\(b\)</span> to <span class="arithmatex">\(c\)</span>, and conversely, <span class="arithmatex">\(b\)</span> is the complement of <span class="arithmatex">\(a\)</span> to <span class="arithmatex">\(c\)</span>.</p>
<p>Given an <span class="arithmatex">\(n = 4\)</span> bit binary number <span class="arithmatex">\(0010\)</span>, if we treat this number as sign-magnitude (ignoring the sign bit), then its 2's complement can be obtained through "first negate then add 1":</p>
<div class="arithmatex">\[
0010 \rightarrow 1101 \rightarrow 1110
\]</div>
<p>Observe that the sum of the sign-magnitude and the 2's complement is <span class="arithmatex">\(0010 + 1110 = 10000\)</span>, i.e., the 2's complement <span class="arithmatex">\(1110\)</span> is the "complement" of the sign-magnitude <span class="arithmatex">\(0010\)</span> to <span class="arithmatex">\(10000\)</span>. <strong>This means that the above "first negate and then add 1" is equivalent to computing the complement to <span class="arithmatex">\(10000\)</span></strong>.</p>
<p>So, what is the "complement" of <span class="arithmatex">\(1110\)</span> to <span class="arithmatex">\(10000\)</span>? We can still compute it by "negating first and then adding 1":</p>
<p>We find that the sum of sign-magnitude and 2's complement is <span class="arithmatex">\(0010 + 1110 = 10000\)</span>, which means the 2's complement <span class="arithmatex">\(1110\)</span> is the "complement" of sign-magnitude <span class="arithmatex">\(0010\)</span> to <span class="arithmatex">\(10000\)</span>. <strong>This means the above "first negate then add 1" is actually the process of computing the complement to <span class="arithmatex">\(10000\)</span></strong>.</p>
<p>So, what is the "complement" of 2's complement <span class="arithmatex">\(1110\)</span> to <span class="arithmatex">\(10000\)</span>? We can still use "first negate then add 1" to obtain it:</p>
<div class="arithmatex">\[
1110 \rightarrow 0001 \rightarrow 0010
\]</div>
<p>In other words, the sign-magnitude and the 2's complement are each other's "complement" to <span class="arithmatex">\(10000\)</span>, so "sign-magnitude to 2's complement" and "2's complement to sign-magnitude" can be implemented with the same operation (first negate and then add 1).</p>
<p>Of course, we can also use the inverse operation of "first negate and then add 1" to find the sign-magnitude of the 2's complement <span class="arithmatex">\(1110\)</span>, that is, "first subtract 1 and then negate":</p>
<p>In other words, sign-magnitude and 2's complement are each other's "complement" to <span class="arithmatex">\(10000\)</span>, so "sign-magnitude to 2's complement" and "2's complement to sign-magnitude" can be implemented using the same operation (first negate then add 1).</p>
<p>Of course, we can also use the inverse operation to find the sign-magnitude of 2's complement <span class="arithmatex">\(1110\)</span>, that is, "first subtract 1 then negate":</p>
<div class="arithmatex">\[
1110 \rightarrow 1101 \rightarrow 0010
\]</div>
<p>To sum up, "first negate and then add 1" and "first subtract 1 and then negate" are both computing the complement to <span class="arithmatex">\(10000\)</span>, and they are equivalent.</p>
<p>Essentially, the "negate" operation is actually to find the complement to <span class="arithmatex">\(1111\)</span> (because <code>sign-magnitude + 1's complement = 1111</code> always holds); and the 1's complement plus 1 is equal to the 2's complement to <span class="arithmatex">\(10000\)</span>.</p>
<p>We take <span class="arithmatex">\(n = 4\)</span> as an example in the above, and it can be generalized to any binary number with any number of digits.</p>
<p>In summary, both "first negate then add 1" and "first subtract 1 then negate" are computing the complement to <span class="arithmatex">\(10000\)</span>, and they are equivalent.</p>
<p>Essentially, the "negate" operation is actually finding the complement to <span class="arithmatex">\(1111\)</span> (because <code>sign-magnitude + 1's complement = 1111</code> always holds); and adding 1 to the 1's complement yields the 2's complement, which is the complement to <span class="arithmatex">\(10000\)</span>.</p>
<p>The above uses <span class="arithmatex">\(n = 4\)</span> as an example, and it can be generalized to binary numbers of any number of bits.</p>
<!-- Source file information -->
@@ -4448,7 +4447,7 @@ aria-label="Footer"
<a
href="../character_encoding/"
class="md-footer__link md-footer__link--prev"
aria-label="Previous: 3.4 Character encoding *"
aria-label="Previous: 3.4 Character Encoding *"
rel="prev"
>
<div class="md-footer__button md-icon">
@@ -4460,7 +4459,7 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
3.4 Character encoding *
3.4 Character Encoding *
</div>
</div>
</a>
@@ -4472,7 +4471,7 @@ aria-label="Footer"
<a
href="../../chapter_array_and_linkedlist/"
class="md-footer__link md-footer__link--next"
aria-label="Next: Chapter 4. &amp;nbsp; Arrays and linked lists"
aria-label="Next: Chapter 4. &amp;nbsp; Array and Linked List"
rel="next"
>
<div class="md-footer__title">
@@ -4480,7 +4479,7 @@ aria-label="Footer"
Next
</span>
<div class="md-ellipsis">
Chapter 4. &nbsp; Arrays and linked lists
Chapter 4. &nbsp; Array and Linked List
</div>
</div>
<div class="md-footer__button md-icon">
@@ -4573,7 +4572,7 @@ aria-label="Footer"
<nav class="md-footer__inner md-grid" aria-label="Footer" >
<a href="../character_encoding/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 3.4 Character encoding *">
<a href="../character_encoding/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 3.4 Character Encoding *">
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11z"/></svg>
@@ -4583,20 +4582,20 @@ aria-label="Footer"
Previous
</span>
<div class="md-ellipsis">
3.4 Character encoding *
3.4 Character Encoding *
</div>
</div>
</a>
<a href="../../chapter_array_and_linkedlist/" class="md-footer__link md-footer__link--next" aria-label="Next: Chapter 4. &amp;nbsp; Arrays and linked lists">
<a href="../../chapter_array_and_linkedlist/" class="md-footer__link md-footer__link--next" aria-label="Next: Chapter 4. &amp;nbsp; Array and Linked List">
<div class="md-footer__title">
<span class="md-footer__direction">
Next
</span>
<div class="md-ellipsis">
Chapter 4. &nbsp; Arrays and linked lists
Chapter 4. &nbsp; Array and Linked List
</div>
</div>
<div class="md-footer__button md-icon">