From d95c628eef637dc509182b3cc2d3c06d7f4d14ec Mon Sep 17 00:00:00 2001 From: krahets Date: Sun, 21 May 2023 19:29:24 +0800 Subject: [PATCH] Fix the test case of binary search. --- codes/c/chapter_searching/binary_search.c | 2 +- codes/cpp/chapter_searching/binary_search.cpp | 2 +- .../csharp/chapter_searching/binary_search.cs | 2 +- .../dart/chapter_searching/binary_search.dart | 2 +- .../chapter_searching/binary_search_test.go | 2 +- .../java/chapter_searching/binary_search.java | 2 +- .../chapter_searching/binary_search.js | 2 +- .../python/chapter_searching/binary_search.py | 2 +- .../binary_search_rotation.py | 24 +++++++++++++++++++ codes/rust/chapter_searching/binary_search.rs | 2 +- .../chapter_searching/binary_search.swift | 2 +- .../chapter_searching/binary_search.ts | 2 +- codes/zig/chapter_searching/binary_search.zig | 2 +- 13 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 codes/python/chapter_searching/binary_search_rotation.py diff --git a/codes/c/chapter_searching/binary_search.c b/codes/c/chapter_searching/binary_search.c index c1407e3af..c21d13404 100644 --- a/codes/c/chapter_searching/binary_search.c +++ b/codes/c/chapter_searching/binary_search.c @@ -45,7 +45,7 @@ int binarySearchLCRO(int *nums, int len, int target) { /* Driver Code */ int main() { int target = 6; - int nums[10] = {1, 3, 6, 8, 12, 15, 23, 67, 70, 92}; + int nums[10] = {1, 3, 6, 8, 12, 15, 23, 26, 31, 35}; /* 二分查找(双闭区间) */ int index = binarySearch(nums, 10, target); diff --git a/codes/cpp/chapter_searching/binary_search.cpp b/codes/cpp/chapter_searching/binary_search.cpp index 7c56649e8..5ddc0ae86 100644 --- a/codes/cpp/chapter_searching/binary_search.cpp +++ b/codes/cpp/chapter_searching/binary_search.cpp @@ -45,7 +45,7 @@ int binarySearchLCRO(vector &nums, int target) { /* Driver Code */ int main() { int target = 6; - vector nums = {1, 3, 6, 8, 12, 15, 23, 67, 70, 92}; + vector nums = {1, 3, 6, 8, 12, 15, 23, 26, 31, 35}; /* 二分查找(双闭区间) */ int index = binarySearch(nums, target); diff --git a/codes/csharp/chapter_searching/binary_search.cs b/codes/csharp/chapter_searching/binary_search.cs index 25e87e73f..948a4adbf 100644 --- a/codes/csharp/chapter_searching/binary_search.cs +++ b/codes/csharp/chapter_searching/binary_search.cs @@ -46,7 +46,7 @@ public class binary_search { [Test] public void Test() { int target = 6; - int[] nums = { 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 }; + int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 }; /* 二分查找(双闭区间) */ int index = binarySearch(nums, target); diff --git a/codes/dart/chapter_searching/binary_search.dart b/codes/dart/chapter_searching/binary_search.dart index 348987f80..fcce4b7a6 100644 --- a/codes/dart/chapter_searching/binary_search.dart +++ b/codes/dart/chapter_searching/binary_search.dart @@ -51,7 +51,7 @@ int binarySearchLCRO(List nums, int target) { /* Driver Code*/ void main() { int target = 6; - final nums = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92]; + final nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; /* 二分查找 (双闭区间) */ int index = binarySearch(nums, target); diff --git a/codes/go/chapter_searching/binary_search_test.go b/codes/go/chapter_searching/binary_search_test.go index 2618f9ec0..0b6084d68 100644 --- a/codes/go/chapter_searching/binary_search_test.go +++ b/codes/go/chapter_searching/binary_search_test.go @@ -12,7 +12,7 @@ import ( func TestBinarySearch(t *testing.T) { var ( target = 6 - nums = []int{1, 3, 6, 8, 12, 15, 23, 67, 70, 92} + nums = []int{1, 3, 6, 8, 12, 15, 23, 26, 31, 35} expected = 2 ) // 在数组中执行二分查找 diff --git a/codes/java/chapter_searching/binary_search.java b/codes/java/chapter_searching/binary_search.java index ba15359c6..5811bf8fc 100644 --- a/codes/java/chapter_searching/binary_search.java +++ b/codes/java/chapter_searching/binary_search.java @@ -45,7 +45,7 @@ public class binary_search { public static void main(String[] args) { int target = 6; - int[] nums = { 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 }; + int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 }; /* 二分查找(双闭区间) */ int index = binarySearch(nums, target); diff --git a/codes/javascript/chapter_searching/binary_search.js b/codes/javascript/chapter_searching/binary_search.js index 8c1224548..0146e2693 100644 --- a/codes/javascript/chapter_searching/binary_search.js +++ b/codes/javascript/chapter_searching/binary_search.js @@ -49,7 +49,7 @@ function binarySearchLCRO(nums, target) { /* Driver Code */ const target = 6; -const nums = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92]; +const nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; /* 二分查找(双闭区间) */ let index = binarySearch(nums, target); diff --git a/codes/python/chapter_searching/binary_search.py b/codes/python/chapter_searching/binary_search.py index 23bb71cee..fb193b8d2 100644 --- a/codes/python/chapter_searching/binary_search.py +++ b/codes/python/chapter_searching/binary_search.py @@ -41,7 +41,7 @@ def binary_search_lcro(nums: list[int], target: int) -> int: """Driver Code""" if __name__ == "__main__": target: int = 6 - nums: list[int] = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92] + nums: list[int] = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35] # 二分查找(双闭区间) index: int = binary_search(nums, target) diff --git a/codes/python/chapter_searching/binary_search_rotation.py b/codes/python/chapter_searching/binary_search_rotation.py new file mode 100644 index 000000000..0d47eb6de --- /dev/null +++ b/codes/python/chapter_searching/binary_search_rotation.py @@ -0,0 +1,24 @@ + + + +def binary_search_rotation(nums: list[int]) -> int: + """二分查找左边界(双闭区间)""" + # 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素 + i, j = 0, len(nums) - 1 + while True: + m = (i + j) // 2 # 计算中点索引 m + if nums[m] < nums[j]: + j = m # 此情况说明 target 在区间 [m+1, j] 中 + elif nums[m] > nums[j]: + i = m + 1 # 此情况说明 target 在区间 [i, m-1] 中 + else: + return i # 此情况说明 i == j == m ,找到旋转点 + + +"""Driver Code""" +if __name__ == "__main__": + nums: list[int] = [23, 67, 70, 92, 1, 3, 6, 8, 12, 15] + + # 二分查找左边界 + index_left = binary_search_rotation(nums) + print("数组旋转点的索引 = ", index_left) diff --git a/codes/rust/chapter_searching/binary_search.rs b/codes/rust/chapter_searching/binary_search.rs index 1c12bd91b..d40e34efe 100644 --- a/codes/rust/chapter_searching/binary_search.rs +++ b/codes/rust/chapter_searching/binary_search.rs @@ -47,7 +47,7 @@ fn binary_search_lcro(nums: &[i32], target: i32) -> i32 { /* Driver Code */ pub fn main() { let target = 6; - let nums = [ 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 ]; + let nums = [ 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 ]; // 二分查找(双闭区间) let mut index = binary_search(&nums, target); diff --git a/codes/swift/chapter_searching/binary_search.swift b/codes/swift/chapter_searching/binary_search.swift index 978979b46..e9c56ad6d 100644 --- a/codes/swift/chapter_searching/binary_search.swift +++ b/codes/swift/chapter_searching/binary_search.swift @@ -49,7 +49,7 @@ enum BinarySearch { /* Driver Code */ static func main() { let target = 6 - let nums = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92] + let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35] /* 二分查找(双闭区间) */ var index = binarySearch(nums: nums, target: target) diff --git a/codes/typescript/chapter_searching/binary_search.ts b/codes/typescript/chapter_searching/binary_search.ts index 34b170e4c..6c2a6e9f3 100644 --- a/codes/typescript/chapter_searching/binary_search.ts +++ b/codes/typescript/chapter_searching/binary_search.ts @@ -52,7 +52,7 @@ function binarySearchLCRO(nums: number[], target: number): number { /* Driver Code */ const target = 6; -const nums = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92]; +const nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]; /* 二分查找(双闭区间) */ let index = binarySearch(nums, target); diff --git a/codes/zig/chapter_searching/binary_search.zig b/codes/zig/chapter_searching/binary_search.zig index 15022e26a..f2a67e192 100644 --- a/codes/zig/chapter_searching/binary_search.zig +++ b/codes/zig/chapter_searching/binary_search.zig @@ -50,7 +50,7 @@ pub fn main() !void { var target: i32 = 6; var nums = std.ArrayList(i32).init(std.heap.page_allocator); defer nums.deinit(); - try nums.appendSlice(&[_]i32{ 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 }); + try nums.appendSlice(&[_]i32{ 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 }); // 二分查找(双闭区间) var index = binarySearch(i32, nums, target);