Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 40158e2

Browse files
committed
Update 01.Array-Binary-Search-01.md
1 parent 14a4b28 commit 40158e2

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

‎Contents/01.Array/03.Array-Binary-Search/01.Array-Binary-Search-01.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222

2323
举个例子来说,以在有序数组 $[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]$ 中查找目标元素 6ドル$ 来说,使用二分查找算法的步骤如下:
2424

25-
1. **确定查找范围**:初始时左边界 $left$ 为 $0$(数组的起始位置),$right$ 为 $10$(数组的末尾位置)。此时查找范围为 $[0, 10]$。
26-
2. **计算中间元素**:中间元素下标位置为 $5,ドル对应元素为 $nums[5] == 5$。
27-
3. **比较中间元素**:因为 6ドル > nums[5],ドル所以目标元素可能在右半部分,更新左边界为中间元素的后一个位置,即 $left = 5$。此时查找范围为 $[5, 10]$。
28-
4. **计算中间元素**:中间元素下标位置为 7ドル,ドル对应元素为 $nums[7] == 7$。
29-
5. **比较中间元素**:因为 6ドル < nums[7],ドル所以目标元素可能在左半部分,更新右边界为中间元素的前一个位置,即 $right = 6$。此时查找范围为 $[5, 6]$。
30-
6. **计算中间元素**:中间元素下标位置为 5ドル$,对应元素为 $nums[5] == 5$。
31-
7. **比较中间元素**:因为 $5 == nums[5],ドル正好是我们正在查找的目标元素,此时返回中间元素的下标位置,算法结束。
25+
1. **确定查找范围**:初始时左边界 $left = 0$(数组的起始位置),$right = 10$(数组的末尾位置)。此时查找范围为 $[0, 10]$。
26+
2. **计算中间元素**:中间元素下标位置 $mid = (0 + 10) \div 2 = 5,ドル对应元素为 $nums[5] == 5$。
27+
3. **比较中间元素**:因为 6ドル > nums[5],ドル所以目标元素可能在右半部分,更新左边界为中间元素的后一个位置,即 $left = 6$。此时查找范围为 $[6, 10]$。
28+
4. **计算中间元素**:中间元素下标位置 $mid = (6 + 10) \div 2 = 8,ドル对应元素为 $nums[8] == 8$。
29+
5. **比较中间元素**:因为 6ドル < nums[8],ドル所以目标元素可能在左半部分,更新右边界为中间元素的前一个位置,即 $right = 7$。此时查找范围为 $[6, 7]$。
30+
6. **计算中间元素**:中间元素下标位置 $mid = (6 + 7) \div 2 = 6$(向下取整),对应元素为 $nums[6] == 6$。
31+
7. **比较中间元素**:因为 $6 == nums[6],ドル正好是我们正在查找的目标元素,此时返回中间元素的下标位置,算法结束。
3232

3333
于是我们发现,对于一个长度为 10ドル$ 的有序数组,我们只进行了 3ドル$ 次查找就找到了目标元素。而如果是按照顺序依次遍历数组,则在最坏情况下,我们可能需要查找 10ドル$ 次才能找到目标元素。
3434

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /