Skip to content

Commit 9b8cec7

Browse files
committed
tests and benchmarks for quick n' bubble sort
1 parent 7eec7b2 commit 9b8cec7

File tree

8 files changed

+126
-110
lines changed

8 files changed

+126
-110
lines changed

Cargo.lock

Lines changed: 82 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[package]
2-
name = "Sorting-algorithm"
2+
name = "sorting-algorithm"
33
version = "0.1.0"
4-
edition = "2023"
4+
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dev-dependencies]
9-
criterion = "0.3"
9+
criterion = "0.4"
1010

1111
[[bench]]
1212
name = "sort_benchmark"

benches/sort_benchmark.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
2+
use sorting_algorithm::bubblesort;
3+
4+
fn sort_arrays_benchmark(c: &mut Criterion) {
5+
let mut arr = black_box([-2,-7,2,10,20,9]);
6+
7+
c.bench_function("Bubblesort", |b| b.iter(|| bubblesort(&mut arr)));
8+
9+
}
10+
11+
criterion_group!(benches, sort_arrays_benchmark);
12+
criterion_main!(benches);

benchmarks/sorting_benchmark.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/bubblesort.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
1-
use std::array;
2-
3-
fn bubblesort(array: &mut [i32]) -> &[i32]{
4-
5-
for i in 0..array.len() {
6-
for j in 0..array.len() - 1 {
7-
if array[j] > array[j + 1] {
8-
array.swap(j, j + 1);
1+
pub fn bubblesort<T: Ord>(arr: &mut [T]) {
2+
let len = arr.len();
3+
for i in 0..len {
4+
for j in 0..len - i - 1 {
5+
if arr[j] > arr[j + 1] {
6+
arr.swap(j, j + 1);
7+
}
98
}
109
}
1110
}
12-
array
13-
}
1411

15-
mod tests {
16-
use super::bubblesort;
1712

18-
#[test]
19-
fn test_bubblesort() {
20-
let mut array = [10, 7, 8, 9, 1, 5];
21-
let sorted = bubblesort(&mut array);
22-
assert_eq!(sorted, &[1, 5, 7, 8, 9, 10]);
23-
}
24-
}

src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
mod bubblesort;
2+
mod quciksort;
3+
4+
pub use bubblesort::bubblesort;
5+
pub use quciksort::quicksort;
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::*;
10+
#[test]
11+
fn test_bubble_sort() {
12+
let mut arr = [6, 2, 4, 1, 9, -2, 5];
13+
bubblesort(&mut arr);
14+
assert_eq!(arr, [-2, 1, 2, 4, 5, 6, 9]);
15+
}
16+
#[test]
17+
fn test_quick_sort() {
18+
let mut array = [10, 7, 8, 9, 1, 5];
19+
let sorted = quicksort(&mut array);
20+
assert_eq!(sorted, &[1, 5, 7, 8, 9, 10]);
21+
}
22+
}

src/main.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/quciksort.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,3 @@ pub fn quicksort(array: &mut [i32]) -> &[i32] {
3333

3434
array
3535
}
36-
37-
mod tests {
38-
use super::quicksort;
39-
40-
#[test]
41-
fn test_quicksort() {
42-
let mut array = [10, 7, 8, 9, 1, 5];
43-
let sorted = quicksort(&mut array);
44-
assert_eq!(sorted, &[1, 5, 7, 8, 9, 10]);
45-
}
46-
}

0 commit comments

Comments
 (0)