Files
hello-algo/ja/codes/rust/chapter_searching/binary_search.rs
Yudong Jin d7b2277d2b Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4

* Retranslate Japanese code with GPT-5.4
2026-03-30 07:30:15 +08:00

66 lines
2.3 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* File: binary_search.rs
* Created Time: 2023-02-05
* Author: codingonion (coderonion@gmail.com)
*/
/* 二分探索(両閉区間) */
fn binary_search(nums: &[i32], target: i32) -> i32 {
// 両閉区間 [0, n-1] を初期化する。つまり i, j はそれぞれ配列の先頭要素と末尾要素を指す
let mut i = 0;
let mut j = nums.len() as i32 - 1;
// ループし、探索区間が空になったら終了するi > j で空)
while i <= j {
let m = i + (j - i) / 2; // 中点インデックス m を計算
if nums[m as usize] < target {
// この場合、target は区間 [m+1, j] にある
i = m + 1;
} else if nums[m as usize] > target {
// この場合、target は区間 [i, m-1] にある
j = m - 1;
} else {
// 目標要素が見つかったらそのインデックスを返す
return m;
}
}
// 目標要素が見つからなければ -1 を返す
return -1;
}
/* 二分探索(左閉右開区間) */
fn binary_search_lcro(nums: &[i32], target: i32) -> i32 {
// 左閉右開区間 [0, n) を初期化する。つまり i, j はそれぞれ配列の先頭要素と末尾要素+1を指す
let mut i = 0;
let mut j = nums.len() as i32;
// ループし、探索区間が空になったら終了するi = j で空)
while i < j {
let m = i + (j - i) / 2; // 中点インデックス m を計算
if nums[m as usize] < target {
// この場合、target は区間 [m+1, j) にある
i = m + 1;
} else if nums[m as usize] > target {
// この場合、target は区間 [i, m) にある
j = m;
} else {
// 目標要素が見つかったらそのインデックスを返す
return m;
}
}
// 目標要素が見つからなければ -1 を返す
return -1;
}
/* Driver Code */
pub fn main() {
let target = 6;
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
// 二分探索(両閉区間)
let mut index = binary_search(&nums, target);
println!("目的の要素 6 のインデックス = {index}");
// 二分探索(左閉右開区間)
index = binary_search_lcro(&nums, target);
println!("目的の要素 6 のインデックス = {index}");
}