mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-27 02:27:12 +00:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
# Binary search
|
||||
# Binary Search
|
||||
|
||||
<u>Binary search</u> is an efficient searching algorithm based on the divide-and-conquer strategy. It leverages the orderliness of data to reduce the search range by half in each round until the target element is found or the search interval becomes empty.
|
||||
|
||||
@@ -53,7 +53,7 @@ The code is shown below:
|
||||
|
||||
**Space complexity is $O(1)$**: Pointers $i$ and $j$ use constant-size space.
|
||||
|
||||
## Interval representation methods
|
||||
## Interval Representation Methods
|
||||
|
||||
In addition to the closed interval mentioned above, another common interval representation is the "left-closed right-open" interval, defined as $[0, n)$, meaning the left boundary includes itself while the right boundary does not. Under this representation, the interval $[i, j)$ is empty when $i = j$.
|
||||
|
||||
@@ -69,7 +69,7 @@ Since both the left and right boundaries in the "closed interval" representation
|
||||
|
||||

|
||||
|
||||
## Advantages and limitations
|
||||
## Advantages and Limitations
|
||||
|
||||
Binary search performs well in both time and space aspects.
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Binary search edge cases
|
||||
# Binary Search Edge Cases
|
||||
|
||||
## Finding the left boundary
|
||||
## Finding the Left Boundary
|
||||
|
||||
!!! question
|
||||
|
||||
@@ -19,13 +19,13 @@ When either of these situations occurs, simply return $-1$. The code is shown be
|
||||
[file]{binary_search_edge}-[class]{}-[func]{binary_search_left_edge}
|
||||
```
|
||||
|
||||
## Finding the right boundary
|
||||
## Finding the Right Boundary
|
||||
|
||||
So how do we find the rightmost `target`? The most direct approach is to modify the code and replace the pointer shrinking operation in the `nums[m] == target` case. The code is omitted here; interested readers can implement it themselves.
|
||||
|
||||
Below we introduce two more clever methods.
|
||||
|
||||
### Reusing left boundary search
|
||||
### Reusing Left Boundary Search
|
||||
|
||||
In fact, we can use the function for finding the leftmost element to find the rightmost element. The specific method is: **Convert finding the rightmost `target` into finding the leftmost `target + 1`**.
|
||||
|
||||
@@ -39,7 +39,7 @@ Note that the returned insertion point is $i$, so we need to subtract $1$ from i
|
||||
[file]{binary_search_edge}-[class]{}-[func]{binary_search_right_edge}
|
||||
```
|
||||
|
||||
### Converting to element search
|
||||
### Converting to Element Search
|
||||
|
||||
We know that when the array does not contain `target`, $i$ and $j$ will eventually point to the first elements greater than and less than `target`, respectively.
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
# Binary search insertion point
|
||||
# Binary Search Insertion Point
|
||||
|
||||
Binary search can not only be used to search for target elements but also to solve many variant problems, such as searching for the insertion position of a target element.
|
||||
|
||||
## Case without duplicate elements
|
||||
## Case Without Duplicate Elements
|
||||
|
||||
!!! question
|
||||
|
||||
@@ -26,7 +26,7 @@ Therefore, when the binary search ends, we must have: $i$ points to the first el
|
||||
[file]{binary_search_insertion}-[class]{}-[func]{binary_search_insertion_simple}
|
||||
```
|
||||
|
||||
## Case with duplicate elements
|
||||
## Case with Duplicate Elements
|
||||
|
||||
!!! question
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Hash optimization strategy
|
||||
# Hash Optimization Strategy
|
||||
|
||||
In algorithm problems, **we often reduce the time complexity of algorithms by replacing linear search with hash-based search**. Let's use an algorithm problem to deepen our understanding.
|
||||
|
||||
@@ -6,7 +6,7 @@ In algorithm problems, **we often reduce the time complexity of algorithms by re
|
||||
|
||||
Given an integer array `nums` and a target element `target`, search for two elements in the array whose "sum" equals `target`, and return their array indices. Any solution will do.
|
||||
|
||||
## Linear search: trading time for space
|
||||
## Linear Search: Trading Time for Space
|
||||
|
||||
Consider directly traversing all possible combinations. As shown in the figure below, we open a two-layer loop and judge in each round whether the sum of two integers equals `target`. If so, return their indices.
|
||||
|
||||
@@ -20,7 +20,7 @@ The code is shown below:
|
||||
|
||||
This method has a time complexity of $O(n^2)$ and a space complexity of $O(1)$, which is very time-consuming with large data volumes.
|
||||
|
||||
## Hash-based search: trading space for time
|
||||
## Hash-Based Search: Trading Space for Time
|
||||
|
||||
Consider using a hash table where key-value pairs are array elements and element indices respectively. Loop through the array, performing the steps shown in the figure below in each round:
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Searching algorithms revisited
|
||||
# Searching Algorithms Revisited
|
||||
|
||||
<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).
|
||||
|
||||
@@ -9,7 +9,7 @@ Searching algorithms can be divided into the following two categories based on t
|
||||
|
||||
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
|
||||
## Brute-Force Search
|
||||
|
||||
Brute-force search locates target elements by traversing each element of the data structure.
|
||||
|
||||
@@ -20,7 +20,7 @@ The advantage of brute-force search is that it is simple and has good generality
|
||||
|
||||
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
|
||||
## Adaptive Search
|
||||
|
||||
Adaptive search utilizes the unique properties of data (such as orderliness) to optimize the search process, thereby locating target elements more efficiently.
|
||||
|
||||
@@ -36,7 +36,7 @@ However, **using these algorithms often requires data preprocessing**. For examp
|
||||
|
||||
Adaptive search algorithms are often called lookup algorithms, **mainly used to quickly retrieve target elements in specific data structures**.
|
||||
|
||||
## Search method selection
|
||||
## Search Method Selection
|
||||
|
||||
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.
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
# Summary
|
||||
|
||||
### Key Review
|
||||
|
||||
- Binary search relies on data orderliness and progressively reduces the search interval by half through loops. It requires input data to be sorted and is only applicable to arrays or data structures based on array implementations.
|
||||
- Brute-force search locates data by traversing the data structure. Linear search is applicable to arrays and linked lists, while breadth-first search and depth-first search are applicable to graphs and trees. Such algorithms have good generality and require no data preprocessing, but have a relatively high time complexity of $O(n)$.
|
||||
- Hash-based search, tree search, and binary search are efficient search methods that can quickly locate target elements in specific data structures. Such algorithms are highly efficient with time complexity reaching $O(\log n)$ or even $O(1)$, but typically require additional data structures.
|
||||
|
||||
Reference in New Issue
Block a user