mirror of
https://github.com/krahets/hello-algo.git
synced 2025-08-02 19:53:11 +08:00
Refactor the articles related to searching algorithm. Add the chapter of binary search. Add the section of searching algorithm revisited. (#464)
This commit is contained in:
24
codes/go/chapter_binary_search/binary_search_test.go
Normal file
24
codes/go/chapter_binary_search/binary_search_test.go
Normal file
@ -0,0 +1,24 @@
|
||||
// File: binary_search_test.go
|
||||
// Created Time: 2022-12-05
|
||||
// Author: Slone123c (274325721@qq.com)
|
||||
|
||||
package chapter_binary_search
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBinarySearch(t *testing.T) {
|
||||
var (
|
||||
target = 3
|
||||
nums = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
expected = 2
|
||||
)
|
||||
// 在数组中执行二分查找
|
||||
actual := binarySearch(nums, target)
|
||||
fmt.Println("目标元素 3 的索引 =", actual)
|
||||
if actual != expected {
|
||||
t.Errorf("目标元素 3 的索引 = %d, 应该为 %d", actual, expected)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user