This commit is contained in:
krahets
2023-12-22 00:01:05 +08:00
parent ef0f9dba2e
commit eae73fc284
30 changed files with 8033 additions and 6 deletions

View File

@ -0,0 +1,51 @@
---
comments: true
---
# 0.1   The Book
The aim of this project is to create an open source, free, novice-friendly introductory tutorial on data structures and algorithms.
- Animated graphs are used throughout the book to structure the knowledge of data structures and algorithms in a way that is clear and easy to understand with a smooth learning curve.
- The source code of the algorithms can be run with a single click, supporting Java, C++, Python, Go, JS, TS, C#, Swift, Rust, Dart, Zig and other languages.
- Readers are encouraged to help each other and make progress in the chapter discussion forums, and questions and comments can usually be answered within two days.
## 0.1.1   Target Readers
If you are a beginner to algorithms, have never touched an algorithm before, or already have some experience brushing up on data structures and algorithms, and have a vague understanding of data structures and algorithms, repeatedly jumping sideways between what you can and can't do, then this book is just for you!
If you have already accumulated a certain amount of questions and are familiar with most of the question types, then this book can help you review and organize the algorithm knowledge system, and the repository source code can be used as a "brushing tool library" or "algorithm dictionary".
If you are an algorithm expert, we look forward to receiving your valuable suggestions or [participate in the creation together](https://www.hello-algo.com/chapter_appendix/contribution/).
!!! success "precondition"
You will need to have at least a basic knowledge of programming in any language and be able to read and write simple code.
## 0.1.2   Content Structure
The main contents of the book are shown in the Figure 0-1 .
- **Complexity Analysis**: dimensions and methods of evaluation of data structures and algorithms. Methods of deriving time complexity, space complexity, common types, examples, etc.
- **Data Structures**: basic data types, classification methods of data structures. Definition, advantages and disadvantages, common operations, common types, typical applications, implementation methods of data structures such as arrays, linked lists, stacks, queues, hash tables, trees, heaps, graphs, etc.
- **Algorithms**: definitions, advantages and disadvantages, efficiency, application scenarios, solution steps, sample topics of search, sorting algorithms, divide and conquer, backtracking algorithms, dynamic programming, greedy algorithms, and more.
![Hello Algo content structure](about_the_book.assets/hello_algo_mindmap.jpg){ class="animation-figure" }
<p align="center"> Figure 0-1 &nbsp; Hello Algo content structure </p>
## 0.1.3 &nbsp; Acknowledgements
During the creation of this book, I received help from many people, including but not limited to:
- Thank you to my mentor at the company, Dr. Shih Lee, for encouraging me to "get moving" during one of our conversations, which strengthened my resolve to write this book.
- I would like to thank my girlfriend Bubbles for being the first reader of this book, and for making many valuable suggestions from the perspective of an algorithm whiz, making this book more suitable for newbies.
- Thanks to Tengbao, Qibao, and Feibao for coming up with a creative name for this book that evokes fond memories of writing the first line of code "Hello World!".
- Thanks to Sutong for designing the beautiful cover and logo for this book and patiently revising it many times under my OCD.
- Thanks to @squidfunk for writing layout suggestions and for developing the open source documentation theme [Material-for-MkDocs](https://github.com/squidfunk/mkdocs-material/tree/master).
During the writing process, I read many textbooks and articles on data structures and algorithms. These works provide excellent models for this book and ensure the accuracy and quality of its contents. I would like to thank all my teachers and predecessors for their outstanding contributions!
This book promotes a hands-on approach to learning, and in this respect is heavily inspired by ["Hands-On Learning for Depth"](https://github.com/d2l-ai/d2l-zh). I highly recommend this excellent book to all readers.
A heartfelt thank you to my parents, it is your constant support and encouragement that gives me the opportunity to do this fun-filled thing.

View File

@ -0,0 +1,24 @@
---
comments: true
icon: material/book-open-outline
---
# 第 0 章 &nbsp; Preface
<div class="center-table" markdown>
![Preface](../assets/covers/chapter_preface.jpg){ width="600" }{ class="cover-image" }
</div>
!!! abstract
Algorithms are like a beautiful symphony, with each line of code flowing like a rhythm.
May this book ring softly in your head, leaving a unique and profound melody.
## 本章内容
- [0.1 &nbsp; The Book](https://www.hello-algo.com/chapter_preface/about_the_book/)
- [0.2 &nbsp; How to Read](https://www.hello-algo.com/chapter_preface/suggestions/)
- [0.3 &nbsp; Summary](https://www.hello-algo.com/chapter_preface/summary/)

View File

@ -0,0 +1,240 @@
---
comments: true
---
# 0.2 &nbsp; How To Read
!!! tip
For the best reading experience, it is recommended that you read through this section.
## 0.2.1 &nbsp; Conventions Of Style
- Those labeled `*` after the title are optional chapters with relatively difficult content. If you have limited time, it is advisable to skip them.
- Proper nouns and words and phrases with specific meanings are marked with `"double quotes"` to avoid ambiguity.
- Important proper nouns and their English translations are marked with `" "` in parentheses, e.g. `"array array"` . It is recommended to memorize them for reading the literature.
- **Bolded text** Indicates key content or summary statements, which deserve special attention.
- When it comes to terms that are inconsistent between programming languages, this book follows Python, for example using $\text{None}$ to mean "empty".
- This book partially abandons the specification of annotations in programming languages in exchange for a more compact layout of the content. There are three main types of annotations: title annotations, content annotations, and multi-line annotations.
=== "Python"
```python title=""
"""Header comments for labeling functions, classes, test samples, etc.""""
# Content comments for detailed code solutions
"""
multi-line
marginal notes
"""
```
=== "C++"
```cpp title=""
/* Header comments for labeling functions, classes, test samples, etc. */
// Content comments for detailed code solutions.
/**
* multi-line
* marginal notes
*/
```
=== "Java"
```java title=""
/* Header comments for labeling functions, classes, test samples, etc. */
// Content comments for detailed code solutions.
/**
* multi-line
* marginal notes
*/
```
=== "C#"
```csharp title=""
/* Header comments for labeling functions, classes, test samples, etc. */
// Content comments for detailed code solutions.
/**
* multi-line
* marginal notes
*/
```
=== "Go"
```go title=""
/* Header comments for labeling functions, classes, test samples, etc. */
// Content comments for detailed code solutions.
/**
* multi-line
* marginal notes
*/
```
=== "Swift"
```swift title=""
/* Header comments for labeling functions, classes, test samples, etc. */
// Content comments for detailed code solutions.
/**
* multi-line
* marginal notes
*/
```
=== "JS"
```javascript title=""
/* Header comments for labeling functions, classes, test samples, etc. */
// Content comments for detailed code solutions.
/**
* multi-line
* marginal notes
*/
```
=== "TS"
```typescript title=""
/* Header comments for labeling functions, classes, test samples, etc. */
// Content comments for detailed code solutions.
/**
* multi-line
* marginal notes
*/
```
=== "Dart"
```dart title=""
/* Header comments for labeling functions, classes, test samples, etc. */
// Content comments for detailed code solutions.
/**
* multi-line
* marginal notes
*/
```
=== "Rust"
```rust title=""
/* Header comments for labeling functions, classes, test samples, etc. */
// Content comments for detailed code solutions.
/**
* multi-line
* marginal notes
*/
```
=== "C"
```c title=""
/* Header comments for labeling functions, classes, test samples, etc. */
// Content comments for detailed code solutions.
/**
* multi-line
* marginal notes
*/
```
=== "Zig"
```zig title=""
// Header comments for labeling functions, classes, test samples, etc.
// Content comments for detailed code solutions.
// Multi-line
// Annotation
```
## 0.2.2 &nbsp; Learn Efficiently In Animated Graphic Solutions
Compared with text, videos and pictures have a higher degree of information density and structure and are easier to understand. In this book, **key and difficult knowledge will be presented mainly in the form of animations and graphs**, while the text serves as an explanation and supplement to the animations and graphs.
If, while reading the book, you find that a particular paragraph provides an animation or a graphic solution as shown below, **please use the figure as the primary source and the text as a supplement and synthesize the two to understand the content**.
![Example animation](../index.assets/animation.gif){ class="animation-figure" }
<p align="center"> Figure 0-2 &nbsp; Example animation </p>
## 0.2.3 &nbsp; Deeper Understanding In Code Practice
The companion code for this book is hosted in the [GitHub repository](https://github.com/krahets/hello-algo). As shown in the Figure 0-3 , **the source code is accompanied by test samples that can be run with a single click**.
If time permits, **it is recommended that you refer to the code and knock it through on your own**. If you have limited time to study, please read through and run all the code at least once.
The process of writing code is often more rewarding than reading it. **Learning by doing is really learning**.
![Running code example](../index.assets/running_code.gif){ class="animation-figure" }
<p align="center"> Figure 0-3 &nbsp; Running code example </p>
The preliminaries for running the code are divided into three main steps.
**Step 1: Install the local programming environment**. Please refer to [Appendix Tutorial](https://www.hello-algo.com/chapter_appendix/installation/) for installation, or skip this step if already installed.
**Step 2: Clone or download the code repository**. If [Git](https://git-scm.com/downloads) is already installed, you can clone this repository with the following command.
```shell
git clone https://github.com/krahets/hello-algo.git
```
Of course, you can also in the location shown in the Figure 0-4 , click "Download ZIP" directly download the code zip, and then in the local solution.
![Clone repository with download code](suggestions.assets/download_code.png){ class="animation-figure" }
<p align="center"> Figure 0-4 &nbsp; Clone repository with download code </p>
**Step 3: Run the source code**. As shown in the Figure 0-5 , for the code block labeled with the file name at the top, we can find the corresponding source code file in the `codes` folder of the repository. The source code files can be run with a single click, which will help you save unnecessary debugging time and allow you to focus on what you are learning.
![Code block with corresponding source file](suggestions.assets/code_md_to_repo.png){ class="animation-figure" }
<p align="center"> Figure 0-5 &nbsp; Code block with corresponding source file </p>
## 0.2.4 &nbsp; Growing Together In Questioning And Discussion
While reading this book, please don't skip over the points that you didn't learn. **Feel free to ask your questions in the comment section**. We will be happy to answer them and can usually respond within two days.
As you can see in the Figure 0-6 , each post comes with a comment section at the bottom. I hope you'll pay more attention to the comments section. On the one hand, you can learn about the problems that people encounter, so as to check the gaps and stimulate deeper thinking. On the other hand, we expect you to generously answer other partners' questions, share your insights, and help others improve.
![Example of comment section](../index.assets/comment.gif){ class="animation-figure" }
<p align="center"> Figure 0-6 &nbsp; Example of comment section </p>
## 0.2.5 &nbsp; Algorithm Learning Route
From a general point of view, we can divide the process of learning data structures and algorithms into three stages.
1. **Introduction to Algorithms**. We need to familiarize ourselves with the characteristics and usage of various data structures and learn about the principles, processes, uses and efficiency of different algorithms.
2. **Brush up on algorithm questions**. It is recommended to start brushing from popular topics, such as [Sword to Offer](https://leetcode.cn/studyplan/coding-interviews/) and [LeetCode Hot 100](https://leetcode.cn/studyplan/top-100- liked/), first accumulate at least 100 questions to familiarize yourself with mainstream algorithmic problems. Forgetfulness can be a challenge when first brushing up, but rest assured that this is normal. We can follow the "Ebbinghaus Forgetting Curve" to review the questions, and usually after 3-5 rounds of repetitions, we will be able to memorize them.
3. **Build the knowledge system**. In terms of learning, we can read algorithm column articles, solution frameworks and algorithm textbooks to continuously enrich the knowledge system. In terms of brushing, we can try to adopt advanced brushing strategies, such as categorizing by topic, multiple solutions, multiple solutions, etc. Related brushing tips can be found in various communities.
As shown in the Figure 0-7 , this book mainly covers "Phase 1" and is designed to help you start Phase 2 and 3 more efficiently.
![algorithm learning route](suggestions.assets/learning_route.png){ class="animation-figure" }
<p align="center"> Figure 0-7 &nbsp; algorithm learning route </p>

View File

@ -0,0 +1,12 @@
---
comments: true
---
# 0.3 &nbsp; Summary
- The main audience of this book is beginners in algorithm. If you already have some basic knowledge, this book can help you systematically review your algorithm knowledge, and the source code in this book can also be used as a "Coding Toolkit".
- The book consists of three main sections, Complexity Analysis, Data Structures, and Algorithms, covering most of the topics in the field.
- For newcomers to algorithms, it is crucial to read an introductory book in the beginning stages to avoid many detours or common pitfalls.
- Animations and graphs within the book are usually used to introduce key points and difficult knowledge. These should be given more attention when reading the book.
- Practice is the best way to learn programming. It is highly recommended that you run the source code and type in the code yourself.
- Each chapter in the web version of this book features a discussion forum, and you are welcome to share your questions and insights at any time.