Skip to content

Commit

Permalink
Finished working on smoothsort, bumped version
Browse files Browse the repository at this point in the history
  • Loading branch information
flakusha committed Jul 21, 2020
1 parent 7399d50 commit 8aeb564
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sorting_rs"
version = "1.1.0"
version = "1.2.0"
authors = ["flakusha <[email protected]>"]
edition = "2018"
repository = "https://github.com/flakusha/rust_sorting"
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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() |
| ------ | -------------------------------- | ------------------------------------ | -------- | ---------- |
Expand Down
9 changes: 5 additions & 4 deletions benches/sort_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ macro_rules! create_bench {
}

fn bench(c: &mut Criterion) {
let sizes: Vec<usize> = vec![10, 100, 1000, 10_000, 100_000, /*1_000_000,
let sizes: Vec<usize> = vec![10, 100, 1000, 10_000, /*100_000, 1_000_000,
10_000_000*/];

let benchmark = create_bench! {
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/bin/leonardo_numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 16 additions & 14 deletions src/smooth_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -53,11 +52,11 @@ const LEO_NUMS: [usize; 90] = [
3559958832009428377, 5760134388741632239,
];

pub fn smooth_sort<T: PartialOrd + Debug>(input: &mut [T])
pub fn smooth_sort<T: PartialOrd>(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::<usize>::new();
Expand All @@ -67,16 +66,13 @@ pub fn smooth_sort<T: PartialOrd + Debug>(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 {
Expand All @@ -92,7 +88,6 @@ pub fn smooth_sort<T: PartialOrd + Debug>(input: &mut [T])
restore_heap(input, t[0], &heap);
}
}
println!("DEBUG: {:?}", input);
}

fn restore_heap<T: PartialOrd>(input: &mut [T], index: usize, heap: &Vec<usize>)
Expand All @@ -104,8 +99,8 @@ fn restore_heap<T: PartialOrd>(input: &mut [T], index: usize, heap: &Vec<usize>)

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;
Expand All @@ -114,7 +109,8 @@ fn restore_heap<T: PartialOrd>(input: &mut [T], index: usize, heap: &Vec<usize>)
break;
}
}
while k > 2 {

while k >= 2 {
let t = get_child_trees(i, k);
// tr kr tl kl
// 0 1 2 3
Expand Down Expand Up @@ -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<i32> = vec![];
smooth_sort(&mut vector_in);
Expand Down

0 comments on commit 8aeb564

Please sign in to comment.