mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-23 10:02:23 +08:00
Add the section of binary search insertion. (#671)
Refactor the section of binary search edge. Finetune the figures of binary search.
This commit is contained in:
@@ -1,55 +0,0 @@
|
||||
// File: binary_search_edge.go
|
||||
// Created Time: 2023-05-29
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_searching
|
||||
|
||||
/* 二分查找最左一个元素 */
|
||||
func binarySearchLeftEdge(nums []int, target int) int {
|
||||
// 初始化双闭区间 [0, n-1]
|
||||
i, j := 0, len(nums)-1
|
||||
for i <= j {
|
||||
// 计算中点索引 m
|
||||
m := i + (j-i)/2
|
||||
if nums[m] < target {
|
||||
// target 在区间 [m+1, j] 中
|
||||
i = m + 1
|
||||
} else if nums[m] > target {
|
||||
// target 在区间 [i, m-1] 中
|
||||
j = m - 1
|
||||
} else {
|
||||
// 首个小于 target 的元素在区间 [i, m-1] 中
|
||||
j = m - 1
|
||||
}
|
||||
}
|
||||
if i == len(nums) || nums[i] != target {
|
||||
// 未找到目标元素,返回 -1
|
||||
return -1
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
/* 二分查找最右一个元素 */
|
||||
func binarySearchRightEdge(nums []int, target int) int {
|
||||
// 初始化双闭区间 [0, n-1]
|
||||
i, j := 0, len(nums)-1
|
||||
for i <= j {
|
||||
// 计算中点索引 m
|
||||
m := i + (j-i)/2
|
||||
if nums[m] < target {
|
||||
// target 在区间 [m+1, j] 中
|
||||
i = m + 1
|
||||
} else if nums[m] > target {
|
||||
// target 在区间 [i, m-1] 中
|
||||
j = m - 1
|
||||
} else {
|
||||
// 首个大于 target 的元素在区间 [m+1, j] 中
|
||||
i = m + 1
|
||||
}
|
||||
}
|
||||
if j < 0 || nums[j] != target {
|
||||
// 未找到目标元素,返回 -1
|
||||
return -1
|
||||
}
|
||||
return j
|
||||
}
|
||||
@@ -22,15 +22,3 @@ func TestBinarySearch(t *testing.T) {
|
||||
t.Errorf("目标元素 6 的索引 = %d, 应该为 %d", actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBinarySearchEdge(t *testing.T) {
|
||||
target := 6
|
||||
nums := []int{1, 3, 6, 6, 6, 6, 6, 10, 12, 15}
|
||||
// 二分查找最左一个元素
|
||||
indexLeft := binarySearchLeftEdge(nums, target)
|
||||
fmt.Println("数组中最左一个元素 6 的索引 = ", indexLeft)
|
||||
|
||||
// 二分查找最右一个元素
|
||||
indexRight := binarySearchRightEdge(nums, target)
|
||||
fmt.Println("数组中最右一个元素 6 的索引 = ", indexRight)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user