Skip to content

Commit 8aeb564

Browse files
committed
Finished working on smoothsort, bumped version
1 parent 7399d50 commit 8aeb564

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sorting_rs"
3-
version = "1.1.0"
3+
version = "1.2.0"
44
authors = ["flakusha <[email protected]>"]
55
edition = "2018"
66
repository = "https://github.com/flakusha/rust_sorting"

README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# sorting_rs
22
Sorting algorithms implemented in Rust
33
## Usage
4-
1. Add this dependency with it's version into your Cargo.toml:
4+
1. Add this dependency and please consider it's version into your Cargo.toml:
55
```toml
66
...
77
[dependencies]
8-
sorting_rs = "1.1.0"
8+
sorting_rs = "1.2.0"
99
...
1010
```
1111
2. Use available sorting algorithms in your Rust code:
@@ -17,8 +17,7 @@ use sorting_rs::*;
1717
please read modules documentation
1818

1919
## This library contains following sorting algorithms:
20-
New algorithms implementations are planned, smooth sort is not ready at the
21-
moment, for example
20+
New algorithms implementations are planned
2221

2322
| Sorting algorithm | Features and downsides | Worst-case performance O(): comparisons; swaps | Best-case performance O(): comparisons; swaps | Space complexity O() |
2423
| ------ | -------------------------------- | ------------------------------------ | -------- | ---------- |

benches/sort_benchmark.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ macro_rules! create_bench {
3030
}
3131

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

3636
let benchmark = create_bench! {
@@ -47,9 +47,10 @@ fn bench(c: &mut Criterion) {
4747
quick_sort,
4848
selection_sort,
4949
shell_sort,
50-
slow_sort,
51-
// smooth_sort,
52-
stooge_sort
50+
smooth_sort
51+
// Exclude extremely slow sorts
52+
// slow_sort,
53+
// stooge_sort
5354
};
5455

5556
c.bench("sort_bench", benchmark);

src/bin/leonardo_numbers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/// 32-, 64-, 128-bit and other systems
55
/// This addition uses usize in case there is mainstream 64-bit system
66
/// # Usage:
7-
/// ```no_run
7+
/// ```ignore
88
/// cargo run leonardo_numbers
99
/// ```
1010
use std::io;

src/smooth_sort.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/// largest remaining element
1010
/// This algorithm makes use of Leonardo numbers. It's a sequence of numbers
1111
/// defined by:
12-
/// ```no_run
12+
/// ```ignore
1313
/// L(0) = 1
1414
/// L(1) = 1
1515
/// L(n) = L(n - 1) + L(n - 2) + 1
@@ -32,7 +32,6 @@
3232
/// sorting_rs::smooth_sort(&mut strings);
3333
/// assert_eq!(strings, &["cargo", "rustc", "rustup"]);
3434
/// ```
35-
use std::fmt::Debug;
3635
3736
const LEO_NUMS: [usize; 90] = [
3837
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] = [
5352
3559958832009428377, 5760134388741632239,
5453
];
5554

56-
pub fn smooth_sort<T: PartialOrd + Debug>(input: &mut [T])
55+
pub fn smooth_sort<T: PartialOrd>(input: &mut [T])
5756
{
5857
if input.len() < 2 {return;}
5958

60-
// Init addtitional heap
59+
// Init addtitional index heap
6160
let input = input;
6261
let in_len = input.len();
6362
let mut heap = Vec::<usize>::new();
@@ -67,16 +66,13 @@ pub fn smooth_sort<T: PartialOrd + Debug>(input: &mut [T])
6766
heap.pop();
6867
let len_leo = heap.len();
6968
heap[len_leo - 1] += 1;
69+
} else if heap.len() >= 1 && heap[heap.len() - 1] == 1 {
70+
heap.push(0);
7071
} else {
71-
if heap.len() >= 1 && heap[heap.len() - 1] == 1 {
72-
heap.push(0);
73-
} else {
74-
heap.push(1);
75-
}
72+
heap.push(1);
7673
}
7774
restore_heap(input, i, &heap);
7875
}
79-
println!("DEBUG: {:?}", input);
8076

8177
for i in (0..in_len).rev() {
8278
if heap[heap.len() - 1] < 2 {
@@ -92,7 +88,6 @@ pub fn smooth_sort<T: PartialOrd + Debug>(input: &mut [T])
9288
restore_heap(input, t[0], &heap);
9389
}
9490
}
95-
println!("DEBUG: {:?}", input);
9691
}
9792

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

105100
while current > 0 {
106101
let j = i - LEO_NUMS[k];
107-
if input[j] > input[i] && (k < 2 || input[j] > input[i - 1] &&
108-
input[j] > input[i - 2]) {
102+
if input[j] > input[i] &&
103+
(k < 2 || input[j] > input[i - 1] && input[j] > input[i - 2]) {
109104
input.swap(i, j);
110105
i = j;
111106
current -= 1;
@@ -114,7 +109,8 @@ fn restore_heap<T: PartialOrd>(input: &mut [T], index: usize, heap: &Vec<usize>)
114109
break;
115110
}
116111
}
117-
while k > 2 {
112+
113+
while k >= 2 {
118114
let t = get_child_trees(i, k);
119115
// tr kr tl kl
120116
// 0 1 2 3
@@ -153,6 +149,12 @@ mod tests {
153149
debug_assert_eq!(vector_in, &[10, 11, 13, 20]);
154150
}
155151
#[test]
152+
fn test_smooth_01() {
153+
let mut vector_in = vec![20, 10, 11, 13, 24, 9, 2, 1, 8];
154+
smooth_sort(&mut vector_in);
155+
debug_assert_eq!(vector_in, &[1, 2, 8, 9, 10, 11, 13, 20, 24]);
156+
}
157+
#[test]
156158
fn test_smooth_empty() {
157159
let mut vector_in:Vec<i32> = vec![];
158160
smooth_sort(&mut vector_in);

0 commit comments

Comments
 (0)