From b31fe7176c37ae385fc1ce6eae96be28217fc280 Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Thu, 3 Dec 2020 19:52:15 -0500 Subject: [PATCH] Run rustfmt on all rs files --- src/main.rs | 43 +++++++++++----------- src/min_cut.rs | 82 +++++++++++++++++++++--------------------- src/quick_sort.rs | 92 +++++++++++++++++++++++------------------------ src/ssc.rs | 2 +- src/util.rs | 13 ++++--- 5 files changed, 118 insertions(+), 114 deletions(-) diff --git a/src/main.rs b/src/main.rs index 584ffe5..45640da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,17 @@ mod merge_sort; +mod min_cut; mod quick_sort; mod ssc; -mod min_cut; mod util; use crate::util::read_to_graph; -use crate::util::read_to_directed_graph; -use crate::util::read_to_vector; + +use crate::merge_sort::merge_sort_inversions; use crate::min_cut::min_cut; use crate::quick_sort::quick_sort; -use crate::merge_sort::merge_sort_inversions; use crate::ssc::ssc; +use crate::util::read_to_directed_graph; +use crate::util::read_to_vector; #[allow(dead_code)] fn c1a2() { @@ -26,32 +27,32 @@ fn c1a3() { quick_sort(&mut vec, &mut comp_acc); println!("course 1 assignment 3: {:?}", comp_acc); /* - 1: 162085 - 2: 164123 - 3: 138382 + 1: 162085 + 2: 164123 + 3: 138382 */ } #[allow(dead_code)] fn c1a4() { - let g = read_to_graph("data/course_1_assignment_4.txt").unwrap(); - let mut smalles_min_cut = u32::MAX; - let iterations = g.nodes.len().pow(1); - for _ in 0..iterations { - let new_min_cut = min_cut(g.clone()); - if new_min_cut < smalles_min_cut { - smalles_min_cut = new_min_cut; - } - } - /* 17 */ - println!("course 1 assignment 4: {:?}", smalles_min_cut); + let g = read_to_graph("data/course_1_assignment_4.txt").unwrap(); + let mut smalles_min_cut = u32::MAX; + let iterations = g.nodes.len().pow(1); + for _ in 0..iterations { + let new_min_cut = min_cut(g.clone()); + if new_min_cut < smalles_min_cut { + smalles_min_cut = new_min_cut; + } + } + /* 17 */ + println!("course 1 assignment 4: {:?}", smalles_min_cut); } #[allow(dead_code)] fn c2a1() { - let g = read_to_directed_graph("data/course_2_assignment_1.txt").unwrap(); - let s = ssc(g); - println!("{:?}", s); + let g = read_to_directed_graph("data/course_2_assignment_1.txt").unwrap(); + let s = ssc(g); + println!("{:?}", s); } fn main() { diff --git a/src/min_cut.rs b/src/min_cut.rs index dd6bd90..132b2a7 100644 --- a/src/min_cut.rs +++ b/src/min_cut.rs @@ -1,50 +1,50 @@ -use rand::Rng; use crate::util::Graph; +use rand::Rng; use std::convert::TryInto; pub fn min_cut(mut g: Graph) -> u32 { - let mut rng = rand::thread_rng(); - // println!("min_cut({:?})\n", g); - while g.nodes.len() > 2 { - // pick random edge (u, v) - let edge_index = rng.gen_range(0, g.edges.len()); - let removed_edge = g.edges.swap_remove(edge_index); + let mut rng = rand::thread_rng(); + // println!("min_cut({:?})\n", g); + while g.nodes.len() > 2 { + // pick random edge (u, v) + let edge_index = rng.gen_range(0, g.edges.len()); + let removed_edge = g.edges.swap_remove(edge_index); - // merge u and v into a single node u - let mut self_loops = vec![]; - let u = removed_edge.0; - let v = removed_edge.1; - for i in 0..g.edges.len() { - // replace all v with u - if g.edges[i].0 == v { - g.edges[i].0 = u; - } - if g.edges[i].1 == v { - g.edges[i].1 = u; - } - // make sure lower node comes first - if g.edges[i].0 > g.edges[i].1 { - g.edges[i] = (g.edges[i].1, g.edges[i]. 0); - } + // merge u and v into a single node u + let mut self_loops = vec![]; + let u = removed_edge.0; + let v = removed_edge.1; + for i in 0..g.edges.len() { + // replace all v with u + if g.edges[i].0 == v { + g.edges[i].0 = u; + } + if g.edges[i].1 == v { + g.edges[i].1 = u; + } + // make sure lower node comes first + if g.edges[i].0 > g.edges[i].1 { + g.edges[i] = (g.edges[i].1, g.edges[i].0); + } - if g.edges[i].0 == g.edges[i].1 { - self_loops.push(i); - } - } + if g.edges[i].0 == g.edges[i].1 { + self_loops.push(i); + } + } - // remove v from nodes - g.nodes.pop(); // we only use the nodes.len() - // if let Some(i) = g.nodes.iter().position(|x| *x == v) { - // g.nodes.swap_remove(i); - // } + // remove v from nodes + g.nodes.pop(); // we only use the nodes.len() + // if let Some(i) = g.nodes.iter().position(|x| *x == v) { + // g.nodes.swap_remove(i); + // } - // remove self-loops - let mut self_loops_removed: usize = 0; - for i in self_loops { - g.edges.remove(i - self_loops_removed); - self_loops_removed += 1; - } - } - // return cut represented by the final two nodes - g.edges.len().try_into().unwrap() + // remove self-loops + let mut self_loops_removed: usize = 0; + for i in self_loops { + g.edges.remove(i - self_loops_removed); + self_loops_removed += 1; + } + } + // return cut represented by the final two nodes + g.edges.len().try_into().unwrap() } diff --git a/src/quick_sort.rs b/src/quick_sort.rs index adc1453..1d9b0db 100644 --- a/src/quick_sort.rs +++ b/src/quick_sort.rs @@ -1,52 +1,50 @@ use std::cmp::PartialOrd; +fn median_of_three(v: &[T]) -> usize { + if v.len() < 3 { + return 0; + } -fn median_of_three( v: &[T]) -> usize { - if v.len() < 3 { - return 0; - } - - let left = 0; - let middle = if v.len() % 2 == 0 { - v.len() / 2 - 1 - } else { - v.len() / 2 - }; - let right = v.len() - 1; - let mut values = vec![v[left], v[middle], v[right]]; - bubble_sort(&mut values); - let median = values[1]; - if median == v[left] { - return left; - } else if median == v[middle] { - return middle; - } else if median == v[right]{ - return right; - } else { - panic!("Did not find median in values!"); - } + let left = 0; + let middle = if v.len() % 2 == 0 { + v.len() / 2 - 1 + } else { + v.len() / 2 + }; + let right = v.len() - 1; + let mut values = vec![v[left], v[middle], v[right]]; + bubble_sort(&mut values); + let median = values[1]; + if median == v[left] { + return left; + } else if median == v[middle] { + return middle; + } else if median == v[right] { + return right; + } else { + panic!("Did not find median in values!"); + } } -fn bubble_sort( v: &mut [T]) -> () { - let mut unsorted = true; - if v.len() <= 1 { - return - } - while unsorted { - unsorted = false; - for i in 0..v.len() - 1 { - if v[i] > v[i + 1] { - v.swap(i, i + 1); - unsorted = true; - } - } - } - -} - -pub fn quick_sort( v: &mut [T], comp_acc: &mut usize) -> () { +fn bubble_sort(v: &mut [T]) -> () { + let mut unsorted = true; if v.len() <= 1 { - return; + return; + } + while unsorted { + unsorted = false; + for i in 0..v.len() - 1 { + if v[i] > v[i + 1] { + v.swap(i, i + 1); + unsorted = true; + } + } + } +} + +pub fn quick_sort(v: &mut [T], comp_acc: &mut usize) -> () { + if v.len() <= 1 { + return; } // let pivot_index = 0; // let pivot_index = v.len() - 1; @@ -54,10 +52,10 @@ pub fn quick_sort( v: &mut [T], comp_acc: &mut usize) -> ( let mut i = 1; v.swap(0, pivot_index); for j in 1..v.len() { - if v[j] < v[0] { - v.swap(i, j); - i += 1; - } + if v[j] < v[0] { + v.swap(i, j); + i += 1; + } } v.swap(0, i - 1); quick_sort(&mut v[..i - 1], comp_acc); diff --git a/src/ssc.rs b/src/ssc.rs index 38b3c6b..0f9892a 100644 --- a/src/ssc.rs +++ b/src/ssc.rs @@ -1,5 +1,5 @@ use crate::util::Graph; pub fn ssc(_g: Graph) -> u32 { - 0 + 0 } diff --git a/src/util.rs b/src/util.rs index b8ddf1b..454975f 100644 --- a/src/util.rs +++ b/src/util.rs @@ -24,9 +24,11 @@ pub fn read_to_vector(path: &str) -> Result, io::Error> { Ok(v) } - pub fn read_to_graph(path: &str) -> Result { - let mut g = Graph { nodes: vec![], edges: vec![] }; + let mut g = Graph { + nodes: vec![], + edges: vec![], + }; let file = File::open(path)?; let br = BufReader::new(file); @@ -44,14 +46,17 @@ pub fn read_to_graph(path: &str) -> Result { if neightbor > current_node { let edge = (current_node, neightbor); g.edges.push(edge); - } + } } } Ok(g) } pub fn read_to_directed_graph(path: &str) -> Result { - let mut g = Graph { nodes: vec![], edges: vec![] }; + let mut g = Graph { + nodes: vec![], + edges: vec![], + }; let file = File::open(path)?; let br = BufReader::new(file);