mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-28 19:07:14 +00:00
deploy
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
|
||||
<meta name="description" content="Data Structures and Algorithms Crash Course with Animated Illustrations and Off-the-Shelf Code">
|
||||
<meta name="description" content="Data structures and algorithms tutorial with animated illustrations and ready-to-run code">
|
||||
|
||||
|
||||
<meta name="author" content="krahets">
|
||||
@@ -578,7 +578,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
Chapter 1. Encounter With Algorithms
|
||||
Chapter 1. Encounter with Algorithms
|
||||
|
||||
|
||||
|
||||
@@ -600,7 +600,7 @@
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
|
||||
|
||||
Chapter 1. Encounter With Algorithms
|
||||
Chapter 1. Encounter with Algorithms
|
||||
|
||||
|
||||
</label>
|
||||
@@ -1195,7 +1195,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
Chapter 4. Array and Linked List
|
||||
Chapter 4. Arrays and Linked Lists
|
||||
|
||||
|
||||
|
||||
@@ -1217,7 +1217,7 @@
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
|
||||
|
||||
Chapter 4. Array and Linked List
|
||||
Chapter 4. Arrays and Linked Lists
|
||||
|
||||
|
||||
</label>
|
||||
@@ -1323,7 +1323,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
4.4 Memory and Cache *
|
||||
4.4 Random-Access Memory and Cache *
|
||||
|
||||
|
||||
|
||||
@@ -1414,7 +1414,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
Chapter 5. Stack and Queue
|
||||
Chapter 5. Stacks and Queues
|
||||
|
||||
|
||||
|
||||
@@ -1436,7 +1436,7 @@
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
|
||||
|
||||
Chapter 5. Stack and Queue
|
||||
Chapter 5. Stacks and Queues
|
||||
|
||||
|
||||
</label>
|
||||
@@ -1514,7 +1514,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
5.3 Double-Ended Queue
|
||||
5.3 Deque
|
||||
|
||||
|
||||
|
||||
@@ -1605,7 +1605,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
Chapter 6. Hashing
|
||||
Chapter 6. Hash Table
|
||||
|
||||
|
||||
|
||||
@@ -1627,7 +1627,7 @@
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
|
||||
|
||||
Chapter 6. Hashing
|
||||
Chapter 6. Hash Table
|
||||
|
||||
|
||||
</label>
|
||||
@@ -1900,7 +1900,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
7.3 Array Representation of Tree
|
||||
7.3 Array Representation of Binary Trees
|
||||
|
||||
|
||||
|
||||
@@ -2119,7 +2119,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
8.2 Building a Heap
|
||||
8.2 Heap Construction Operation
|
||||
|
||||
|
||||
|
||||
@@ -2147,7 +2147,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
8.3 Top-K Problem
|
||||
8.3 Top-k Problem
|
||||
|
||||
|
||||
|
||||
@@ -2505,7 +2505,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
10.2 Binary Search Insertion
|
||||
10.2 Binary Search Insertion Point
|
||||
|
||||
|
||||
|
||||
@@ -2533,7 +2533,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
10.3 Binary Search Edge Cases
|
||||
10.3 Binary Search Boundaries
|
||||
|
||||
|
||||
|
||||
@@ -2589,7 +2589,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
10.5 Search Algorithms Revisited
|
||||
10.5 Searching Algorithms Revisited
|
||||
|
||||
|
||||
|
||||
@@ -2738,7 +2738,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
11.1 Sorting Algorithms
|
||||
11.1 Sorting Algorithm
|
||||
|
||||
|
||||
|
||||
@@ -3211,7 +3211,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
12.4 Hanoi Tower Problem
|
||||
12.4 Hanota Problem
|
||||
|
||||
|
||||
|
||||
@@ -4123,7 +4123,7 @@
|
||||
<span class="md-ellipsis">
|
||||
|
||||
|
||||
16.3 Terminology Table
|
||||
16.3 Glossary
|
||||
|
||||
|
||||
|
||||
@@ -4268,10 +4268,10 @@
|
||||
<!-- Page content -->
|
||||
<h1 id="11-algorithms-are-everywhere">1.1 Algorithms Are Everywhere<a class="headerlink" href="#11-algorithms-are-everywhere" title="Permanent link">¶</a></h1>
|
||||
<p>When we hear the term "algorithm," we naturally think of mathematics. However, many algorithms do not involve complex mathematics but rely more on basic logic, which can be seen everywhere in our daily lives.</p>
|
||||
<p>Before we start discussing about algorithms officially, there's an interesting fact worth sharing: <strong>you've learned many algorithms unconsciously and are used to applying them in your daily life</strong>. Here, I will give a few specific examples to prove this point.</p>
|
||||
<p>Before we formally explore algorithms, here's an interesting fact worth sharing: <strong>you have already learned many algorithms without realizing it, and you are used to applying them in daily life</strong>. Let me give a few specific examples to illustrate this point.</p>
|
||||
<p><strong>Example 1: Looking Up a Dictionary</strong>. In an English dictionary, words are listed alphabetically. Assuming we're searching for a word that starts with the letter <span class="arithmatex">\(r\)</span>, this is typically done in the following way:</p>
|
||||
<ol>
|
||||
<li>Open the dictionary to about halfway and check the first vocabulary of the page, let's say the letter starts with <span class="arithmatex">\(m\)</span>.</li>
|
||||
<li>Open the dictionary to about halfway and check the first word on that page; suppose it starts with the letter <span class="arithmatex">\(m\)</span>.</li>
|
||||
<li>Since <span class="arithmatex">\(r\)</span> comes after <span class="arithmatex">\(m\)</span> in the alphabet, the first half can be ignored and the search space is narrowed down to the second half.</li>
|
||||
<li>Repeat steps <code>1.</code> and <code>2.</code> until you find the page where the word starts with <span class="arithmatex">\(r\)</span>.</li>
|
||||
</ol>
|
||||
@@ -4297,7 +4297,7 @@
|
||||
<p align="center"> Figure 1-1 Process of looking up a dictionary </p>
|
||||
|
||||
<p>Looking up a dictionary, an essential skill for elementary school students is actually the famous "Binary Search" algorithm. From a data structure perspective, we can consider the dictionary as a sorted "array"; from an algorithmic perspective, the series of actions taken to look up a word in the dictionary can be viewed as the algorithm "Binary Search."</p>
|
||||
<p><strong>Example 2: Organizing Card Deck</strong>. When playing cards, we need to arrange the cards in our hands in ascending order, as shown in the following process.</p>
|
||||
<p><strong>Example 2: Organizing Playing Cards</strong>. When playing cards, we need to arrange the cards in our hands in ascending order, as shown in the following process.</p>
|
||||
<ol>
|
||||
<li>Divide the playing cards into "ordered" and "unordered" sections, assuming initially the leftmost card is already in order.</li>
|
||||
<li>Take out a card from the unordered section and insert it into the correct position in the ordered section; after this, the leftmost two cards are in order.</li>
|
||||
@@ -4306,10 +4306,10 @@
|
||||
<p><img alt="Process of sorting a deck of cards" class="animation-figure" src="../algorithms_are_everywhere.assets/playing_cards_sorting.png" /></p>
|
||||
<p align="center"> Figure 1-2 Process of sorting a deck of cards </p>
|
||||
|
||||
<p>The above method of organizing playing cards is practically the "Insertion Sort" algorithm, which is very efficient for small datasets. Many programming languages' sorting functions include the insertion sort.</p>
|
||||
<p>The above method of organizing playing cards is essentially the "Insertion Sort" algorithm, which is very efficient for small datasets. Many programming languages' built-in sorting implementations use insertion sort internally.</p>
|
||||
<p><strong>Example 3: Making Change</strong>. Assume making a purchase of <span class="arithmatex">\(69\)</span> at a supermarket. If you give the cashier <span class="arithmatex">\(100\)</span>, they will need to provide you with <span class="arithmatex">\(31\)</span> in change. This process can be clearly understood as illustrated in Figure 1-3.</p>
|
||||
<ol>
|
||||
<li>The options are currencies valued below <span class="arithmatex">\(31\)</span>, including <span class="arithmatex">\(1\)</span>, <span class="arithmatex">\(5\)</span>, <span class="arithmatex">\(10\)</span>, and <span class="arithmatex">\(20\)</span>.</li>
|
||||
<li>The available denominations smaller than <span class="arithmatex">\(31\)</span> are <span class="arithmatex">\(1\)</span>, <span class="arithmatex">\(5\)</span>, <span class="arithmatex">\(10\)</span>, and <span class="arithmatex">\(20\)</span>.</li>
|
||||
<li>Take out the largest <span class="arithmatex">\(20\)</span> from the options, leaving <span class="arithmatex">\(31 - 20 = 11\)</span>.</li>
|
||||
<li>Take out the largest <span class="arithmatex">\(10\)</span> from the remaining options, leaving <span class="arithmatex">\(11 - 10 = 1\)</span>.</li>
|
||||
<li>Take out the largest <span class="arithmatex">\(1\)</span> from the remaining options, leaving <span class="arithmatex">\(1 - 1 = 0\)</span>.</li>
|
||||
@@ -4318,11 +4318,11 @@
|
||||
<p><img alt="Process of making change" class="animation-figure" src="../algorithms_are_everywhere.assets/greedy_change.png" /></p>
|
||||
<p align="center"> Figure 1-3 Process of making change </p>
|
||||
|
||||
<p>In the steps described, we choose the best option at each stage by utilizing the largest denomination available, which leads to an effective change-making strategy. From a data structures and algorithms perspective, this approach is known as a "Greedy" algorithm.</p>
|
||||
<p>In the steps above, we choose what seems to be the best option at each stage by using the largest denomination available, which leads to an effective way to make change. From a data structures and algorithms perspective, this approach is known as a "Greedy" algorithm.</p>
|
||||
<p>From cooking a meal to interstellar travel, almost all problem-solving involves algorithms. The advent of computers allows us to store data structures in memory and write code to call the CPU and GPU to execute algorithms. In this way, we can transfer real-life problems to computers and solve various complex issues in a more efficient way.</p>
|
||||
<div class="admonition tip">
|
||||
<p class="admonition-title">Tip</p>
|
||||
<p>If you are still confused about concepts like data structures, algorithms, arrays, and binary searches, I encourage you to keep reading. This book will gently guide you into the realm of understanding data structures and algorithms.</p>
|
||||
<p>If concepts such as data structures, algorithms, arrays, and binary search still feel only half-familiar, keep reading. This book will guide you into the world of data structures and algorithms.</p>
|
||||
</div>
|
||||
|
||||
<!-- Source file information -->
|
||||
|
||||
Reference in New Issue
Block a user