2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,4 +1,4 @@
// Type alias for function that returns true if arguments are in the correct order
// Type alias for function that returns true if arguments should be swapped
type OrderFunc<T> = Fn(&T, &T) -> bool;
fn main() {
@ -6,25 +6,24 @@ fn main() {
let mut numbers = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1];
println!("Before: {:?}", numbers);
quick_sort(&mut numbers, &f);
quick_sort(&mut numbers, &is_less);
println!("After: {:?}", numbers);
// Sort strings
let mut strings = ["beach", "hotel", "airplane", "car", "house", "art"];
println!("Before: {:?}", strings);
quick_sort(&mut strings, &f);
quick_sort(&mut strings, &is_less);
println!("After: {:?}", strings);
}
// Example OrderFunc which is used to order items from least to greatest
#[inline]
fn f<T: Ord>(x: &T, y: &T) -> bool {
#[inline(always)]
fn is_less<T: Ord>(x: &T, y: &T) -> bool {
x < y
}
// We use in place quick sort
// For details see http://en.wikipedia.org/wiki/Quicksort#In-place_version
fn quick_sort<T>(v: &mut [T], f: &OrderFunc<T>) {
let len = v.len();
@ -41,9 +40,6 @@ fn quick_sort<T>(v: &mut [T], f: &OrderFunc<T>) {
quick_sort(&mut v[pivot_index + 1..len], f);
}
// Reorders the slice with values lower than the pivot at the left side,
// and values bigger than it at the right side.
// Also returns the store index.
fn partition<T>(v: &mut [T], f: &OrderFunc<T>) -> usize {
let len = v.len();
let pivot_index = len / 2;