Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,53 @@
|
|||
fn main() {
|
||||
println!("Sort numbers in descending order");
|
||||
let mut numbers = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1];
|
||||
println!("Before: {:?}", numbers);
|
||||
|
||||
quick_sort(&mut numbers, &|x,y| x > y);
|
||||
println!("After: {:?}\n", numbers);
|
||||
|
||||
println!("Sort strings alphabetically");
|
||||
let mut strings = ["beach", "hotel", "airplane", "car", "house", "art"];
|
||||
println!("Before: {:?}", strings);
|
||||
|
||||
quick_sort(&mut strings, &|x,y| x < y);
|
||||
println!("After: {:?}\n", strings);
|
||||
|
||||
println!("Sort strings by length");
|
||||
println!("Before: {:?}", strings);
|
||||
|
||||
quick_sort(&mut strings, &|x,y| x.len() < y.len());
|
||||
println!("After: {:?}", strings);
|
||||
}
|
||||
|
||||
fn quick_sort<T,F>(v: &mut [T], f: &F)
|
||||
where F: Fn(&T,&T) -> bool
|
||||
{
|
||||
let len = v.len();
|
||||
if len >= 2 {
|
||||
let pivot_index = partition(v, f);
|
||||
quick_sort(&mut v[0..pivot_index], f);
|
||||
quick_sort(&mut v[pivot_index + 1..len], f);
|
||||
}
|
||||
}
|
||||
|
||||
fn partition<T,F>(v: &mut [T], f: &F) -> usize
|
||||
where F: Fn(&T,&T) -> bool
|
||||
{
|
||||
let len = v.len();
|
||||
let pivot_index = len / 2;
|
||||
let last_index = len - 1;
|
||||
|
||||
v.swap(pivot_index, last_index);
|
||||
|
||||
let mut store_index = 0;
|
||||
for i in 0..last_index {
|
||||
if f(&v[i], &v[last_index]) {
|
||||
v.swap(i, store_index);
|
||||
store_index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
v.swap(store_index, len - 1);
|
||||
store_index
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
fn main() {
|
||||
let numbers = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1];
|
||||
println!("{:?}\n", quick_sort(numbers.iter()));
|
||||
}
|
||||
|
||||
fn quick_sort<T, E>(mut v: T) -> Vec<E>
|
||||
where
|
||||
T: Iterator<Item = E>,
|
||||
E: PartialOrd,
|
||||
{
|
||||
match v.next() {
|
||||
None => Vec::new(),
|
||||
|
||||
Some(pivot) => {
|
||||
let (lower, higher): (Vec<_>, Vec<_>) = v.partition(|it| it < &pivot);
|
||||
let lower = quick_sort(lower.into_iter());
|
||||
let higher = quick_sort(higher.into_iter());
|
||||
lower.into_iter()
|
||||
.chain(core::iter::once(pivot))
|
||||
.chain(higher.into_iter())
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue