Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rust quicksort #292

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions 04_quicksort/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions 04_quicksort/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "quicksort"
version = "0.1.0"
edition = "2021"

[dependencies]
140 changes: 140 additions & 0 deletions 04_quicksort/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
fn rec_sum(list: &[usize]) -> usize {
match list.get(0) {
Some(x) => x + rec_sum(&list[1..]),
None => 0,
}
}

fn rec_count<T>(list: &[T]) -> usize {
match list.get(0) {
Some(_) => 1 + rec_count(&list[1..]),
None => 0,
}
}

fn maximum<T: Ord>(list: &[T]) -> Option<&T> {
match list.get(0) {
Some(x) => match maximum(&list[1..]) {
Some(max) => {
if x > max {
Some(x)
} else {
Some(max)
}
}
None => Some(x),
},
None => None,
}
}

fn binary_search<T: Ord>(list: &[T], item: T) -> Option<usize> {
let mid = list.len() / 2;

match list.get(mid) {
None => None,
Some(val) => {
if *val == item {
Some(mid)
} else if *val > item {
let sublist = &list[..mid];
binary_search(sublist, item)
} else {
let sublist = &list[(mid + 1)..];
binary_search(sublist, item)
}
}
}
}

fn quicksort<T: Ord + Clone>(list: &Vec<T>) -> Vec<T> {
if list.len() < 2 {
list.to_vec()
} else {
let pivot = &list[0];

let mut less = vec![];
let mut greater = vec![];

for x in &list[1..] {
if x <= pivot {
less.push(x.clone());
} else {
greater.push(x.clone());
}
}

let mut new = Vec::with_capacity(list.len());
new.append(&mut quicksort(&less));
new.push(pivot.clone());
new.append(&mut quicksort(&greater));
new
}
}

fn main() {
let list = vec![10, 5, 2, 12, 3];

println!("quicksort: {:?}", quicksort(&list));
println!("binary search: {:?}", binary_search(&list, 3));
println!("sum: {}", rec_sum(&list));
println!("count: {}", rec_count(&list));
println!("maximum: {:?}", maximum(&list));
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn simple_rec_sum() {
let list = [2, 4, 6];
let expected = 12;

let result = rec_sum(&list);

assert_eq!(result, expected);
}

#[test]
fn simple_rec_count() {
let list = [2, 4, 6];
let expected = 3;

let result = rec_count(&list);

assert_eq!(result, expected);
}

#[test]
fn simple_maximum() {
let list = [2, 4, 6, 3];
let expected = 6;

let result = maximum(&list);

assert_eq!(result, Some(&expected));
}

#[test]
fn simple_binary_search() {
let list = &[2, 3, 4, 13, 40];
let item = 3;
let expected = Some(1);

let result = binary_search(list, item);

assert_eq!(result, expected);
}

#[test]
fn search_for_nonexistent_item() {
let list = &[2, 3, 4, 13, 40];
let item = 100;
let expected = None;

let result = binary_search(list, item);

assert_eq!(result, expected);
}
}