feat(Go-Tool):2021/2/5: 新增二分查找区间内数值方法
This commit is contained in:
@@ -40,3 +40,66 @@ func TestBinarySearch(t *testing.T) {
|
||||
position = BinarySearch(oddNumList, -10)
|
||||
fmt.Println(position)
|
||||
}
|
||||
|
||||
func TestBinarySearchBetween(t *testing.T) {
|
||||
|
||||
evenNumList := []int{1, 2, 3, 5, 6, 7, 9, 10, 22, 111, 333, 431}
|
||||
|
||||
for _, r := range evenNumList {
|
||||
position := BinarySearchBetween(evenNumList, r)
|
||||
fmt.Println(position)
|
||||
}
|
||||
|
||||
position := BinarySearchBetween(evenNumList, 999)
|
||||
fmt.Println(position)
|
||||
|
||||
position = BinarySearchBetween(evenNumList, -10)
|
||||
fmt.Println(position)
|
||||
|
||||
position = BinarySearchBetween(evenNumList, 1)
|
||||
fmt.Println(position)
|
||||
|
||||
position = BinarySearchBetween(evenNumList, 3211)
|
||||
fmt.Println(position)
|
||||
|
||||
position = BinarySearchBetween(evenNumList, 334)
|
||||
fmt.Println(position)
|
||||
|
||||
position = BinarySearchBetween(evenNumList, 110)
|
||||
fmt.Println(position)
|
||||
|
||||
position = BinarySearchBetween(evenNumList, 3222)
|
||||
fmt.Println(position)
|
||||
|
||||
fmt.Println()
|
||||
|
||||
oddNumList := []int{1, 2, 3, 5, 6, 7, 9, 10, 22, 111, 333, 431, 3211}
|
||||
for _, r := range oddNumList {
|
||||
position := BinarySearchBetween(oddNumList, r)
|
||||
fmt.Println(position)
|
||||
}
|
||||
|
||||
fmt.Println("测试between")
|
||||
fmt.Println()
|
||||
|
||||
position = BinarySearchBetween(oddNumList, 999)
|
||||
fmt.Println(position)
|
||||
|
||||
position = BinarySearchBetween(oddNumList, -10)
|
||||
fmt.Println(position)
|
||||
|
||||
position = BinarySearchBetween(oddNumList, 1)
|
||||
fmt.Println(position)
|
||||
|
||||
position = BinarySearchBetween(oddNumList, 3211)
|
||||
fmt.Println(position)
|
||||
|
||||
position = BinarySearchBetween(oddNumList, 334)
|
||||
fmt.Println(position)
|
||||
|
||||
position = BinarySearchBetween(oddNumList, 110)
|
||||
fmt.Println(position)
|
||||
|
||||
position = BinarySearchBetween(oddNumList, 3222)
|
||||
fmt.Println(position)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user