feat: Add Go code to binary search recursion under divide and conquer (#630)
* feat: Add Go code to binary search recursion under divide and conquer * style: Code comment standardization * style: modify function comment
This commit is contained in:
parent
30d1d36dd7
commit
4e4a13e1bb
34
codes/go/chapter_divide_and_conquer/binary_search_recur.go
Normal file
34
codes/go/chapter_divide_and_conquer/binary_search_recur.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
// File: binary_search_recur.go
|
||||||
|
// Created Time: 2023-07-19
|
||||||
|
// Author: hongyun-robot (1836017030@qq.com)
|
||||||
|
|
||||||
|
package chapter_divide_and_conquer
|
||||||
|
|
||||||
|
/* 二分查找:问题 f(i, j) */
|
||||||
|
func dfs(nums []int, target, i, j int) int {
|
||||||
|
// 如果区间为空,代表没有目标元素,则返回 -1
|
||||||
|
if i > j {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
// 计算索引中点
|
||||||
|
m := i + ((j - i) >> 1)
|
||||||
|
//判断中点与目标元素大小
|
||||||
|
if nums[m] < target {
|
||||||
|
// 小于则递归右半数组
|
||||||
|
// 递归子问题 f(m+1, j)
|
||||||
|
return dfs(nums, target, m+1, j)
|
||||||
|
} else if nums[m] > target {
|
||||||
|
// 小于则递归左半数组
|
||||||
|
// 递归子问题 f(i, m-1)
|
||||||
|
return dfs(nums, target, i, m-1)
|
||||||
|
} else {
|
||||||
|
// 找到目标元素,返回其索引
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 二分查找 */
|
||||||
|
func binarySearch(nums []int, target int) int {
|
||||||
|
n := len(nums)
|
||||||
|
return dfs(nums, target, 0, n-1)
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
// File: binary_search_recur_test.go
|
||||||
|
// Created Time: 2023-07-19
|
||||||
|
// Author: hongyun-robot (1836017030@qq.com)
|
||||||
|
|
||||||
|
package chapter_divide_and_conquer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBinarySearch(t *testing.T) {
|
||||||
|
nums := []int{1, 3, 6, 8, 12, 15, 23, 26, 31, 35}
|
||||||
|
target := 6
|
||||||
|
noTarget := 99
|
||||||
|
targetIndex := binarySearch(nums, target)
|
||||||
|
fmt.Println("目标元素 6 的索引 = ", targetIndex)
|
||||||
|
noTargetIndex := binarySearch(nums, noTarget)
|
||||||
|
fmt.Println("不存在目标元素的索引 = ", noTargetIndex)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user