Skip to content

Commit 6f72ba8

Browse files
authored
Implement sorting algorithms with trait and example sorters. M;ade sk… (#9)
…eletons for QuickSort and MergeSort.
2 parents c711e4d + 3f41bc0 commit 6f72ba8

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed

src/algorithm.rs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
pub trait SortingAlgorithm {
2+
type Input;
3+
type Output;
4+
5+
fn run(&self, input: Self::Input) -> Self::Output;
6+
}
7+
8+
#[cfg(test)]
9+
mod tests {
10+
use super::*;
11+
12+
struct ArraySorter;
13+
impl SortingAlgorithm for ArraySorter {
14+
type Input = [i32; 3];
15+
type Output = [i32; 3];
16+
17+
fn run(&self, input: [i32; 3]) -> [i32; 3] {
18+
let mut output = input;
19+
output.sort();
20+
output
21+
}
22+
}
23+
24+
struct StringSorter;
25+
impl SortingAlgorithm for StringSorter {
26+
type Input = String;
27+
type Output = String;
28+
29+
fn run(&self, input: String) -> String {
30+
let mut output = input.chars().collect::<Vec<char>>();
31+
output.sort();
32+
output.into_iter().collect()
33+
}
34+
}
35+
36+
struct VectorSorter;
37+
impl SortingAlgorithm for VectorSorter {
38+
type Input = Vec<i32>;
39+
type Output = Vec<i32>;
40+
41+
fn run(&self, input: Vec<i32>) -> Vec<i32> {
42+
let mut output = input;
43+
output.sort();
44+
output
45+
}
46+
}
47+
48+
#[test]
49+
fn test_array_sorter() {
50+
let sorter = ArraySorter;
51+
let input = [3, 2, 1];
52+
let output = sorter.run(input);
53+
assert_eq!(output, [1, 2, 3]);
54+
}
55+
56+
#[test]
57+
fn test_string_sorter() {
58+
let sorter = StringSorter;
59+
let input = "cba".to_string();
60+
let output = sorter.run(input);
61+
assert_eq!(output, "abc");
62+
}
63+
64+
#[test]
65+
fn test_vector_sorter() {
66+
let sorter = VectorSorter;
67+
let input = vec![3, 2, 1];
68+
let output = sorter.run(input);
69+
assert_eq!(output, vec![1, 2, 3]);
70+
}
71+
72+
}

src/main.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
mod algorithm;
2+
mod quicksort;
3+
mod mergesort;
4+
5+
use algorithm::SortingAlgorithm;
6+
use quicksort::QuickSort;
7+
use mergesort::MergeSort;
8+
19
fn main() {
2-
println!("Hello, world!");
10+
let sorting_algorithms: Vec<Box<dyn SortingAlgorithm<Input = Vec<i32>, Output = Vec<i32>>>> = vec![Box::new(QuickSort), Box::new(MergeSort)];
11+
sorting_algorithms.iter().for_each(|sorting_algorithm| {
12+
let input = vec![3, 2, 1];
13+
let output = sorting_algorithm.run(input);
14+
println!("{:?}", output);
15+
});
316
}

src/mergesort.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use crate::algorithm::SortingAlgorithm;
2+
3+
pub struct MergeSort;
4+
impl SortingAlgorithm for MergeSort {
5+
type Input = Vec<i32>;
6+
type Output = Vec<i32>;
7+
8+
fn run(&self, mut input: Vec<i32>) -> Vec<i32> {
9+
input
10+
}
11+
}

src/quicksort.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use crate::algorithm::SortingAlgorithm;
2+
3+
pub struct QuickSort;
4+
impl SortingAlgorithm for QuickSort {
5+
type Input = Vec<i32>;
6+
type Output = Vec<i32>;
7+
8+
fn run(&self, mut input: Vec<i32>) -> Vec<i32> {
9+
input
10+
}
11+
}

0 commit comments

Comments
 (0)