From 8aeb564f678d504083d10521f1595b1f6e2c363a Mon Sep 17 00:00:00 2001 From: Constantine Date: Tue, 21 Jul 2020 23:02:31 +0300 Subject: [PATCH] Finished working on smoothsort, bumped version --- Cargo.toml | 2 +- README.md | 7 +++---- benches/sort_benchmark.rs | 9 +++++---- src/bin/leonardo_numbers.rs | 2 +- src/smooth_sort.rs | 30 ++++++++++++++++-------------- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6a32438..b9964f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sorting_rs" -version = "1.1.0" +version = "1.2.0" authors = ["flakusha "] edition = "2018" repository = "https://github.com/flakusha/rust_sorting" diff --git a/README.md b/README.md index b86a0ec..2c389af 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ # sorting_rs Sorting algorithms implemented in Rust ## Usage -1. Add this dependency with it's version into your Cargo.toml: +1. Add this dependency and please consider it's version into your Cargo.toml: ```toml ... [dependencies] -sorting_rs = "1.1.0" +sorting_rs = "1.2.0" ... ``` 2. Use available sorting algorithms in your Rust code: @@ -17,8 +17,7 @@ use sorting_rs::*; please read modules documentation ## This library contains following sorting algorithms: -New algorithms implementations are planned, smooth sort is not ready at the -moment, for example +New algorithms implementations are planned | Sorting algorithm | Features and downsides | Worst-case performance O(): comparisons; swaps | Best-case performance O(): comparisons; swaps | Space complexity O() | | ------ | -------------------------------- | ------------------------------------ | -------- | ---------- | diff --git a/benches/sort_benchmark.rs b/benches/sort_benchmark.rs index 7013fa8..b5d9a44 100644 --- a/benches/sort_benchmark.rs +++ b/benches/sort_benchmark.rs @@ -30,7 +30,7 @@ macro_rules! create_bench { } fn bench(c: &mut Criterion) { - let sizes: Vec = vec![10, 100, 1000, 10_000, 100_000, /*1_000_000, + let sizes: Vec = vec![10, 100, 1000, 10_000, /*100_000, 1_000_000, 10_000_000*/]; let benchmark = create_bench! { @@ -47,9 +47,10 @@ fn bench(c: &mut Criterion) { quick_sort, selection_sort, shell_sort, - slow_sort, - // smooth_sort, - stooge_sort + smooth_sort + // Exclude extremely slow sorts + // slow_sort, + // stooge_sort }; c.bench("sort_bench", benchmark); diff --git a/src/bin/leonardo_numbers.rs b/src/bin/leonardo_numbers.rs index 61f2c9b..9d317cd 100644 --- a/src/bin/leonardo_numbers.rs +++ b/src/bin/leonardo_numbers.rs @@ -4,7 +4,7 @@ /// 32-, 64-, 128-bit and other systems /// This addition uses usize in case there is mainstream 64-bit system /// # Usage: -/// ```no_run +/// ```ignore /// cargo run leonardo_numbers /// ``` use std::io; diff --git a/src/smooth_sort.rs b/src/smooth_sort.rs index 3cded7d..c5905bc 100644 --- a/src/smooth_sort.rs +++ b/src/smooth_sort.rs @@ -9,7 +9,7 @@ /// largest remaining element /// This algorithm makes use of Leonardo numbers. It's a sequence of numbers /// defined by: -/// ```no_run +/// ```ignore /// L(0) = 1 /// L(1) = 1 /// L(n) = L(n - 1) + L(n - 2) + 1 @@ -32,7 +32,6 @@ /// sorting_rs::smooth_sort(&mut strings); /// assert_eq!(strings, &["cargo", "rustc", "rustup"]); /// ``` -use std::fmt::Debug; const LEO_NUMS: [usize; 90] = [ 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, @@ -53,11 +52,11 @@ const LEO_NUMS: [usize; 90] = [ 3559958832009428377, 5760134388741632239, ]; -pub fn smooth_sort(input: &mut [T]) +pub fn smooth_sort(input: &mut [T]) { if input.len() < 2 {return;} - // Init addtitional heap + // Init addtitional index heap let input = input; let in_len = input.len(); let mut heap = Vec::::new(); @@ -67,16 +66,13 @@ pub fn smooth_sort(input: &mut [T]) heap.pop(); let len_leo = heap.len(); heap[len_leo - 1] += 1; + } else if heap.len() >= 1 && heap[heap.len() - 1] == 1 { + heap.push(0); } else { - if heap.len() >= 1 && heap[heap.len() - 1] == 1 { - heap.push(0); - } else { - heap.push(1); - } + heap.push(1); } restore_heap(input, i, &heap); } - println!("DEBUG: {:?}", input); for i in (0..in_len).rev() { if heap[heap.len() - 1] < 2 { @@ -92,7 +88,6 @@ pub fn smooth_sort(input: &mut [T]) restore_heap(input, t[0], &heap); } } - println!("DEBUG: {:?}", input); } fn restore_heap(input: &mut [T], index: usize, heap: &Vec) @@ -104,8 +99,8 @@ fn restore_heap(input: &mut [T], index: usize, heap: &Vec) while current > 0 { let j = i - LEO_NUMS[k]; - if input[j] > input[i] && (k < 2 || input[j] > input[i - 1] && - input[j] > input[i - 2]) { + if input[j] > input[i] && + (k < 2 || input[j] > input[i - 1] && input[j] > input[i - 2]) { input.swap(i, j); i = j; current -= 1; @@ -114,7 +109,8 @@ fn restore_heap(input: &mut [T], index: usize, heap: &Vec) break; } } - while k > 2 { + + while k >= 2 { let t = get_child_trees(i, k); // tr kr tl kl // 0 1 2 3 @@ -153,6 +149,12 @@ mod tests { debug_assert_eq!(vector_in, &[10, 11, 13, 20]); } #[test] + fn test_smooth_01() { + let mut vector_in = vec![20, 10, 11, 13, 24, 9, 2, 1, 8]; + smooth_sort(&mut vector_in); + debug_assert_eq!(vector_in, &[1, 2, 8, 9, 10, 11, 13, 20, 24]); + } + #[test] fn test_smooth_empty() { let mut vector_in:Vec = vec![]; smooth_sort(&mut vector_in);