-
Notifications
You must be signed in to change notification settings - Fork 14.5k
translation: update binary_search_recur.md #1663
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
base: main
Are you sure you want to change the base?
Changes from all commits
5f77ed3
72c7809
6abf0fb
3e44192
76be14b
d11b140
ed17b6f
b6cfce6
f43022f
99f7dac
2060ba5
efc34c0
a74e3a5
ee9dc5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,40 @@ | ||
| # Divide and conquer search strategy | ||
|
|
||
| We have learned that search algorithms fall into two main categories. | ||
| We have learned from the [chapter 10.5](../chapter_searching/searching_algorithm_revisited.md) that search algorithms fall into two main categories. | ||
|
|
||
| - **Brute-force search**: It is implemented by traversing the data structure, with a time complexity of $O(n)$. | ||
| - **Adaptive search**: It utilizes a unique data organization form or prior information, and its time complexity can reach $O(\log n)$ or even $O(1)$. | ||
| - **Brute-force search**: It is implemented by traversing every element of a data structure, with a time complexity of $O(n)$. | ||
| - **Adaptive search**: It utilizes a unique data structure or prior information, with a time complexity of $O(\log n)$ or even $O(1)$. | ||
|
|
||
| In fact, **search algorithms with a time complexity of $O(\log n)$ are usually based on the divide-and-conquer strategy**, such as binary search and trees. | ||
| In fact, **search algorithms with a time complexity of $O(\log n)$ are typically based on the divide-and-conquer strategy**, such as binary search and tree. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be "trees" instead of "tree" because we are not referring to a specific one. |
||
|
|
||
| - Each step of binary search divides the problem (searching for a target element in an array) into a smaller problem (searching for the target element in half of the array), continuing until the array is empty or the target element is found. | ||
| - Trees represent the divide-and-conquer idea, where in data structures like binary search trees, AVL trees, and heaps, the time complexity of various operations is $O(\log n)$. | ||
| - Binary search divides the original problem (searching for a target element in an array) into a smaller subproblem (searching for a target element in half of the array) at each step. This process continues until the array is empty or the target element is found. | ||
| - Trees are a classic example of the divide-and-conquer paradigm. In data structures such as binary search trees, AVL trees, and heaps, the time complexity of operations is $O(\log n)$. | ||
|
|
||
| The divide-and-conquer strategy of binary search is as follows. | ||
| The divide-and-conquer strategy of binary search is characterized as follows. | ||
|
|
||
| - **The problem can be divided**: Binary search recursively divides the original problem (searching in an array) into subproblems (searching in half of the array), achieved by comparing the middle element with the target element. | ||
| - **Subproblems are independent**: In binary search, each round handles one subproblem, unaffected by other subproblems. | ||
| - **The solutions of subproblems do not need to be merged**: Binary search aims to find a specific element, so there is no need to merge the solutions of subproblems. When a subproblem is solved, the original problem is also solved. | ||
| - **Dividing the problem**: Binary search recursively divides the original problem (searching in the full array) into subproblems (searching in half of the array) by comparing the middle element with the target element. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Dividing the problem" -> "Dividable problem", this aligns better with the other headers |
||
| - **Independence of subproblems**: Each round of binary search handle only one subproblem, unaffected by other subproblems. | ||
| - **No subproblem solution merging**: Binary search aims to find a specific element, so there is no need to merge the solutions of subproblems. Solving the subproblem inherently solves the original problem. | ||
|
|
||
| Divide-and-conquer can enhance search efficiency because brute-force search can only eliminate one option per round, **whereas divide-and-conquer can eliminate half of the options**. | ||
| Divide-and-conquer improves search efficiency. This is because, in brute-force search, only one option can be eliminated per round. In contrast, **divide-and-conquer search eliminates half of the options in each round**. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "This is because, in brute-force search" -> "Since in brute-force search" might be more concise. |
||
|
|
||
| ### Implementing binary search based on divide-and-conquer | ||
|
|
||
| In previous chapters, binary search was implemented based on iteration. Now, we implement it based on divide-and-conquer (recursion). | ||
| In previous chapters, binary search was implemented iteratively. Here, we will implement it using a divide-and-conquer (recursive) approach. | ||
|
|
||
| !!! question | ||
|
|
||
| Given an ordered array `nums` of length $n$, where all elements are unique, please find the element `target`. | ||
| Given a sorted array `nums` of length $n$ with unique elements, find the index of element `target`. | ||
|
|
||
| From a divide-and-conquer perspective, we denote the subproblem corresponding to the search interval $[i, j]$ as $f(i, j)$. | ||
| From a divide-and-conquer perspective, we define $f(i, j)$ as the subproblem of the search interval $[i, j]$. | ||
|
|
||
| Starting from the original problem $f(0, n-1)$, perform the binary search through the following steps. | ||
| Starting from the original problem $f(0, n-1)$, the binary search proceeds as follows. | ||
|
|
||
| 1. Calculate the midpoint $m$ of the search interval $[i, j]$, and use it to eliminate half of the search interval. | ||
| 2. Recursively solve the subproblem reduced by half in size, which could be $f(i, m-1)$ or $f(m+1, j)$. | ||
| 3. Repeat steps `1.` and `2.`, until `target` is found or the interval is empty and returns. | ||
| 1. Calculate the midpoint $m$ of the search interval $[i, j]$ to eliminate half of the search space. | ||
| 2. Recursively solve the subproblem on the reduced interval, either $f(i, m-1)$ or $f(m+1, j)$. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "reduced interval" -> "reduced search space" keep consistent with previous wording |
||
| 3. Repeat steps `1.` and `2.`, until `target` is found or the interval becomes empty. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, replace interval with search space. |
||
|
|
||
| The figure below shows the divide-and-conquer process of binary search for element $6$ in an array. | ||
| The figure below shows the divide-and-conquer process of performing binary search for element $6$ in a sorted array. | ||
|
|
||
|  | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this reference is necessary?