Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

translation: Update binary_search_insertion.md #1554

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
36 changes: 18 additions & 18 deletions en/docs/chapter_searching/binary_search_insertion.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ Binary search is not only used to search for target elements but also to solve m

!!! question

Given an ordered array `nums` of length $n$ and an element `target`, where the array has no duplicate elements. Now insert `target` into the array `nums` while maintaining its order. If the element `target` already exists in the array, insert it to its left side. Please return the index of `target` in the array after insertion. See the example shown in the figure below.
Given a sorted array `nums` of length $n$ with unique elements and an element `target`. Now insert `target` into `nums` while maintaining its sorted order. If `target` already exists in the array, insert it to the left of the existing element. Please return the index of `target` in the array after insertion. See the example shown in the figure below.
yanedie marked this conversation as resolved.
Show resolved Hide resolved

![Example data for binary search insertion point](binary_search_insertion.assets/binary_search_insertion_example.png)

If you want to reuse the binary search code from the previous section, you need to answer the following two questions.

**Question one**: When the array contains `target`, is the insertion point index the index of that element?
**Question one**: If the array already contains `target`, would the insertion point be the index of existing element?

The requirement to insert `target` to the left of equal elements means that the newly inserted `target` replaces the original `target` position. Thus, **when the array contains `target`, the insertion point index is the index of that `target`**.
The requirement to insert `target` to the left of equal elements means that the newly inserted `target` will replace the original `target` position. In other words, **when the array contains `target`, the insertion point is indeed the index of that `target`**.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"In other words" may be some colloquialisms

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK


**Question two**: When the array does not contain `target`, what is the index of the insertion point?
**Question two**: When the array does not contain `target`, at which index would it be inserted?

Further consider the binary search process: when `nums[m] < target`, pointer $i$ moves, meaning that pointer $i$ is approaching an element greater than or equal to `target`. Similarly, pointer $j$ is always approaching an element less than or equal to `target`.
Let's further consider the binary search process: when `nums[m] < target`, pointer $i$ moves, meaning that pointer $i$ is approaching an element greater than or equal to `target`. Similarly, pointer $j$ is always approaching an element less than or equal to `target`.

Therefore, at the end of the binary, it is certain that: $i$ points to the first element greater than `target`, and $j$ points to the first element less than `target`. **It is easy to see that when the array does not contain `target`, the insertion index is $i$**. The code is as follows:
Therefore, at the end of the binary, it is certain that: $i$ points to the first element greater than `target`, and $j$ points to the first element less than `target`. **It is easy to see that when the array does not contain `target`, the insertion point is $i$**. The code is as follows:

```src
[file]{binary_search_insertion}-[class]{}-[func]{binary_search_insertion_simple}
Expand All @@ -32,21 +32,21 @@ Therefore, at the end of the binary, it is certain that: $i$ points to the first

Based on the previous question, assume the array may contain duplicate elements, all else remains the same.

Suppose there are multiple `target`s in the array, ordinary binary search can only return the index of one of the `target`s, **and it cannot determine how many `target`s are to the left and right of that element**.
Suppose there are multiple `target`s in the array, a regular binary search can only return the index of one `target`, **and it cannot determine how many `target`s are to the left and right of that it**.
yanedie marked this conversation as resolved.
Show resolved Hide resolved

The task requires inserting the target element to the very left, **so we need to find the index of the leftmost `target` in the array**. Initially consider implementing this through the steps shown in the figure below.
The problem requires inserting the target element to the very left, **so we need to find the index of the leftmost `target` in the array**. Initially consider implementing this through the steps shown in the figure below.

1. Perform a binary search, get an arbitrary index of `target`, denoted as $k$.
2. Start from index $k$, and perform a linear search to the left until the leftmost `target` is found and return.
1. Perform a binary search to find any `target`'s index, say $k$.
yanedie marked this conversation as resolved.
Show resolved Hide resolved
2. Starting from index $k$, perform a linear search to the left until the leftmost `target` is found and return.
yanedie marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tenses of the two paragraphs are inconsistent. Perform and Starting

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Starting from index $k$" is a present participial phrase. Their tenses are the same.


![Linear search for the insertion point of duplicate elements](binary_search_insertion.assets/binary_search_insertion_naive.png)

Although this method is feasible, it includes linear search, so its time complexity is $O(n)$. This method is inefficient when the array contains many duplicate `target`s.

Now consider extending the binary search code. As shown in the figure below, the overall process remains the same, each round first calculates the midpoint index $m$, then judges the size relationship between `target` and `nums[m]`, divided into the following cases.
Now consider extending the binary search code. As shown in the figure below, the overall process remains the same. In each round, we first calculate the middle index $m$, then compare the value of `target` and `nums[m]`, which results in the following cases.
yanedie marked this conversation as resolved.
Show resolved Hide resolved

- When `nums[m] < target` or `nums[m] > target`, it means `target` has not been found yet, thus use the normal binary search interval reduction operation, **thus making pointers $i$ and $j$ approach `target`**.
- When `nums[m] == target`, it indicates that the elements less than `target` are in the interval $[i, m - 1]$, therefore use $j = m - 1$ to narrow the interval, **thus making pointer $j$ approach elements less than `target`**.
- When `nums[m] < target` or `nums[m] > target`, it means `target` has not been found yet, thus use the normal binary search to narrow the search range, **bring the pointers $i$ and $j$ closer to `target`**.
- When `nums[m] == target`, it indicates that the elements less than `target` are in the range $[i, m - 1]$, therefore use $j = m - 1$ to narrow the range, **thus making pointer $j$ closer to the elements less than `target`**.
yanedie marked this conversation as resolved.
Show resolved Hide resolved

After the loop, $i$ points to the leftmost `target`, and $j$ points to the first element less than `target`, **therefore index $i$ is the insertion point**.

Expand Down Expand Up @@ -74,18 +74,18 @@ After the loop, $i$ points to the leftmost `target`, and $j$ points to the first
=== "<8>"
![binary_search_insertion_step8](binary_search_insertion.assets/binary_search_insertion_step8.png)

Observe the code, the operations of the branch `nums[m] > target` and `nums[m] == target` are the same, so the two can be combined.
Observe the following code. The operations in the branches `nums[m] > target` and `nums[m] == target` are the same, so these two branches can be merged.

Even so, we can still keep the conditions expanded, as their logic is clearer and more readable.
Even so, we can still keep the conditions expanded, as it makes the logic clearer and improves readability.

```src
[file]{binary_search_insertion}-[class]{}-[func]{binary_search_insertion}
```

!!! tip

The code in this section uses "closed intervals". Readers interested can implement the "left-closed right-open" method themselves.
The code in this section uses "closed interval". If you are interested in "left-closed,right-open", try to implement the code on your own.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Readers who are interested can implement the ‘left-closed, right-open’ method themselves.” may be better, you can consider about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use "You" for a more conversational and engaging style. This aligns with the goal of guiding the user as if the documentation is speaking directly to them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you said makes sense. Maybe we need to confirm other chapters. It would be best if each chapter can use the same standard.


In summary, binary search is merely about setting search targets for pointers $i$ and $j$, which might be a specific element (like `target`) or a range of elements (like elements less than `target`).
In summary, binary search essentially involves setting search targets for pointers $i$ and $j$, which might be a specific element (like `target`) or a range of elements (like elements less than `target`).

In the continuous loop of binary search, pointers $i$ and $j$ gradually approach the predefined target. Ultimately, they either find the answer or stop after crossing the boundary.
In the continuous loop of binary search, pointers $i$ and $j$ gradually approach the predefined target. In the end, they either find the answer or stop after crossing the boundary.
yanedie marked this conversation as resolved.
Show resolved Hide resolved