From 3b3841ba3627c773735658fb999dc96bcbad40a1 Mon Sep 17 00:00:00 2001 From: WangSL <48207171+WSL0809@users.noreply.github.com> Date: Sun, 25 Jun 2023 21:00:24 +0800 Subject: [PATCH] The Rust version of the selection sort. (#524) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- codes/rust/Cargo.toml | 10 ++++ .../chapter_searching/binary_search_edge.rs | 59 +++++++++++++++++++ codes/rust/chapter_sorting/selection_sort.rs | 32 ++++++++++ 3 files changed, 101 insertions(+) create mode 100644 codes/rust/chapter_searching/binary_search_edge.rs create mode 100644 codes/rust/chapter_sorting/selection_sort.rs diff --git a/codes/rust/Cargo.toml b/codes/rust/Cargo.toml index d1709499b..8c40d4f7e 100644 --- a/codes/rust/Cargo.toml +++ b/codes/rust/Cargo.toml @@ -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" diff --git a/codes/rust/chapter_searching/binary_search_edge.rs b/codes/rust/chapter_searching/binary_search_edge.rs new file mode 100644 index 000000000..3d6048bc7 --- /dev/null +++ b/codes/rust/chapter_searching/binary_search_edge.rs @@ -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); +} \ No newline at end of file diff --git a/codes/rust/chapter_sorting/selection_sort.rs b/codes/rust/chapter_sorting/selection_sort.rs new file mode 100644 index 000000000..6e30a7b21 --- /dev/null +++ b/codes/rust/chapter_sorting/selection_sort.rs @@ -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); +} \ No newline at end of file