algos/src/main.rs

58 lines
1.1 KiB
Rust
Raw Normal View History

use std::clone::Clone;
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;
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;
}
}
while i < a.len() {
r.push(a[i]);
i += 1;
}
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;
}
fn main() {
println!("Hello, world!");
let l = vec![4, 2, 1, 3];
let l = merge_sort(l);
println!("{:?}", l);
let r = mult_bin(42, 21);
println!("{:?}", r);
}