mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-11 11:07:14 +08:00
Revisit the English version (#1835)
* Review the English version using Claude-4.5. * Update mkdocs.yml * Align the section titles. * Bug fixes
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
# Encounter with algorithms
|
||||
# Introduction to algorithms
|
||||
|
||||

|
||||

|
||||
|
||||
!!! 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.
|
||||
A young girl dances gracefully, intertwined with data, her skirt flowing with the melody of algorithms.
|
||||
|
||||
She invites you to dance with her. Follow her steps closely and enter the world of algorithms, full of logic and beauty.
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
# 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 step-by-step instructions for solving a specific problem within a finite time, while a data structure defines how 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 compare data structures and algorithms to assembling 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.
|
||||
- Algorithms are ubiquitous in daily life and are not distant, esoteric knowledge. In fact, we have already learned many algorithms unconsciously and use them to solve problems big and small in life.
|
||||
- The principle of looking up a dictionary is consistent with the binary search algorithm. Binary search embodies the important algorithmic idea of divide and conquer.
|
||||
- The process of organizing playing cards is very similar to the insertion sort algorithm. Insertion sort is suitable for sorting small datasets.
|
||||
- The steps of making change are essentially a greedy algorithm, where the best choice is made at each step based on the current situation.
|
||||
- An algorithm is a set of instructions or operational steps that solves a specific problem within a finite amount of time, while a data structure is the way computers organize and store data.
|
||||
- Data structures and algorithms are closely connected. Data structures are the foundation of algorithms, and algorithms breathe life into data structures.
|
||||
- We can compare data structures and algorithms to assembling building blocks. The blocks represent data, the shape and connection method of the blocks represent the data structure, and the steps to assemble the blocks correspond to the algorithm.
|
||||
|
||||
### Q & A
|
||||
|
||||
**Q**:As a programmer, I’ve rarely needed to implement algorithms manually in my daily work. Most commonly used algorithms are already built into programming languages and libraries, ready to use. Does this suggest that the problems we encounter in our work haven’t yet reached the level of complexity that demands custom algorithm design?
|
||||
**Q**: As a programmer, I have never used algorithms to solve problems in my daily work. Common algorithms are already encapsulated by programming languages and can be used directly. Does this mean that the problems in our work have not yet reached the level where algorithms are needed?
|
||||
|
||||
If specific work skills are like the "moves" in martial arts, then fundamental subjects are more like "internal strength".
|
||||
If we compare specific work skills to "techniques" in martial arts, then fundamental subjects should be more like "internal skills".
|
||||
|
||||
I believe the significance of learning algorithms (and other fundamental subjects) isn’t necessarily to implement them from scratch at work, but to enable more professional decision-making and problem-solving based on a solid understanding of the concepts. This, in turn, raises the overall quality of our work. For example, every programming language provides a built-in sorting function:
|
||||
I believe the significance of learning algorithms (and other fundamental subjects) is not to implement them from scratch at work, but rather to be able to make professional reactions and judgments when solving problems based on the knowledge learned, thereby improving the overall quality of work. Here is a simple example. Every programming language has a built-in sorting function:
|
||||
|
||||
- If we have not learned data structures and algorithms, then given any data, we might just give it to this sorting function. It runs smoothly, has good performance, and seems to have no problems.
|
||||
- However, if we’ve studied algorithms, we understand that the time complexity of a built-in sorting function is typically $O(n \log n)$. Moreover, if the data consists of integers with a fixed number of digits (such as student IDs), we can apply a more efficient approach like radix sort, reducing the time complexity to O(nk) , where k is the number of digits. When handling large volumes of data, the time saved can turn into significant value — lowering costs, improving user experience, and enhancing system performance.
|
||||
- If we have not studied data structures and algorithms, we might simply feed any given data to this sorting function. It runs smoothly with good performance, and there doesn't seem to be any problem.
|
||||
- But if we have studied algorithms, we would know that the time complexity of the built-in sorting function is $O(n \log n)$. However, if the given data consists of integers with a fixed number of digits (such as student IDs), we can use the more efficient "radix sort", reducing the time complexity to $O(nk)$, where $k$ is the number of digits. When the data volume is very large, the saved running time can create significant value (reduced costs, improved experience, etc.).
|
||||
|
||||
In engineering, many problems are difficult to solve optimally; most are addressed with ‘near-optimal’ solutions. The difficulty of a problem depends not only on its inherent complexity but also on the knowledge and experience of the person tackling it. The deeper one’s expertise and experience, the more thorough the analysis, and the more elegantly the problem can be solved.
|
||||
In the field of engineering, a large number of problems are difficult to reach optimal solutions, and many problems are only solved "approximately". The difficulty of a problem depends on one hand on the nature of the problem itself, and on the other hand on the knowledge reserve of the person observing the problem. The more complete a person's knowledge and the more experience they have, the deeper their analysis of the problem will be, and the more elegantly the problem can be solved.
|
||||
|
||||
@@ -1,53 +1,53 @@
|
||||
# What is an algorithm
|
||||
|
||||
## Definition of an algorithm
|
||||
## Algorithm definition
|
||||
|
||||
An <u>algorithm</u> is a set of instructions or steps to solve a specific problem within a finite amount of time. It has the following characteristics:
|
||||
An <u>algorithm</u> is a set of instructions or operational steps that solves 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.
|
||||
- The problem is well-defined, with clear input and output definitions.
|
||||
- It is feasible and can be completed within a finite number of steps, time, and memory space.
|
||||
- Each step has a definite meaning, and under the same input and operating conditions, the output is always the same.
|
||||
|
||||
## Definition of a data structure
|
||||
## Data structure definition
|
||||
|
||||
A <u>data structure</u> is a way of organizing and storing data in a computer, with the following design goals:
|
||||
A <u>data structure</u> is a way of organizing and storing data, covering the data content, relationships between data, and methods for data operations. It has the following design objectives.
|
||||
|
||||
- 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.
|
||||
- Occupy as little space as possible to save computer memory.
|
||||
- Data operations should be as fast as possible, covering data access, addition, deletion, update, etc.
|
||||
- Provide a concise data representation and logical information so that algorithms can run efficiently.
|
||||
|
||||
**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:
|
||||
**Data structure design is a process full of trade-offs**. If we want to achieve improvements in one aspect, we often need to make compromises in another aspect. Here are two examples.
|
||||
|
||||
- Compared to arrays, linked lists offer more convenience in data addition and deletion but sacrifice data access speed.
|
||||
- Compared with linked lists, graphs provide richer logical information but require more memory space.
|
||||
- Compared to arrays, linked lists are more convenient for data addition and deletion operations but sacrifice data access speed.
|
||||
- Compared to linked lists, graphs provide richer logical information but require larger memory space.
|
||||
|
||||
## Relationship between data structures and algorithms
|
||||
## The relationship between data structures and algorithms
|
||||
|
||||
As shown in the figure below, data structures and algorithms are highly related and closely integrated, specifically in the following three aspects:
|
||||
As shown in the figure below, data structures and algorithms are highly related and tightly coupled, specifically manifested 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 inject vitality into data structures. 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.
|
||||
- Data structures are the foundation of algorithms. Data structures provide algorithms with structured storage of data and methods for operating on data.
|
||||
- Algorithms breathe life into data structures. Data structures themselves only store data information; combined with algorithms, they can solve specific problems.
|
||||
- Algorithms can usually be implemented based on different data structures, but execution efficiency may vary greatly. Choosing the appropriate data structure is key.
|
||||
|
||||

|
||||

|
||||
|
||||
Data structures and algorithms can be likened to a set of building blocks, as illustrated in the figure below. 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.
|
||||
Data structures and algorithms are like assembling building blocks as shown in the figure below. A set of building blocks, in addition to containing many parts, also comes with detailed assembly instructions. By following the instructions step by step, we can assemble an exquisite building block model.
|
||||
|
||||

|
||||
|
||||
The detailed correspondence between the two is shown in the table below.
|
||||
|
||||
<p align="center"> Table <id> Comparing data structures and algorithms to building blocks </p>
|
||||
<p align="center"> Table <id> Comparing data structures and algorithms to assembling building blocks </p>
|
||||
|
||||
| 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 |
|
||||
| Data structures and algorithms | Assembling building blocks |
|
||||
| ------------------------------ | ------------------------------------------------------------------ |
|
||||
| Input data | Unassembled building blocks |
|
||||
| Data structure | Organization form of building blocks, including shape, size, connection method, etc. |
|
||||
| Algorithm | A series of operational steps to assemble the blocks into the target form |
|
||||
| Output data | Building block model |
|
||||
|
||||
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.
|
||||
It is worth noting that data structures and algorithms are independent of programming languages. For this reason, this book is able to provide implementations based on multiple programming languages.
|
||||
|
||||
!!! tip "Conventional Abbreviation"
|
||||
!!! 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 questions actually test knowledge of both data structures and algorithms.
|
||||
In actual discussions, we usually abbreviate "data structures and algorithms" as "algorithms". For example, the well-known LeetCode algorithm problems actually examine knowledge of both data structures and algorithms.
|
||||
|
||||
Reference in New Issue
Block a user