2020-11-13 20:48:47 +01:00
|
|
|
use std::clone::Clone;
|
2020-11-17 02:24:01 +01:00
|
|
|
use std::cmp::PartialOrd;
|
|
|
|
use std::marker::Copy;
|
|
|
|
use std::vec::Vec;
|
|
|
|
|
|
|
|
fn merge<T: Clone + PartialOrd + Copy>(a: Vec<T>, b: Vec<T>) -> Vec<T> {
|
|
|
|
let mut r = vec![];
|
|
|
|
let mut i = 0;
|
|
|
|
let mut j = 0;
|
2020-11-13 20:48:47 +01:00
|
|
|
|
2020-11-17 02:24:01 +01:00
|
|
|
while i < a.len() && j < b.len() {
|
|
|
|
if a[i] < b[j] {
|
|
|
|
r.push(a[i]);
|
|
|
|
i += 1;
|
|
|
|
} else {
|
|
|
|
r.push(b[j]);
|
|
|
|
j += 1;
|
|
|
|
}
|
|
|
|
}
|
2020-11-13 20:48:47 +01:00
|
|
|
|
2020-11-17 02:24:01 +01:00
|
|
|
while i < a.len() {
|
|
|
|
r.push(a[i]);
|
|
|
|
i += 1;
|
|
|
|
}
|
2020-11-13 20:48:47 +01:00
|
|
|
|
2020-11-17 02:24:01 +01:00
|
|
|
while j < b.len() {
|
|
|
|
r.push(b[j]);
|
|
|
|
j += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn merge_sort<T: Clone + PartialOrd + Copy>(v: Vec<T>) -> Vec<T> {
|
|
|
|
if v.len() <= 1 {
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
let i = v.len() / 2;
|
|
|
|
let a = merge_sort((&v[..i]).to_vec());
|
|
|
|
let b = merge_sort((&v[i..]).to_vec());
|
|
|
|
let r = merge(a, b);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mult_bin(a: i64, b: i64) -> i64 {
|
|
|
|
return a * b;
|
2020-11-13 20:48:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
println!("Hello, world!");
|
2020-11-17 02:24:01 +01:00
|
|
|
let l = vec![4, 2, 1, 3];
|
2020-11-13 20:48:47 +01:00
|
|
|
let l = merge_sort(l);
|
|
|
|
println!("{:?}", l);
|
2020-11-17 02:24:01 +01:00
|
|
|
|
|
|
|
let r = mult_bin(42, 21);
|
|
|
|
println!("{:?}", r);
|
2020-11-13 20:48:47 +01:00
|
|
|
}
|