mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-16 05:30:59 +00:00
build
This commit is contained in:
@@ -7,7 +7,7 @@ comments: true
|
||||
We have already learned that search algorithms are divided into two major categories.
|
||||
|
||||
- **Brute-force search**: Implemented by traversing the data structure, with a time complexity of $O(n)$.
|
||||
- **Adaptive search**: Utilizes unique data organization forms or prior information, with time complexity reaching $O(\log n)$ or even $O(1)$.
|
||||
- **Adaptive search**: Leverages specific data organization or prior information, with time complexity reaching $O(\log n)$ or even $O(1)$.
|
||||
|
||||
In fact, **search algorithms with time complexity of $O(\log n)$ are typically implemented based on the divide and conquer strategy**, such as binary search and trees.
|
||||
|
||||
@@ -28,7 +28,7 @@ In previous sections, binary search was implemented based on iteration. Now we i
|
||||
|
||||
!!! question
|
||||
|
||||
Given a sorted array `nums` of length $n$, where all elements are unique, find the element `target`.
|
||||
Given a sorted array `nums` of length $n$, where all elements are unique, find `target`.
|
||||
|
||||
From a divide and conquer perspective, we denote the subproblem corresponding to the search interval $[i, j]$ as $f(i, j)$.
|
||||
|
||||
@@ -36,7 +36,7 @@ Starting from the original problem $f(0, n-1)$, perform binary search through th
|
||||
|
||||
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 return.
|
||||
3. Repeat steps `1.` and `2.` until `target` is found, or return when the interval is empty.
|
||||
|
||||
Figure 12-4 shows the divide and conquer process of binary search for element $6$ in an array.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user