mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-16 13:40:58 +00:00
build
This commit is contained in:
@@ -6,11 +6,11 @@ comments: true
|
||||
|
||||
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.
|
||||
|
||||
Before we start discussing about algorithms officially, there's an interesting fact worth sharing: **you've learned many algorithms unconsciously and are used to applying them in your daily life**. Here, I will give a few specific examples to prove this point.
|
||||
Before we formally explore algorithms, here's an interesting fact worth sharing: **you have already learned many algorithms without realizing it, and you are used to applying them in daily life**. Let me give a few specific examples to illustrate this point.
|
||||
|
||||
**Example 1: Looking Up a Dictionary**. In an English dictionary, words are listed alphabetically. Assuming we're searching for a word that starts with the letter $r$, this is typically done in the following way:
|
||||
|
||||
1. Open the dictionary to about halfway and check the first vocabulary of the page, let's say the letter starts with $m$.
|
||||
1. Open the dictionary to about halfway and check the first word on that page; suppose it starts with the letter $m$.
|
||||
2. Since $r$ comes after $m$ in the alphabet, the first half can be ignored and the search space is narrowed down to the second half.
|
||||
3. Repeat steps `1.` and `2.` until you find the page where the word starts with $r$.
|
||||
|
||||
@@ -33,7 +33,7 @@ Before we start discussing about algorithms officially, there's an interesting f
|
||||
|
||||
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."
|
||||
|
||||
**Example 2: Organizing Card Deck**. When playing cards, we need to arrange the cards in our hands in ascending order, as shown in the following process.
|
||||
**Example 2: Organizing Playing Cards**. When playing cards, we need to arrange the cards in our hands in ascending order, as shown in the following process.
|
||||
|
||||
1. Divide the playing cards into "ordered" and "unordered" sections, assuming initially the leftmost card is already in order.
|
||||
2. 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.
|
||||
@@ -43,11 +43,11 @@ Looking up a dictionary, an essential skill for elementary school students is ac
|
||||
|
||||
<p align="center"> Figure 1-2 Process of sorting a deck of cards </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.
|
||||
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.
|
||||
|
||||
**Example 3: Making Change**. Assume making a purchase of $69$ at a supermarket. If you give the cashier $100$, they will need to provide you with $31$ in change. This process can be clearly understood as illustrated in Figure 1-3.
|
||||
|
||||
1. The options are currencies valued below $31$, including $1$, $5$, $10$, and $20$.
|
||||
1. The available denominations smaller than $31$ are $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$.
|
||||
@@ -57,10 +57,10 @@ The above method of organizing playing cards is practically the "Insertion Sort"
|
||||
|
||||
<p align="center"> Figure 1-3 Process of making change </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.
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
!!! tip
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
@@ -6,13 +6,13 @@ comments: true
|
||||
|
||||
### 1. Key Review
|
||||
|
||||
- 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.
|
||||
- Algorithms are ubiquitous in daily life and are not some distant, esoteric body of 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.
|
||||
- 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 a way of organizing and storing data in a computer.
|
||||
- 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.
|
||||
- We can compare data structures and algorithms to assembling building blocks. The blocks represent data, the way they are shaped and connected represents the data structure, and the steps used to assemble them correspond to the algorithm.
|
||||
|
||||
### 2. Q & A
|
||||
|
||||
@@ -20,9 +20,9 @@ comments: true
|
||||
|
||||
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) 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:
|
||||
I believe the significance of learning algorithms (and other fundamental subjects) is not that you will need to implement them from scratch at work, but that the knowledge you gain enables you to make sound professional judgments when solving problems, thereby improving the overall quality of your work. Here is a simple example. Every programming language has a built-in sorting function:
|
||||
|
||||
- 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 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.
|
||||
In engineering, many problems are difficult to solve optimally, and many others are only solved "well enough." The difficulty of a problem depends, on the one hand, on the nature of the problem itself and, on the other hand, on the knowledge of the person examining it. The more complete a person's knowledge and the more experience they have, the deeper their analysis will be, and the more elegantly the problem can be solved.
|
||||
|
||||
@@ -9,12 +9,12 @@ comments: true
|
||||
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 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.
|
||||
- It is feasible and can be completed with finite steps, time, and memory.
|
||||
- Each step has a definite meaning, and under the same input and operating conditions, the output is always the same.
|
||||
|
||||
## 1.2.2 Data Structure Definition
|
||||
|
||||
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.
|
||||
A <u>data structure</u> is a way of organizing and storing data, including the data itself, the relationships between data elements, and the methods used to operate on them. It has the following design objectives.
|
||||
|
||||
- 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.
|
||||
@@ -58,7 +58,7 @@ The detailed correspondence between the two is shown in Table 1-1.
|
||||
|
||||
</div>
|
||||
|
||||
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.
|
||||
It is worth noting that data structures and algorithms are independent of programming languages. That is why this book can provide implementations in multiple programming languages.
|
||||
|
||||
!!! tip "Conventional abbreviation"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user