mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-12 11:37:08 +08:00
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
This commit is contained in:
53
en/codes/rust/chapter_sorting/bubble_sort.rs
Normal file
53
en/codes/rust/chapter_sorting/bubble_sort.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* File: bubble_sort.rs
|
||||
* Created Time: 2023-02-05
|
||||
* Author: codingonion (coderonion@gmail.com)
|
||||
*/
|
||||
|
||||
use hello_algo_rust::include::print_util;
|
||||
|
||||
/* Bubble sort */
|
||||
fn bubble_sort(nums: &mut [i32]) {
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for i in (1..nums.len()).rev() {
|
||||
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
|
||||
for j in 0..i {
|
||||
if nums[j] > nums[j + 1] {
|
||||
// Swap nums[j] and nums[j + 1]
|
||||
nums.swap(j, j + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Bubble sort (flag optimization) */
|
||||
fn bubble_sort_with_flag(nums: &mut [i32]) {
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for i in (1..nums.len()).rev() {
|
||||
let mut flag = false; // Initialize flag
|
||||
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
|
||||
for j in 0..i {
|
||||
if nums[j] > nums[j + 1] {
|
||||
// Swap nums[j] and nums[j + 1]
|
||||
nums.swap(j, j + 1);
|
||||
flag = true; // Record element swap
|
||||
}
|
||||
}
|
||||
if !flag {
|
||||
break; // No elements were swapped in this round of "bubbling", exit directly
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
pub fn main() {
|
||||
let mut nums = [4, 1, 3, 1, 5, 2];
|
||||
bubble_sort(&mut nums);
|
||||
print!("After bubble sort completes, nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
let mut nums1 = [4, 1, 3, 1, 5, 2];
|
||||
bubble_sort_with_flag(&mut nums1);
|
||||
print!("\nAfter bubble sort, nums1 = ");
|
||||
print_util::print_array(&nums1);
|
||||
}
|
||||
43
en/codes/rust/chapter_sorting/bucket_sort.rs
Normal file
43
en/codes/rust/chapter_sorting/bucket_sort.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* File: bucket_sort.rs
|
||||
* Created Time: 2023-07-09
|
||||
* Author: night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
use hello_algo_rust::include::print_util;
|
||||
|
||||
/* Bucket sort */
|
||||
fn bucket_sort(nums: &mut [f64]) {
|
||||
// Initialize k = n/2 buckets, expected to allocate 2 elements per bucket
|
||||
let k = nums.len() / 2;
|
||||
let mut buckets = vec![vec![]; k];
|
||||
// 1. Distribute array elements into various buckets
|
||||
for &num in nums.iter() {
|
||||
// Input data range is [0, 1), use num * k to map to index range [0, k-1]
|
||||
let i = (num * k as f64) as usize;
|
||||
// Add num to bucket i
|
||||
buckets[i].push(num);
|
||||
}
|
||||
// 2. Sort each bucket
|
||||
for bucket in &mut buckets {
|
||||
// Use built-in sorting function, can also replace with other sorting algorithms
|
||||
bucket.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
||||
}
|
||||
// 3. Traverse buckets to merge results
|
||||
let mut i = 0;
|
||||
for bucket in buckets.iter() {
|
||||
for &num in bucket.iter() {
|
||||
nums[i] = num;
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
// Assume input data is floating point, interval [0, 1)
|
||||
let mut nums = [0.49, 0.96, 0.82, 0.09, 0.57, 0.43, 0.91, 0.75, 0.15, 0.37];
|
||||
bucket_sort(&mut nums);
|
||||
print!("After bucket sort completes, nums = ");
|
||||
print_util::print_array(&nums);
|
||||
}
|
||||
70
en/codes/rust/chapter_sorting/counting_sort.rs
Normal file
70
en/codes/rust/chapter_sorting/counting_sort.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* File: counting_sort.rs
|
||||
* Created Time: 2023-07-09
|
||||
* Author: night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
use hello_algo_rust::include::print_util;
|
||||
|
||||
/* Counting sort */
|
||||
// Simple implementation, cannot be used for sorting objects
|
||||
fn counting_sort_naive(nums: &mut [i32]) {
|
||||
// 1. Count the maximum element m in the array
|
||||
let m = *nums.iter().max().unwrap();
|
||||
// 2. Count the occurrence of each number
|
||||
// counter[num] represents the occurrence of num
|
||||
let mut counter = vec![0; m as usize + 1];
|
||||
for &num in nums.iter() {
|
||||
counter[num as usize] += 1;
|
||||
}
|
||||
// 3. Traverse counter, filling each element back into the original array nums
|
||||
let mut i = 0;
|
||||
for num in 0..m + 1 {
|
||||
for _ in 0..counter[num as usize] {
|
||||
nums[i] = num;
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Counting sort */
|
||||
// Complete implementation, can sort objects and is a stable sort
|
||||
fn counting_sort(nums: &mut [i32]) {
|
||||
// 1. Count the maximum element m in the array
|
||||
let m = *nums.iter().max().unwrap() as usize;
|
||||
// 2. Count the occurrence of each number
|
||||
// counter[num] represents the occurrence of num
|
||||
let mut counter = vec![0; m + 1];
|
||||
for &num in nums.iter() {
|
||||
counter[num as usize] += 1;
|
||||
}
|
||||
// 3. Calculate the prefix sum of counter, converting "occurrence count" to "tail index"
|
||||
// counter[num]-1 is the last index where num appears in res
|
||||
for i in 0..m {
|
||||
counter[i + 1] += counter[i];
|
||||
}
|
||||
// 4. Traverse nums in reverse order, placing each element into the result array res
|
||||
// Initialize the array res to record results
|
||||
let n = nums.len();
|
||||
let mut res = vec![0; n];
|
||||
for i in (0..n).rev() {
|
||||
let num = nums[i];
|
||||
res[counter[num as usize] - 1] = num; // Place num at the corresponding index
|
||||
counter[num as usize] -= 1; // Decrement the prefix sum by 1, getting the next index to place num
|
||||
}
|
||||
// Use result array res to overwrite the original array nums
|
||||
nums.copy_from_slice(&res)
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
let mut nums = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4];
|
||||
counting_sort_naive(&mut nums);
|
||||
print!("After counting sort (cannot sort objects) completes, nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
let mut nums1 = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4];
|
||||
counting_sort(&mut nums1);
|
||||
print!("\nAfter counting sort, nums1 = ");
|
||||
print_util::print_array(&nums1);
|
||||
}
|
||||
54
en/codes/rust/chapter_sorting/heap_sort.rs
Normal file
54
en/codes/rust/chapter_sorting/heap_sort.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* File: heap_sort.rs
|
||||
* Created Time: 2023-07-04
|
||||
* Author: night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
use hello_algo_rust::include::print_util;
|
||||
|
||||
/* Heap length is n, start heapifying node i, from top to bottom */
|
||||
fn sift_down(nums: &mut [i32], n: usize, mut i: usize) {
|
||||
loop {
|
||||
// If node i is largest or indices l, r are out of bounds, no need to continue heapify, break
|
||||
let l = 2 * i + 1;
|
||||
let r = 2 * i + 2;
|
||||
let mut ma = i;
|
||||
if l < n && nums[l] > nums[ma] {
|
||||
ma = l;
|
||||
}
|
||||
if r < n && nums[r] > nums[ma] {
|
||||
ma = r;
|
||||
}
|
||||
// Swap two nodes
|
||||
if ma == i {
|
||||
break;
|
||||
}
|
||||
// Swap two nodes
|
||||
nums.swap(i, ma);
|
||||
// Loop downwards heapification
|
||||
i = ma;
|
||||
}
|
||||
}
|
||||
|
||||
/* Heap sort */
|
||||
fn heap_sort(nums: &mut [i32]) {
|
||||
// Build heap operation: heapify all nodes except leaves
|
||||
for i in (0..nums.len() / 2).rev() {
|
||||
sift_down(nums, nums.len(), i);
|
||||
}
|
||||
// Extract the largest element from the heap and repeat for n-1 rounds
|
||||
for i in (1..nums.len()).rev() {
|
||||
// Delete node
|
||||
nums.swap(0, i);
|
||||
// Start heapifying the root node, from top to bottom
|
||||
sift_down(nums, i, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
let mut nums = [4, 1, 3, 1, 5, 2];
|
||||
heap_sort(&mut nums);
|
||||
print!("After heap sort completes, nums = ");
|
||||
print_util::print_array(&nums);
|
||||
}
|
||||
29
en/codes/rust/chapter_sorting/insertion_sort.rs
Normal file
29
en/codes/rust/chapter_sorting/insertion_sort.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* File: insertion_sort.rs
|
||||
* Created Time: 2023-02-13
|
||||
* Author: xBLACKICEx (xBLACKICEx@outlook.com)
|
||||
*/
|
||||
|
||||
use hello_algo_rust::include::print_util;
|
||||
|
||||
/* Insertion sort */
|
||||
fn insertion_sort(nums: &mut [i32]) {
|
||||
// Outer loop: sorted interval is [0, i-1]
|
||||
for i in 1..nums.len() {
|
||||
let (base, mut j) = (nums[i], (i - 1) as i32);
|
||||
// Inner loop: insert base into the correct position within the sorted interval [0, i-1]
|
||||
while j >= 0 && nums[j as usize] > base {
|
||||
nums[(j + 1) as usize] = nums[j as usize]; // Move nums[j] to the right by one position
|
||||
j -= 1;
|
||||
}
|
||||
nums[(j + 1) as usize] = base; // Assign base to the correct position
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
let mut nums = [4, 1, 3, 1, 5, 2];
|
||||
insertion_sort(&mut nums);
|
||||
print!("After insertion sort completes, nums = ");
|
||||
print_util::print_array(&nums);
|
||||
}
|
||||
66
en/codes/rust/chapter_sorting/merge_sort.rs
Normal file
66
en/codes/rust/chapter_sorting/merge_sort.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* File: merge_sort.rs
|
||||
* Created Time: 2023-02-14
|
||||
* Author: xBLACKICEx (xBLACKICEx@outlook.com)
|
||||
*/
|
||||
|
||||
/* Merge left subarray and right subarray */
|
||||
fn merge(nums: &mut [i32], left: usize, mid: usize, right: usize) {
|
||||
// Left subarray interval is [left, mid], right subarray interval is [mid+1, right]
|
||||
// Create a temporary array tmp to store the merged results
|
||||
let tmp_size = right - left + 1;
|
||||
let mut tmp = vec![0; tmp_size];
|
||||
// Initialize the start indices of the left and right subarrays
|
||||
let (mut i, mut j, mut k) = (left, mid + 1, 0);
|
||||
// While both subarrays still have elements, compare and copy the smaller element into the temporary array
|
||||
while i <= mid && j <= right {
|
||||
if nums[i] <= nums[j] {
|
||||
tmp[k] = nums[i];
|
||||
i += 1;
|
||||
} else {
|
||||
tmp[k] = nums[j];
|
||||
j += 1;
|
||||
}
|
||||
k += 1;
|
||||
}
|
||||
// Copy the remaining elements of the left and right subarrays into the temporary array
|
||||
while i <= mid {
|
||||
tmp[k] = nums[i];
|
||||
k += 1;
|
||||
i += 1;
|
||||
}
|
||||
while j <= right {
|
||||
tmp[k] = nums[j];
|
||||
k += 1;
|
||||
j += 1;
|
||||
}
|
||||
// Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval
|
||||
for k in 0..tmp_size {
|
||||
nums[left + k] = tmp[k];
|
||||
}
|
||||
}
|
||||
|
||||
/* Merge sort */
|
||||
fn merge_sort(nums: &mut [i32], left: usize, right: usize) {
|
||||
// Termination condition
|
||||
if left >= right {
|
||||
return; // Terminate recursion when subarray length is 1
|
||||
}
|
||||
|
||||
// Divide and conquer stage
|
||||
let mid = left + (right - left) / 2; // Calculate midpoint
|
||||
merge_sort(nums, left, mid); // Recursively process the left subarray
|
||||
merge_sort(nums, mid + 1, right); // Recursively process the right subarray
|
||||
|
||||
// Merge stage
|
||||
merge(nums, left, mid, right);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* Merge sort */
|
||||
let mut nums = [7, 3, 2, 6, 0, 1, 5, 4];
|
||||
let right = nums.len() - 1;
|
||||
merge_sort(&mut nums, 0, right);
|
||||
println!("After merge sort, nums = {:?}", nums);
|
||||
}
|
||||
148
en/codes/rust/chapter_sorting/quick_sort.rs
Normal file
148
en/codes/rust/chapter_sorting/quick_sort.rs
Normal file
@@ -0,0 +1,148 @@
|
||||
/**
|
||||
* File: quick_sort.rs
|
||||
* Created Time: 2023-02-16
|
||||
* Author: xBLACKICEx (xBLACKICE@outlook.com)
|
||||
*/
|
||||
|
||||
/* Quick sort */
|
||||
struct QuickSort;
|
||||
|
||||
impl QuickSort {
|
||||
/* Sentinel partition */
|
||||
fn partition(nums: &mut [i32], left: usize, right: usize) -> usize {
|
||||
// Use nums[left] as the pivot
|
||||
let (mut i, mut j) = (left, right);
|
||||
while i < j {
|
||||
while i < j && nums[j] >= nums[left] {
|
||||
j -= 1; // Search from right to left for the first element smaller than the pivot
|
||||
}
|
||||
while i < j && nums[i] <= nums[left] {
|
||||
i += 1; // Search from left to right for the first element greater than the pivot
|
||||
}
|
||||
nums.swap(i, j); // Swap these two elements
|
||||
}
|
||||
nums.swap(i, left); // Swap the pivot to the boundary between the two subarrays
|
||||
i // Return the index of the pivot
|
||||
}
|
||||
|
||||
/* Quick sort */
|
||||
pub fn quick_sort(left: i32, right: i32, nums: &mut [i32]) {
|
||||
// Terminate recursion when subarray length is 1
|
||||
if left >= right {
|
||||
return;
|
||||
}
|
||||
// Sentinel partition
|
||||
let pivot = Self::partition(nums, left as usize, right as usize) as i32;
|
||||
// Recursively process the left subarray and right subarray
|
||||
Self::quick_sort(left, pivot - 1, nums);
|
||||
Self::quick_sort(pivot + 1, right, nums);
|
||||
}
|
||||
}
|
||||
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
struct QuickSortMedian;
|
||||
|
||||
impl QuickSortMedian {
|
||||
/* Select the median of three candidate elements */
|
||||
fn median_three(nums: &mut [i32], left: usize, mid: usize, right: usize) -> usize {
|
||||
let (l, m, r) = (nums[left], nums[mid], nums[right]);
|
||||
if (l <= m && m <= r) || (r <= m && m <= l) {
|
||||
return mid; // m is between l and r
|
||||
}
|
||||
if (m <= l && l <= r) || (r <= l && l <= m) {
|
||||
return left; // l is between m and r
|
||||
}
|
||||
right
|
||||
}
|
||||
|
||||
/* Sentinel partition (median of three) */
|
||||
fn partition(nums: &mut [i32], left: usize, right: usize) -> usize {
|
||||
// Select the median of three candidate elements
|
||||
let med = Self::median_three(nums, left, (left + right) / 2, right);
|
||||
// Swap the median to the array's leftmost position
|
||||
nums.swap(left, med);
|
||||
// Use nums[left] as the pivot
|
||||
let (mut i, mut j) = (left, right);
|
||||
while i < j {
|
||||
while i < j && nums[j] >= nums[left] {
|
||||
j -= 1; // Search from right to left for the first element smaller than the pivot
|
||||
}
|
||||
while i < j && nums[i] <= nums[left] {
|
||||
i += 1; // Search from left to right for the first element greater than the pivot
|
||||
}
|
||||
nums.swap(i, j); // Swap these two elements
|
||||
}
|
||||
nums.swap(i, left); // Swap the pivot to the boundary between the two subarrays
|
||||
i // Return the index of the pivot
|
||||
}
|
||||
|
||||
/* Quick sort */
|
||||
pub fn quick_sort(left: i32, right: i32, nums: &mut [i32]) {
|
||||
// Terminate recursion when subarray length is 1
|
||||
if left >= right {
|
||||
return;
|
||||
}
|
||||
// Sentinel partition
|
||||
let pivot = Self::partition(nums, left as usize, right as usize) as i32;
|
||||
// Recursively process the left subarray and right subarray
|
||||
Self::quick_sort(left, pivot - 1, nums);
|
||||
Self::quick_sort(pivot + 1, right, nums);
|
||||
}
|
||||
}
|
||||
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
struct QuickSortTailCall;
|
||||
|
||||
impl QuickSortTailCall {
|
||||
/* Sentinel partition */
|
||||
fn partition(nums: &mut [i32], left: usize, right: usize) -> usize {
|
||||
// Use nums[left] as the pivot
|
||||
let (mut i, mut j) = (left, right);
|
||||
while i < j {
|
||||
while i < j && nums[j] >= nums[left] {
|
||||
j -= 1; // Search from right to left for the first element smaller than the pivot
|
||||
}
|
||||
while i < j && nums[i] <= nums[left] {
|
||||
i += 1; // Search from left to right for the first element greater than the pivot
|
||||
}
|
||||
nums.swap(i, j); // Swap these two elements
|
||||
}
|
||||
nums.swap(i, left); // Swap the pivot to the boundary between the two subarrays
|
||||
i // Return the index of the pivot
|
||||
}
|
||||
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
pub fn quick_sort(mut left: i32, mut right: i32, nums: &mut [i32]) {
|
||||
// Terminate when subarray length is 1
|
||||
while left < right {
|
||||
// Sentinel partition operation
|
||||
let pivot = Self::partition(nums, left as usize, right as usize) as i32;
|
||||
// Perform quick sort on the shorter of the two subarrays
|
||||
if pivot - left < right - pivot {
|
||||
Self::quick_sort(left, pivot - 1, nums); // Recursively sort the left subarray
|
||||
left = pivot + 1; // Remaining unsorted interval is [pivot + 1, right]
|
||||
} else {
|
||||
Self::quick_sort(pivot + 1, right, nums); // Recursively sort the right subarray
|
||||
right = pivot - 1; // Remaining unsorted interval is [left, pivot - 1]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* Quick sort */
|
||||
let mut nums = [2, 4, 1, 0, 3, 5];
|
||||
QuickSort::quick_sort(0, (nums.len() - 1) as i32, &mut nums);
|
||||
println!("After quick sort, nums = {:?}", nums);
|
||||
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
let mut nums = [2, 4, 1, 0, 3, 5];
|
||||
QuickSortMedian::quick_sort(0, (nums.len() - 1) as i32, &mut nums);
|
||||
println!("After quick sort (median pivot optimization), nums = {:?}", nums);
|
||||
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
let mut nums = [2, 4, 1, 0, 3, 5];
|
||||
QuickSortTailCall::quick_sort(0, (nums.len() - 1) as i32, &mut nums);
|
||||
println!("After quick sort (recursion depth optimization), nums = {:?}", nums);
|
||||
}
|
||||
63
en/codes/rust/chapter_sorting/radix_sort.rs
Normal file
63
en/codes/rust/chapter_sorting/radix_sort.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* File: radix_sort.rs
|
||||
* Created Time: 2023-07-09
|
||||
* Author: night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
use hello_algo_rust::include::print_util;
|
||||
|
||||
/* Get the k-th digit of element num, where exp = 10^(k-1) */
|
||||
fn digit(num: i32, exp: i32) -> usize {
|
||||
// Passing exp instead of k can avoid repeated expensive exponentiation here
|
||||
return ((num / exp) % 10) as usize;
|
||||
}
|
||||
|
||||
/* Counting sort (based on nums k-th digit) */
|
||||
fn counting_sort_digit(nums: &mut [i32], exp: i32) {
|
||||
// Decimal digit range is 0~9, therefore need a bucket array of length 10
|
||||
let mut counter = [0; 10];
|
||||
let n = nums.len();
|
||||
// Count the occurrence of digits 0~9
|
||||
for i in 0..n {
|
||||
let d = digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d
|
||||
counter[d] += 1; // Count the occurrence of digit d
|
||||
}
|
||||
// Calculate prefix sum, converting "occurrence count" into "array index"
|
||||
for i in 1..10 {
|
||||
counter[i] += counter[i - 1];
|
||||
}
|
||||
// Traverse in reverse, based on bucket statistics, place each element into res
|
||||
let mut res = vec![0; n];
|
||||
for i in (0..n).rev() {
|
||||
let d = digit(nums[i], exp);
|
||||
let j = counter[d] - 1; // Get the index j for d in the array
|
||||
res[j] = nums[i]; // Place the current element at index j
|
||||
counter[d] -= 1; // Decrease the count of d by 1
|
||||
}
|
||||
// Use result to overwrite the original array nums
|
||||
nums.copy_from_slice(&res);
|
||||
}
|
||||
|
||||
/* Radix sort */
|
||||
fn radix_sort(nums: &mut [i32]) {
|
||||
// Get the maximum element of the array, used to determine the maximum number of digits
|
||||
let m = *nums.into_iter().max().unwrap();
|
||||
// Traverse from the lowest to the highest digit
|
||||
let mut exp = 1;
|
||||
while exp <= m {
|
||||
counting_sort_digit(nums, exp);
|
||||
exp *= 10;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
// Radix sort
|
||||
let mut nums = [
|
||||
10546151, 35663510, 42865989, 34862445, 81883077, 88906420, 72429244, 30524779, 82060337,
|
||||
63832996,
|
||||
];
|
||||
radix_sort(&mut nums);
|
||||
print!("After radix sort completes, nums = ");
|
||||
print_util::print_array(&nums);
|
||||
}
|
||||
35
en/codes/rust/chapter_sorting/selection_sort.rs
Normal file
35
en/codes/rust/chapter_sorting/selection_sort.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* File: selection_sort.rs
|
||||
* Created Time: 2023-05-30
|
||||
* Author: WSL0809 (wslzzy@outlook.com)
|
||||
*/
|
||||
|
||||
use hello_algo_rust::include::print_util;
|
||||
|
||||
/* Selection sort */
|
||||
fn selection_sort(nums: &mut [i32]) {
|
||||
if nums.is_empty() {
|
||||
return;
|
||||
}
|
||||
let n = nums.len();
|
||||
// Outer loop: unsorted interval is [i, n-1]
|
||||
for i in 0..n - 1 {
|
||||
// Inner loop: find the smallest element within the unsorted interval
|
||||
let mut k = i;
|
||||
for j in i + 1..n {
|
||||
if nums[j] < nums[k] {
|
||||
k = j; // Record the index of the smallest element
|
||||
}
|
||||
}
|
||||
// Swap the smallest element with the first element of the unsorted interval
|
||||
nums.swap(i, k);
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
pub fn main() {
|
||||
let mut nums = [4, 1, 3, 1, 5, 2];
|
||||
selection_sort(&mut nums);
|
||||
print!("\nAfter selection sort, nums = ");
|
||||
print_util::print_array(&nums);
|
||||
}
|
||||
Reference in New Issue
Block a user