cargo fmt rust code (#1131)

* cargo fmt code

* Add empty line to seperate unrelated comments

* Fix review

* Update bubble_sort.rs

* Update merge_sort.rs

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
rongyi
2024-03-16 02:13:41 +08:00
committed by GitHub
parent 54ceef3443
commit 7b1094318b
70 changed files with 1021 additions and 836 deletions
+18 -12
View File
@@ -11,14 +11,17 @@ fn binary_search(nums: &[i32], target: i32) -> i32 {
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] 中
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] 中
} else if nums[m as usize] > target {
// 此情况说明 target 在区间 [i, m-1] 中
j = m - 1;
} else { // 找到目标元素,返回其索引
} else {
// 找到目标元素,返回其索引
return m;
}
}
}
// 未找到目标元素,返回 -1
return -1;
@@ -31,14 +34,17 @@ fn binary_search_lcro(nums: &[i32], target: i32) -> i32 {
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) 中
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) 中
} else if nums[m as usize] > target {
// 此情况说明 target 在区间 [i, m) 中
j = m;
} else { // 找到目标元素,返回其索引
} else {
// 找到目标元素,返回其索引
return m;
}
}
}
// 未找到目标元素,返回 -1
return -1;
@@ -47,8 +53,8 @@ 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, 26, 31, 35 ];
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
// 二分查找(双闭区间)
let mut index = binary_search(&nums, target);
println!("目标元素 6 的索引 = {index}");