mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 10:26:27 +08:00
The Rust version of the selection sort. (#524)
* The Rust version of the selection sort. * The Rust version of the binary_search_edge * update,The Rust version of the binary_search_edge and selection_sort * update,The Rust version of the binary_search_edge and selection_sort * update The Rust version of the binary_search_edge and selection_sort
This commit is contained in:
parent
41b7b229a3
commit
3b3841ba36
3 changed files with 101 additions and 0 deletions
|
@ -94,6 +94,11 @@ path = "chapter_hashing/array_hash_map.rs"
|
|||
name = "binary_search"
|
||||
path = "chapter_searching/binary_search.rs"
|
||||
|
||||
# Run Command: cargo run --bin binary_search_edge
|
||||
[[bin]]
|
||||
name = "binary_search_edge"
|
||||
path = "chapter_searching/binary_search_edge.rs"
|
||||
|
||||
# Run Command: cargo run --bin bubble_sort
|
||||
[[bin]]
|
||||
name = "bubble_sort"
|
||||
|
@ -114,6 +119,11 @@ path = "chapter_sorting/quick_sort.rs"
|
|||
name = "merge_sort"
|
||||
path = "chapter_sorting/merge_sort.rs"
|
||||
|
||||
# Run Command: cargo run --bin selection_sort
|
||||
[[bin]]
|
||||
name = "selection_sort"
|
||||
path = "chapter_sorting/selection_sort.rs"
|
||||
|
||||
# Run Command: cargo run --bin array_stack
|
||||
[[bin]]
|
||||
name = "array_stack"
|
||||
|
|
59
codes/rust/chapter_searching/binary_search_edge.rs
Normal file
59
codes/rust/chapter_searching/binary_search_edge.rs
Normal file
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* File: binary_search_edge.rs
|
||||
* Created Time: 2023-05-31
|
||||
* Author: WSL0809 (wslzzy@outlook.com)
|
||||
*/
|
||||
|
||||
/* 二分查找最左一个元素 */
|
||||
fn binary_search_left_edge(nums: &[i32], target: i32) -> i32 {
|
||||
let mut i = 0;
|
||||
let mut j = nums.len() as i32 - 1; // 初始化双闭区间 [0, n-1]
|
||||
while i <= j {
|
||||
let m = i + (j - i) / 2; // 计算中点索引 m
|
||||
if nums[m as usize] < target {
|
||||
i = m + 1; // target 在区间 [m+1, j] 中
|
||||
} else if nums[m as usize] > target {
|
||||
j = m - 1; // target 在区间 [i, m-1] 中
|
||||
} else {
|
||||
j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
|
||||
}
|
||||
}
|
||||
if i == nums.len() as i32 || nums[i as usize] != target {
|
||||
return -1; // 未找到目标元素,返回 -1
|
||||
}
|
||||
i
|
||||
}
|
||||
|
||||
/* 二分查找最右一个元素 */
|
||||
fn binary_search_right_edge(nums: &[i32], target: i32) -> i32 {
|
||||
let mut i = 0;
|
||||
let mut j = nums.len() as i32 - 1; // 初始化双闭区间 [0, n-1]
|
||||
while i <= j {
|
||||
let m = i + (j - i) / 2; // 计算中点索引 m
|
||||
if nums[m as usize] < target {
|
||||
i = m + 1; // target 在区间 [m+1, j] 中
|
||||
} else if nums[m as usize] > target {
|
||||
j = m - 1; // target 在区间 [i, m-1] 中
|
||||
} else {
|
||||
i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
|
||||
}
|
||||
}
|
||||
if j < 0 || nums[j as usize] != target {
|
||||
return -1; // 未找到目标元素,返回 -1
|
||||
}
|
||||
j
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
pub fn main() {
|
||||
let target = 6;
|
||||
let nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
|
||||
|
||||
// 二分查找最左一个元素
|
||||
let index_left = binary_search_left_edge(&nums, target);
|
||||
println!("数组中最左一个元素 6 的索引 = {}", index_left);
|
||||
|
||||
// 二分查找最右一个元素
|
||||
let index_right = binary_search_right_edge(&nums, target);
|
||||
println!("数组中最右一个元素 6 的索引 = {}", index_right);
|
||||
}
|
32
codes/rust/chapter_sorting/selection_sort.rs
Normal file
32
codes/rust/chapter_sorting/selection_sort.rs
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* File: selection_sort.rs
|
||||
* Created Time: 2023-05-30
|
||||
* Author: WSL0809 (wslzzy@outlook.com)
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
/* 选择排序 */
|
||||
fn selection_sort(nums: &mut [i32]) {
|
||||
let n = nums.len();
|
||||
// 外循环:未排序区间为 [i, n-1]
|
||||
for i in 0..n-1 {
|
||||
// 内循环:找到未排序区间内的最小元素
|
||||
let mut k = i;
|
||||
for j in i+1..n {
|
||||
if nums[j] < nums[k] {
|
||||
k = j; // 记录最小元素的索引
|
||||
}
|
||||
}
|
||||
// 将该最小元素与未排序区间的首个元素交换
|
||||
nums.swap(i, k);
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
pub fn main() {
|
||||
let mut nums = [4, 1, 3, 1, 5, 2];
|
||||
selection_sort(&mut nums);
|
||||
print!("\n选择排序完成后 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
}
|
Loading…
Reference in a new issue