2020-11-17 03:12:09 +01:00
|
|
|
mod merge_sort;
|
2020-11-20 02:37:42 +01:00
|
|
|
mod quick_sort;
|
|
|
|
|
|
|
|
use crate::quick_sort::quick_sort;
|
2020-11-17 03:12:09 +01:00
|
|
|
use crate::merge_sort::merge_sort_inversions;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
|
|
|
use std::io::{BufRead, BufReader, Error, ErrorKind};
|
2020-11-17 02:24:01 +01:00
|
|
|
use std::vec::Vec;
|
|
|
|
|
2020-11-17 03:12:09 +01:00
|
|
|
fn read_to_vector(path: &str) -> Result<Vec<i64>, io::Error> {
|
|
|
|
let file = File::open(path)?;
|
|
|
|
let br = BufReader::new(file);
|
|
|
|
let mut v = Vec::new();
|
|
|
|
for line in br.lines() {
|
|
|
|
let line = line?;
|
|
|
|
let n = line
|
|
|
|
.trim()
|
|
|
|
.parse()
|
|
|
|
.map_err(|e| Error::new(ErrorKind::InvalidData, e))?;
|
|
|
|
v.push(n);
|
2020-11-17 02:24:01 +01:00
|
|
|
}
|
2020-11-17 03:12:09 +01:00
|
|
|
Ok(v)
|
2020-11-17 02:24:01 +01:00
|
|
|
}
|
|
|
|
|
2020-11-20 02:37:42 +01:00
|
|
|
#[allow(dead_code)]
|
2020-11-17 03:12:09 +01:00
|
|
|
fn c1a2() {
|
2020-11-20 02:37:42 +01:00
|
|
|
let vec = read_to_vector("data/course_1_assignment_2.txt").unwrap();
|
2020-11-17 03:12:09 +01:00
|
|
|
let (_sorted, inversions) = merge_sort_inversions(vec);
|
|
|
|
println!("course 1 assignment 2: {:?}", inversions);
|
2020-11-13 20:48:47 +01:00
|
|
|
}
|
|
|
|
|
2020-11-20 14:19:59 +01:00
|
|
|
#[allow(dead_code)]
|
2020-11-20 02:37:42 +01:00
|
|
|
fn c1a3() {
|
2020-11-20 14:19:59 +01:00
|
|
|
let mut vec = read_to_vector("data/course_1_assignment_3.txt").unwrap();
|
2020-11-20 02:37:42 +01:00
|
|
|
let mut comp_acc: usize = 0;
|
|
|
|
quick_sort(&mut vec, &mut comp_acc);
|
2020-11-20 14:19:59 +01:00
|
|
|
println!("course 1 assignment 3: {:?}", comp_acc);
|
2020-11-20 02:37:42 +01:00
|
|
|
/*
|
|
|
|
1: 162085
|
|
|
|
2: 164123
|
2020-11-20 14:19:59 +01:00
|
|
|
3: 138382
|
2020-11-20 02:37:42 +01:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2020-11-13 20:48:47 +01:00
|
|
|
fn main() {
|
2020-11-20 14:19:59 +01:00
|
|
|
c1a2();
|
2020-11-20 02:37:42 +01:00
|
|
|
c1a3();
|
2020-11-13 20:48:47 +01:00
|
|
|
}
|