Skip to content
Merged
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
53 changes: 52 additions & 1 deletion src/sorting/quick_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,65 @@ pub fn partition<T: PartialOrd>(arr: &mut [T], lo: isize, hi: isize) -> isize {
arr.swap(i as usize, pivot as usize);
i
}

fn _quick_sort<T: Ord>(arr: &mut [T], lo: isize, hi: isize) {
if lo < hi {
let p = partition(arr, lo, hi);
_quick_sort(arr, lo, p - 1);
_quick_sort(arr, p + 1, hi);
}
}

pub fn quick_sort<T: Ord>(arr: &mut [T]) {
let len = arr.len();
_quick_sort(arr, 0, (len - 1) as isize);
if len > 1 {
_quick_sort(arr, 0, (len - 1) as isize);
}
}

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

#[test]
fn basic() {
let mut res = vec![10, 8, 4, 3, 1, 9, 2, 7, 5, 6];
quick_sort(&mut res);
assert_eq!(res, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}

#[test]
fn basic_string() {
let mut res = vec!["a", "bb", "d", "cc"];
quick_sort(&mut res);
assert_eq!(res, vec!["a", "bb", "cc", "d"]);
}

#[test]
fn empty() {
let mut res = Vec::<u8>::new();
quick_sort(&mut res);
assert_eq!(res, vec![]);
}

#[test]
fn one_element() {
let mut res = vec![1];
quick_sort(&mut res);
assert_eq!(res, vec![1]);
}

#[test]
fn pre_sorted() {
let mut res = vec![1, 2, 3, 4];
quick_sort(&mut res);
assert_eq!(res, vec![1, 2, 3, 4]);
}

#[test]
fn reverse_sorted() {
let mut res = vec![4, 3, 2, 1];
quick_sort(&mut res);
assert_eq!(res, vec![1, 2, 3, 4]);
}
}