Skip to content

Commit

Permalink
sort: Add benchmark for shell sort
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Jul 3, 2024
1 parent 53c4900 commit 74bf003
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
6 changes: 5 additions & 1 deletion sort/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ harness = false

[[bench]]
name = "selection_sort"
harness = false
harness = false

[[bench]]
name = "shell_sort"
harness = false
37 changes: 37 additions & 0 deletions sort/benches/shell_sort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

use criterion::{criterion_group, criterion_main, Criterion};

use sort::shell_sort::shell_sort;
use sort::util::random_ints;

fn criterion_benchmark(c: &mut Criterion) {
for exp in 1..5 {
let len: usize = 2 * 10_usize.pow(exp);
let arr = random_ints(len).expect("Failed to generate random integers");
let title1 = format!("std_sort_for_shell_sort {len}");
let title2 = format!("shell_sort {len}");
let mut arr_sorted = arr.clone();
arr_sorted.sort();

c.bench_function(&title1, |b| {
b.iter(|| {
let mut arr1 = arr.clone();
arr1.sort();
assert_eq!(arr1, arr_sorted);
})
});
c.bench_function(&title2, |b| {
b.iter(|| {
let mut arr2 = arr.clone();
shell_sort(&mut arr2);
assert_eq!(arr2, arr_sorted);
})
});
}
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
1 change: 1 addition & 0 deletions src/array/sort/shell-sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@

- 最差情况下的时间复杂度 `O(N^2)`, 空间复杂度是 `O(1)`
- 最好情竞下的时间复杂度是 `Ω(N log(N))`
- 比插入排序快
- 与插入排序不同的时, 希尔排序适合大中型的数组, 对于任意顺序的数组也有效

0 comments on commit 74bf003

Please sign in to comment.