mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-24 02:03:10 +08:00
build
This commit is contained in:
66
docs-en/chapter_introduction/algorithms_are_everywhere.md
Normal file
66
docs-en/chapter_introduction/algorithms_are_everywhere.md
Normal file
@ -0,0 +1,66 @@
|
||||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 1.1 Algorithms Are Everywhere
|
||||
|
||||
When we hear the word "algorithm", we naturally think of mathematics. However, many algorithms do not involve complex mathematics but rely more on basic logic, which is ubiquitous in our daily lives.
|
||||
|
||||
Before we formally discuss algorithms, an interesting fact is worth sharing: **you have already learned many algorithms unconsciously and have become accustomed to applying them in your daily life**. Below, I will give a few specific examples to prove this point.
|
||||
|
||||
**Example 1: Looking Up a Dictionary**. In a standard dictionary, each word corresponds to a phonetic transcription and the dictionary is organized alphabetically based on these transcriptions. Let's say we're looking for a word that begins with the letter $r$. This is typically done in the following way:
|
||||
|
||||
1. Open the dictionary around its midpoint and note the first letter on that page, assuming it to be $m$.
|
||||
2. Given the sequence of words following the initial letter $m$, estimate where words starting with the letter $r$ might be located within the alphabetical order.
|
||||
3. Iterate steps `1.` and `2.` until you find the page where the word begins with the letter $r$.
|
||||
|
||||
=== "<1>"
|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<2>"
|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<3>"
|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<4>"
|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<5>"
|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> Figure 1-1 Dictionary search step </p>
|
||||
|
||||
The skill of looking up a dictionary, essential for elementary school students, is actually the renowned binary search algorithm. Through the lens of data structures, we can view the dictionary as a sorted "array"; while from an algorithmic perspective, the series of operations in looking up a dictionary can be seen as "binary search".
|
||||
|
||||
**Example 2: Organizing Playing Cards**. When playing cards, we need to arrange the cards in ascending order each game, as shown in the following process.
|
||||
|
||||
1. Divide the playing cards into "ordered" and "unordered" parts, assuming initially that the leftmost card is already ordered.
|
||||
2. Take out a card from the unordered part and insert it into the correct position in the ordered part; once completed, the leftmost two cards will be in an ordered sequence.
|
||||
3. Continue the loop described in step `2.`, each iteration involving insertion of one card from the unordered segment into the ordered portion, until all cards are appropriately ordered.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> Figure 1-2 Playing cards sorting process </p>
|
||||
|
||||
The above method of organizing playing cards is essentially the "insertion sort" algorithm, which is very efficient for small datasets. Many programming languages' sorting library functions include insertion sort.
|
||||
|
||||
**Example 3: Making Change**. Suppose we buy goods worth $69$ yuan at a supermarket and give the cashier $100$ yuan, then the cashier needs to give us $31$ yuan in change. They would naturally complete the thought process as shown below.
|
||||
|
||||
1. The options are currencies smaller than $31$, including $1$, $5$, $10$, and $20$.
|
||||
2. Take out the largest $20$ from the options, leaving $31 - 20 = 11$.
|
||||
3. Take out the largest $10$ from the remaining options, leaving $11 - 10 = 1$.
|
||||
4. Take out the largest $1$ from the remaining options, leaving $1 - 1 = 0$.
|
||||
5. Complete the change-making, with the solution being $20 + 10 + 1 = 31$.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> Figure 1-3 Change making process </p>
|
||||
|
||||
In the aforementioned steps, at each stage, we make the optimal choice (utilizing the highest denomination possible), ultimately deriving at a feasible change-making approach. From the perspective of data structures and algorithms, this approach is essentially a "greedy" algorithm.
|
||||
|
||||
From preparing a dish to traversing interstellar realms, virtually every problem-solving endeavor relies on algorithms. The emergence of computers enables us to store data structures in memory and write code to call CPUs and GPUs to execute algorithms. Consequently, we can transfer real-life predicaments to computers, efficiently addressing a myriad of complex issues.
|
||||
|
||||
!!! tip
|
||||
|
||||
If concepts such as data structures, algorithms, arrays, and binary search still seem somewhat obsecure, I encourage you to continue reading. This book will gently guide you into the realm of understanding data structures and algorithms.
|
24
docs-en/chapter_introduction/index.md
Normal file
24
docs-en/chapter_introduction/index.md
Normal file
@ -0,0 +1,24 @@
|
||||
---
|
||||
comments: true
|
||||
icon: material/calculator-variant-outline
|
||||
---
|
||||
|
||||
# 第 1 章 Introduction to Algorithms
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
{ width="600" }{ class="cover-image" }
|
||||
|
||||
</div>
|
||||
|
||||
!!! abstract
|
||||
|
||||
A graceful maiden dances, intertwined with the data, her skirt swaying to the melody of algorithms.
|
||||
|
||||
She invites you to a dance, follow her steps, and enter the world of algorithms full of logic and beauty.
|
||||
|
||||
## 本章内容
|
||||
|
||||
- [1.1 Algorithms Are Everywhere](https://www.hello-algo.com/chapter_introduction/algorithms_are_everywhere/)
|
||||
- [1.2 What is Algorithms](https://www.hello-algo.com/chapter_introduction/what_is_dsa/)
|
||||
- [1.3 Summary](https://www.hello-algo.com/chapter_introduction/summary/)
|
13
docs-en/chapter_introduction/summary.md
Normal file
13
docs-en/chapter_introduction/summary.md
Normal file
@ -0,0 +1,13 @@
|
||||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 1.3 Summary
|
||||
|
||||
- Algorithms are ubiquitous in daily life and are not as inaccessible and complex as they might seem. In fact, we have already unconsciously learned many algorithms to solve various problems in life.
|
||||
- The principle of looking up a word in a dictionary is consistent with the binary search algorithm. The binary search algorithm embodies the important algorithmic concept of divide and conquer.
|
||||
- The process of organizing playing cards is very similar to the insertion sort algorithm. The insertion sort algorithm is suitable for sorting small datasets.
|
||||
- The steps of making change in currency essentially follow the greedy algorithm, where each step involves making the best possible choice at the moment.
|
||||
- An algorithm is a set of instructions or steps used to solve a specific problem within a finite amount of time, while a data structure is the way data is organized and stored in a computer.
|
||||
- Data structures and algorithms are closely linked. Data structures are the foundation of algorithms, and algorithms are the stage to utilize the functions of data structures.
|
||||
- We can liken data structures and algorithms to building blocks. The blocks represent data, the shape and connection method of the blocks represent data structures, and the steps of assembling the blocks correspond to algorithms.
|
65
docs-en/chapter_introduction/what_is_dsa.md
Normal file
65
docs-en/chapter_introduction/what_is_dsa.md
Normal file
@ -0,0 +1,65 @@
|
||||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 1.2 What is an Algorithm
|
||||
|
||||
## 1.2.1 Definition of an Algorithm
|
||||
|
||||
An "algorithm" is a set of instructions or steps to solve a specific problem within a finite amount of time. It has the following characteristics:
|
||||
|
||||
- The problem is clearly defined, including unambiguous definitions of input and output.
|
||||
- The algorithm is feasible, meaning it can be completed within a finite number of steps, time, and memory space.
|
||||
- Each step has a definitive meaning. The output is consistently the same under the same inputs and conditions.
|
||||
|
||||
## 1.2.2 Definition of a Data Structure
|
||||
|
||||
A "data structure" is a way of organizing and storing data in a computer, with the following design goals:
|
||||
|
||||
- Minimize space occupancy to save computer memory.
|
||||
- Make data operations as fast as possible, covering data access, addition, deletion, updating, etc.
|
||||
- Provide concise data representation and logical information to enable efficient algorithm execution.
|
||||
|
||||
**Designing data structures is a balancing act, often requiring trade-offs**. If you want to improve in one aspect, you often need to compromise in another. Here are two examples:
|
||||
|
||||
- Compared to arrays, linked lists offer more convenience in data addition and deletion but sacrifice data access speed.
|
||||
- Graphs, compared to linked lists, provide richer logical information but require more memory space.
|
||||
|
||||
## 1.2.3 Relationship Between Data Structures and Algorithms
|
||||
|
||||
As shown in the diagram below, data structures and algorithms are highly related and closely integrated, specifically in the following three aspects:
|
||||
|
||||
- Data structures are the foundation of algorithms. They provide structured data storage and methods for manipulating data for algorithms.
|
||||
- Algorithms are the stage where data structures come into play. The data structure alone only stores data information; it is through the application of algorithms that specific problems can be solved.
|
||||
- Algorithms can often be implemented based on different data structures, but their execution efficiency can vary greatly. Choosing the right data structure is key.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> Figure 1-4 Relationship between data structures and algorithms </p>
|
||||
|
||||
Data structures and algorithms can be likened to a set of building blocks, as illustrated in the Figure 1-5 . A building block set includes numerous pieces, accompanied by detailed assembly instructions. Following these instructions step by step allows us to construct an intricate block model.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> Figure 1-5 Assembling blocks </p>
|
||||
|
||||
The detailed correspondence between the two is shown in the Table 1-1 .
|
||||
|
||||
<p align="center"> Table 1-1 Comparing Data Structures and Algorithms to Building Blocks </p>
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
| Data Structures and Algorithms | Building Blocks |
|
||||
| ------------------------------ | --------------------------------------------------------------- |
|
||||
| Input data | Unassembled blocks |
|
||||
| Data structure | Organization of blocks, including shape, size, connections, etc |
|
||||
| Algorithm | A series of steps to assemble the blocks into the desired shape |
|
||||
| Output data | Completed Block model |
|
||||
|
||||
</div>
|
||||
|
||||
It's worth noting that data structures and algorithms are independent of programming languages. For this reason, this book is able to provide implementations in multiple programming languages.
|
||||
|
||||
!!! tip "Conventional Abbreviation"
|
||||
|
||||
In real-life discussions, we often refer to "Data Structures and Algorithms" simply as "Algorithms". For example, the well-known LeetCode algorithm problems actually test both data structure and algorithm knowledge.
|
Reference in New Issue
Block a user