Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(merge_sort_inplace,rotate): add inplace merge_sort no_std,utils rotate #50

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
[[https://conventionalcommits.org][https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white.svg]]

** Algorithms
- Features
- no_std
- std
*** Sorting 排序
- [[./src/sorting/insertion_sort.rs::9][InsertionSort 插入排序]]
- [[./src/sorting/selection_sort.rs::9][SelectionSort 选择排序]]
Expand All @@ -27,3 +30,6 @@
- [[./src/math/pi.rs::6][pi 圆周率]]
*** String 字符串
- [[./src/string/max_substring.rs::10][MaxSubarray 最大子数组]]

*** utils 工具
- [[./src/utils.rs::41][rotate 旋转数组]]
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub mod searching;
/// Math
pub mod math;

/// Tools
pub mod tools;
/// Utils
pub mod utils;

/// String
pub mod string;
73 changes: 64 additions & 9 deletions src/sorting/merge_sort.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
// ! maybe need fix
#[cfg(feature = "no_std")]
use crate::utils::rotate;

#[cfg(feature = "no_std")]
fn merge<T>(array: &mut [T], mid: usize, comparator: fn(&T, &T) -> bool) {
let len = array.len();
let mut left = 0;
let mut right = mid;

while left < right && right < len {
if comparator(&array[left], &array[right]) {
left += 1;
}
// 此时comparator(array[left],array[right]) = false
else {
let tmp = right;
while right < len && comparator(&array[left], &array[right]) == false {
right += 1;
}

// 此时comparator(array[left],array[right]) = true
// 在right到tmp之间的元素是需要与i到tmp旋转的
let rotate_mid = tmp - left;
let slice = &mut array[left..right];
rotate(slice, rotate_mid).unwrap();
// 更新左指针位置
left += right - tmp;
}
}
}

#[cfg(not(feature = "no_std"))]
fn merge<T: Clone>(array: &mut [T], mid: usize, comparator: fn(&T, &T) -> bool) {
let left = array[..mid].to_vec();
Expand Down Expand Up @@ -30,26 +60,51 @@ fn merge<T: Clone>(array: &mut [T], mid: usize, comparator: fn(&T, &T) -> bool)
index += 1;
}
}

/// Merge Sort
/// - no_std启用时: 采用旋转元素与自底向上的归并排序
/// - std启用时: 原始的归并排序
/// # Example
/// ```
/// use algori::sorting::{merge_sort,is_sorted};
/// let mut a = [2,3,1,34,15,8,0,7,4,3,21,4,6,7,4,2341,321,41231,312,62];
/// merge_sort(&mut a ,|a,b| a<=b);
/// assert_eq!(is_sorted(&mut a ,|a,b| a<=b),true);
/// assert_eq!([0, 1, 2, 3, 3, 4, 4, 4, 6, 7, 7, 8, 15, 21, 34, 62, 312, 321, 2341, 41231],a)
/// ```
#[cfg(feature = "no_std")]
pub fn merge_sort<T>(array: &mut [T], comparator: fn(&T, &T) -> bool) {
let n = array.len();
let mut size = 1;

while size < n {
for left in (0..n).step_by(2 * size) {
let mid = left + size;
let right = core::cmp::min(left + 2 * size, n);
if mid < right {
merge(&mut array[left..right], mid - left, comparator);
}
}
size *= 2;
}
}

#[cfg(not(feature = "no_std"))]
pub fn merge_sort<T: Clone>(array: &mut [T], comparator: fn(&T, &T) -> bool) {
if array.len() > 1 {
if array.len() > 1 {
let mid = array.len() / 2;
merge_sort(&mut array[..mid], comparator);
merge_sort(&mut array[mid..], comparator);
merge(array, mid, comparator);
let n = array.len();
let mut size = 1;

while size < n {
for left in (0..n).step_by(2 * size) {
let mid = left + size;
let right = core::cmp::min(left + 2 * size, n);
if mid < right {
merge(&mut array[left..right], mid - left, comparator);
}
}
size *= 2;
}
}
#[cfg(not(feature = "no_std"))]

#[cfg(test)]
mod tests {
use super::super::is_sorted;
Expand Down
1 change: 0 additions & 1 deletion src/sorting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ pub use self::insertion_sort::*;
pub use self::selection_sort::*;
pub use self::utils::*;

#[cfg(not(feature = "no_std"))]
pub use self::merge_sort::*;
21 changes: 0 additions & 21 deletions src/tools.rs

This file was deleted.

103 changes: 103 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/// test the time of function
/// # Examples
/// ```
/// use algori::test_time;
/// use algori::sorting::insertion_sort;
/// let mut a = [1,4,6,7,2,2,1,4,65,6];
/// test_time!("Insertion Sort",insertion_sort(&mut a,|a,b|a<=b));
/// ```
#[macro_export]
macro_rules! test_time {
($title:literal,$func:expr) => {
let now = std::time::Instant::now();
$func;
println!(
"Job:\t{}\nUsing\t{}\tseconds\n\t{}\tnanos",
$title,
now.elapsed().as_secs(),
now.elapsed().as_nanos()
);
};
}

fn reverse<T>(array: &mut [T], mut start: usize, mut end: usize) {
while start < end {
array.swap(start, end);
start += 1;
end -= 1;
}
}
/// 手摇旋转
/// 旋转以 `mid` 为分界的切片
/// - 若mid超出边界或者位于边界 那么返回Err(())
/// - 若成功返回Ok(())
/// # Example
/// ```
/// use algori::utils::rotate;
/// let mut a = ['a','b','c','d','e','f'];
/// rotate(&mut a, 2);
/// assert_eq!(a,['c','d','e','f','a','b']);
/// ```
pub fn rotate<T>(array: &mut [T], mid: usize) -> Result<(), ()> {
let len = array.len();
// 越界Err
if mid >= len {
return Err(());
}
if mid == 0 {
return Ok(());
}
reverse(array, 0, mid - 1);
reverse(array, mid, len - 1);
reverse(array, 0, len - 1);
return Ok(());
}

#[cfg(test)]
mod rotate_test {
use crate::utils::rotate;
// 测试奇数的数组
#[test]
fn odd() {
let mut a = ['a', 'b', 'c', 'd', 'e'];
rotate(&mut a, 2).unwrap();
assert_eq!(a, ['c', 'd', 'e', 'a', 'b']);
}
// 偶数的数组
#[test]
fn even() {
let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
rotate(&mut a, 2).unwrap();
assert_eq!(a, ['c', 'd', 'e', 'f', 'a', 'b']);
}
// 下标越界
#[test]
fn over_index() {
let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
let result = rotate(&mut a, 6);
assert_eq!(a, ['a', 'b', 'c', 'd', 'e', 'f']);
assert_eq!(result, Err(()));
}
// 相等
#[test]
fn len() {
let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
let result = rotate(&mut a, 5);
assert_eq!(a, ['f', 'a', 'b', 'c', 'd', 'e']);
assert_eq!(result, Ok(()));
}
// 开始
#[test]
fn start() {
let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
let result = rotate(&mut a, 0);
assert_eq!(a, ['a', 'b', 'c', 'd', 'e', 'f']);
assert_eq!(result, Ok(()));
}
#[test]
fn rotate_test1() {
let mut a = [20, 30, 40, 50, 15];
rotate(&mut a, 4).unwrap();
assert_eq!([15, 20, 30, 40, 50], a);
}
}