Files
hello-algo/en/codes/ruby/chapter_searching/binary_search.rb
Yudong Jin 2778a6f9c7 Translate all code to English (#1836)
* Review the EN heading format.

* Fix pythontutor headings.

* Fix pythontutor headings.

* bug fixes

* Fix headings in **/summary.md

* Revisit the CN-to-EN translation for Python code using Claude-4.5

* Revisit the CN-to-EN translation for Java code using Claude-4.5

* Revisit the CN-to-EN translation for Cpp code using Claude-4.5.

* Fix the dictionary.

* Fix cpp code translation for the multipart strings.

* Translate Go code to English.

* Update workflows to test EN code.

* Add EN translation for C.

* Add EN translation for CSharp.

* Add EN translation for Swift.

* Trigger the CI check.

* Revert.

* Update en/hash_map.md

* Add the EN version of Dart code.

* Add the EN version of Kotlin code.

* Add missing code files.

* Add the EN version of JavaScript code.

* Add the EN version of TypeScript code.

* Fix the workflows.

* Add the EN version of Ruby code.

* Add the EN version of Rust code.

* Update the CI check for the English version  code.

* Update Python CI check.

* Fix cmakelists for en/C code.

* Fix Ruby comments
2025-12-31 07:44:52 +08:00

64 lines
1.9 KiB
Ruby

=begin
File: binary_search.rb
Created Time: 2024-04-09
Author: Blue Bean (lonnnnnnner@gmail.com)
=end
### Binary search (closed interval) ###
def binary_search(nums, target)
# Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array
i, j = 0, nums.length - 1
# Loop, exit when the search interval is empty (empty when i > j)
while i <= j
# In theory, Ruby numbers can be infinitely large (limited by memory), no need to consider overflow
m = (i + j) / 2 # Calculate the midpoint index m
if nums[m] < target
i = m + 1 # This means target is in the interval [m+1, j]
elsif nums[m] > target
j = m - 1 # This means target is in the interval [i, m-1]
else
return m # Found the target element, return its index
end
end
-1 # Target element not found, return -1
end
### Binary search (left-closed right-open interval) ###
def binary_search_lcro(nums, target)
# Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1
i, j = 0, nums.length
# Loop, exit when the search interval is empty (empty when i = j)
while i < j
# Calculate the midpoint index m
m = (i + j) / 2
if nums[m] < target
i = m + 1 # This means target is in the interval [m+1, j)
elsif nums[m] > target
j = m - 1 # This means target is in the interval [i, m)
else
return m # Found the target element, return its index
end
end
-1 # Target element not found, return -1
end
### Driver Code ###
if __FILE__ == $0
target = 6
nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]
# Binary search (closed interval on both sides)
index = binary_search(nums, target)
puts "Index of target element 6 is #{index}"
# Binary search (left-closed right-open interval)
index = binary_search_lcro(nums, target)
puts "Index of target element 6 is #{index}"
end