This commit is contained in:
krahets
2024-04-13 21:17:44 +08:00
parent 9332a91e26
commit 6afa70e7bc
55 changed files with 334 additions and 182 deletions
@@ -339,7 +339,27 @@ comments: true
=== "Ruby"
```ruby title="binary_search.rb"
[class]{}-[func]{binary_search}
### 二分搜尋(雙閉區間) ###
def binary_search(nums, target)
# 初始化雙閉區間 [0, n-1] ,即 i, j 分別指向陣列首元素、尾元素
i, j = 0, nums.length - 1
# 迴圈,當搜尋區間為空時跳出(當 i > j 時為空)
while i <= j
# 理論上 Ruby 的數字可以無限大(取決於記憶體大小),無須考慮大數越界問題
m = (i + j) / 2 # 計算中點索引 m
if nums[m] < target
i = m + 1 # 此情況說明 target 在區間 [m+1, j] 中
elsif nums[m] > target
j = m - 1 # 此情況說明 target 在區間 [i, m-1] 中
else
return m # 找到目標元素,返回其索引
end
end
-1 # 未找到目標元素,返回 -1
end
```
=== "Zig"
@@ -667,7 +687,27 @@ comments: true
=== "Ruby"
```ruby title="binary_search.rb"
[class]{}-[func]{binary_search_lcro}
### 二分搜尋(左閉右開區間) ###
def binary_search_lcro(nums, target)
# 初始化左閉右開區間 [0, n) ,即 i, j 分別指向陣列首元素、尾元素+1
i, j = 0, nums.length
# 迴圈,當搜尋區間為空時跳出(當 i = j 時為空)
while i < j
# 計算中點索引 m
m = (i + j) / 2
if nums[m] < target
i = m + 1 # 此情況說明 target 在區間 [m+1, j) 中
elsif nums[m] > target
j = m - 1 # 此情況說明 target 在區間 [i, m) 中
else
return m # 找到目標元素,返回其索引
end
end
-1 # 未找到目標元素,返回 -1
end
```
=== "Zig"