mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-17 06:00:58 +00: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,84 +1,84 @@
|
||||
# Search algorithms revisited
|
||||
# Searching algorithms revisited
|
||||
|
||||
<u>Searching algorithms (search algorithms)</u> are used to retrieve one or more elements that meet specific criteria within data structures such as arrays, linked lists, trees, or graphs.
|
||||
<u>Searching algorithms</u> are used to search for one or a group of elements that meet specific conditions in data structures (such as arrays, linked lists, trees, or graphs).
|
||||
|
||||
Searching algorithms can be divided into the following two categories based on their approach.
|
||||
Searching algorithms can be divided into the following two categories based on their implementation approach:
|
||||
|
||||
- **Locating the target element by traversing the data structure**, such as traversals of arrays, linked lists, trees, and graphs, etc.
|
||||
- **Using the organizational structure of the data or existing data to achieve efficient element searches**, such as binary search, hash search, binary search tree search, etc.
|
||||
- **Locating target elements by traversing the data structure**, such as traversing arrays, linked lists, trees, and graphs.
|
||||
- **Achieving efficient element search by utilizing data organization structure or prior information contained in the data**, such as binary search, hash-based search, and binary search tree search.
|
||||
|
||||
These topics were introduced in previous chapters, so they are not unfamiliar to us. In this section, we will revisit searching algorithms from a more systematic perspective.
|
||||
It's not hard to see that these topics have all been covered in previous chapters, so searching algorithms are not unfamiliar to us. In this section, we will approach from a more systematic perspective and re-examine searching algorithms.
|
||||
|
||||
## Brute-force search
|
||||
|
||||
A Brute-force search locates the target element by traversing every element of the data structure.
|
||||
Brute-force search locates target elements by traversing each element of the data structure.
|
||||
|
||||
- "Linear search" is suitable for linear data structures such as arrays and linked lists. It starts from one end of the data structure and accesses each element one by one until the target element is found or the other end is reached without finding the target element.
|
||||
- "Breadth-first search" and "Depth-first search" are two traversal strategies for graphs and trees. Breadth-first search starts from the initial node and searches layer by layer (left to right), accessing nodes from near to far. Depth-first search starts from the initial node, follows a path until the end (top to bottom), then backtracks and tries other paths until the entire data structure is traversed.
|
||||
- "Linear search" is applicable to linear data structures such as arrays and linked lists. It starts from one end of the data structure and accesses elements one by one until the target element is found or the other end is reached without finding the target element.
|
||||
- "Breadth-first search" and "depth-first search" are two traversal strategies for graphs and trees. Breadth-first search starts from the initial node and searches layer by layer, visiting nodes from near to far. Depth-first search starts from the initial node, follows a path to the end, then backtracks and tries other paths until the entire data structure is traversed.
|
||||
|
||||
The advantage of brute-force search is its simplicity and versatility, **no need for data preprocessing or the help of additional data structures**.
|
||||
The advantage of brute-force search is that it is simple and has good generality, **requiring no data preprocessing or additional data structures**.
|
||||
|
||||
However, **the time complexity of this type of algorithm is $O(n)$**, where $n$ is the number of elements, so the performance is poor with large data sets.
|
||||
However, **the time complexity of such algorithms is $O(n)$**, where $n$ is the number of elements, so performance is poor when dealing with large amounts of data.
|
||||
|
||||
## Adaptive search
|
||||
|
||||
An Adaptive search uses the unique properties of data (such as order) to optimize the search process, thereby locating the target element more efficiently.
|
||||
Adaptive search utilizes the unique properties of data (such as orderliness) to optimize the search process, thereby locating target elements more efficiently.
|
||||
|
||||
- "Binary search" uses the orderliness of data to achieve efficient searching, only suitable for arrays.
|
||||
- "Hash search" uses a hash table to establish a key-value mapping between search data and target data, thus implementing the query operation.
|
||||
- "Tree search" in a specific tree structure (such as a binary search tree), quickly eliminates nodes based on node value comparisons, thus locating the target element.
|
||||
- "Binary search" uses the orderliness of data to achieve efficient searching, applicable only to arrays.
|
||||
- "Hash-based search" uses hash tables to establish key-value pair mappings between search data and target data, thereby achieving query operations.
|
||||
- "Tree search" in specific tree structures (such as binary search trees), quickly eliminates nodes based on comparing node values to locate target elements.
|
||||
|
||||
The advantage of these algorithms is high efficiency, **with time complexities reaching $O(\log n)$ or even $O(1)$**.
|
||||
The advantage of such algorithms is high efficiency, **with time complexity reaching $O(\log n)$ or even $O(1)$**.
|
||||
|
||||
However, **using these algorithms often requires data preprocessing**. For example, binary search requires sorting the array in advance, and hash search and tree search both require the help of additional data structures. Maintaining these structures also requires more overhead in terms of time and space.
|
||||
However, **using these algorithms often requires data preprocessing**. For example, binary search requires pre-sorting the array, while hash-based search and tree search both require additional data structures, and maintaining these data structures also requires extra time and space overhead.
|
||||
|
||||
!!! tip
|
||||
|
||||
Adaptive search algorithms are often referred to as search algorithms, **mainly used for quickly retrieving target elements in specific data structures**.
|
||||
Adaptive search algorithms are often called lookup algorithms, **mainly used to quickly retrieve target elements in specific data structures**.
|
||||
|
||||
## Choosing a search method
|
||||
## Search method selection
|
||||
|
||||
Given a set of data of size $n$, we can use a linear search, binary search, tree search, hash search, or other methods to retrieve the target element. The working principles of these methods are shown in the figure below.
|
||||
Given a dataset of size $n$, we can use linear search, binary search, tree search, hash-based search, and other methods to search for the target element. The working principles of each method are shown in the figure below.
|
||||
|
||||

|
||||

|
||||
|
||||
The characteristics and operational efficiency of the aforementioned methods are shown in the following table.
|
||||
The operational efficiency and characteristics of the above methods are as follows:
|
||||
|
||||
<p align="center"> Table <id> Comparison of search algorithm efficiency </p>
|
||||
|
||||
| | Linear search | Binary search | Tree search | Hash search |
|
||||
| | Linear search | Binary search | Tree search | Hash-based search |
|
||||
| ------------------ | ------------- | --------------------- | --------------------------- | -------------------------- |
|
||||
| Search element | $O(n)$ | $O(\log n)$ | $O(\log n)$ | $O(1)$ |
|
||||
| Insert element | $O(1)$ | $O(n)$ | $O(\log n)$ | $O(1)$ |
|
||||
| Delete element | $O(n)$ | $O(n)$ | $O(\log n)$ | $O(1)$ |
|
||||
| Extra space | $O(1)$ | $O(1)$ | $O(n)$ | $O(n)$ |
|
||||
| Data preprocessing | / | Sorting $O(n \log n)$ | Building tree $O(n \log n)$ | Building hash table $O(n)$ |
|
||||
| Data orderliness | Unordered | Ordered | Ordered | Unordered |
|
||||
| Data preprocessing | / | Sorting $O(n \log n)$ | Tree building $O(n \log n)$ | Hash table building $O(n)$ |
|
||||
| Data ordered | Unordered | Ordered | Ordered | Unordered |
|
||||
|
||||
The choice of search algorithm also depends on the volume of data, search performance requirements, frequency of data queries and updates, etc.
|
||||
The choice of search algorithm also depends on data volume, search performance requirements, data query and update frequency, etc.
|
||||
|
||||
**Linear search**
|
||||
|
||||
- Good versatility, no need for any data preprocessing operations. If we only need to query the data once, then the time for data preprocessing in the other three methods would be longer than the time for a linear search.
|
||||
- Suitable for small volumes of data, where time complexity has a smaller impact on efficiency.
|
||||
- Suitable for scenarios with very frequent data updates, because this method does not require any additional maintenance of the data.
|
||||
- Good generality, requiring no data preprocessing operations. If we only need to query the data once, the data preprocessing time for the other three methods would be longer than linear search.
|
||||
- Suitable for small data volumes, where time complexity has less impact on efficiency.
|
||||
- Suitable for scenarios with high data update frequency, as this method does not require any additional data maintenance.
|
||||
|
||||
**Binary search**
|
||||
|
||||
- Suitable for larger data volumes, with stable performance and a worst-case time complexity of $O(\log n)$.
|
||||
- However, the data volume cannot be too large, because storing arrays requires contiguous memory space.
|
||||
- Not suitable for scenarios with frequent additions and deletions, because maintaining an ordered array incurs a lot of overhead.
|
||||
- Suitable for large data volumes with stable efficiency performance, worst-case time complexity of $O(\log n)$.
|
||||
- Data volume cannot be too large, as storing arrays requires contiguous memory space.
|
||||
- Not suitable for scenarios with frequent data insertion and deletion, as maintaining a sorted array has high overhead.
|
||||
|
||||
**Hash search**
|
||||
**Hash-based search**
|
||||
|
||||
- Suitable for scenarios where fast query performance is essential, with an average time complexity of $O(1)$.
|
||||
- Not suitable for scenarios needing ordered data or range searches, because hash tables cannot maintain data orderliness.
|
||||
- High dependency on hash functions and hash collision handling strategies, with significant performance degradation risks.
|
||||
- Not suitable for overly large data volumes, because hash tables need extra space to minimize collisions and provide good query performance.
|
||||
- Suitable for scenarios with high query performance requirements, with an average time complexity of $O(1)$.
|
||||
- Not suitable for scenarios requiring ordered data or range searches, as hash tables cannot maintain data orderliness.
|
||||
- High dependence on hash functions and hash collision handling strategies, with significant risk of performance degradation.
|
||||
- Not suitable for excessively large data volumes, as hash tables require extra space to minimize collisions and thus provide good query performance.
|
||||
|
||||
**Tree search**
|
||||
|
||||
- Suitable for massive data, because tree nodes are stored scattered in memory.
|
||||
- Suitable for maintaining ordered data or range searches.
|
||||
- With the continuous addition and deletion of nodes, the binary search tree may become skewed, degrading the time complexity to $O(n)$.
|
||||
- If using AVL trees or red-black trees, operations can run stably at $O(\log n)$ efficiency, but the operation to maintain tree balance adds extra overhead.
|
||||
- Suitable for massive data, as tree nodes are stored dispersedly in memory.
|
||||
- Suitable for scenarios requiring maintained ordered data or range searches.
|
||||
- During continuous node insertion and deletion, binary search trees may become skewed, degrading time complexity to $O(n)$.
|
||||
- If using AVL trees or red-black trees, all operations can run stably at $O(\log n)$ efficiency, but operations to maintain tree balance add extra overhead.
|
||||
|
||||
Reference in New Issue
Block a user