From 9ad6aaf8be7aca2688f65da3265d3ac89cbd1b0f Mon Sep 17 00:00:00 2001 From: semihbkgr Date: Sat, 10 Feb 2024 14:14:16 +0300 Subject: [PATCH] adjust operation indexes --- src/main.rs | 30 +++++++++++++++--------------- src/sorting/bubble_sort.rs | 5 +++++ src/sorting/mod.rs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3db7fac..cfabdd8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -136,18 +136,18 @@ impl AlgorithmUI { result.push('\n'); } let mut text = Text::raw(result); - match operation { + match operation.adjusted() { Operation::Compare(a, b) => { for line in text.lines.iter_mut() { let line_content = line.spans[0].content.clone(); let line_chars = line_content.chars().into_iter().collect::>(); - let pre = Span::raw(line_chars[..*a].iter().collect::()); - let a_span = Span::raw(line_chars[*a..*a + 1].iter().collect::()) + let pre = Span::raw(line_chars[..a].iter().collect::()); + let a_span = Span::raw(line_chars[a..a + 1].iter().collect::()) .fg(Color::LightCyan); - let mid = Span::raw(line_chars[*a + 1..*b].iter().collect::()); - let b_span = Span::raw(line_chars[*b..*b + 1].iter().collect::()) + let mid = Span::raw(line_chars[a + 1..b].iter().collect::()); + let b_span = Span::raw(line_chars[b..b + 1].iter().collect::()) .fg(Color::LightCyan); - let last = Span::raw(line_chars[*b + 1..].iter().collect::()); + let last = Span::raw(line_chars[b + 1..].iter().collect::()); line.spans = vec![pre, a_span, mid, b_span, last]; } @@ -156,13 +156,13 @@ impl AlgorithmUI { for line in text.lines.iter_mut() { let line_content = line.spans[0].content.clone(); let line_chars = line_content.chars().into_iter().collect::>(); - let pre = Span::raw(line_chars[..*a].iter().collect::()); - let a_span = Span::raw(line_chars[*a..*a + 1].iter().collect::()) + let pre = Span::raw(line_chars[..a].iter().collect::()); + let a_span = Span::raw(line_chars[a..a + 1].iter().collect::()) .fg(Color::LightGreen); - let mid = Span::raw(line_chars[*a + 1..*b].iter().collect::()); - let b_span = Span::raw(line_chars[*b..*b + 1].iter().collect::()) + let mid = Span::raw(line_chars[a + 1..b].iter().collect::()); + let b_span = Span::raw(line_chars[b..b + 1].iter().collect::()) .fg(Color::LightGreen); - let last = Span::raw(line_chars[*b + 1..].iter().collect::()); + let last = Span::raw(line_chars[b + 1..].iter().collect::()); line.spans = vec![pre, a_span, mid, b_span, last]; } @@ -171,10 +171,10 @@ impl AlgorithmUI { for line in text.lines.iter_mut() { let line_content = line.spans[0].content.clone(); let line_chars = line_content.chars().into_iter().collect::>(); - let pre = Span::raw(line_chars[..*i].iter().collect::()); - let span = Span::raw(line_chars[*i..*i + 1].iter().collect::()) + let pre = Span::raw(line_chars[..i].iter().collect::()); + let span = Span::raw(line_chars[i..i + 1].iter().collect::()) .fg(Color::LightYellow); - let last = Span::raw(line_chars[*i + 1..].iter().collect::()); + let last = Span::raw(line_chars[i + 1..].iter().collect::()); line.spans = vec![pre, span, last]; } @@ -378,7 +378,7 @@ fn ui(frame: &mut Frame, app: &mut App) { if !algorithm.auto_next { let (step, operation) = algorithm.status.step_info(); - let info = format!("step: {}\n{}", step, operation); + let info = format!("step: {}\n{}", step, operation.adjusted()); let text_info = Text::from(info); let paragraph_info = Paragraph::new(text_info).alignment(Alignment::Left); let next_area = next_area_vertical(area, 2, 1); diff --git a/src/sorting/bubble_sort.rs b/src/sorting/bubble_sort.rs index c67ee5d..150219e 100644 --- a/src/sorting/bubble_sort.rs +++ b/src/sorting/bubble_sort.rs @@ -8,13 +8,18 @@ pub const NAME: &str = "bubble sort"; pub fn sort(nums: &mut [i32], ctx: &dyn AlgorithmContext) { let len = nums.len(); for i in 0..len { + let mut swapped = false; for j in 0..len - i - 1 { ctx.next(Compare(j, j + 1), nums.to_vec()); if nums[j] > nums[j + 1] { nums.swap(j, j + 1); ctx.next(Swap(j, j + 1), nums.to_vec()); + swapped = true; } } + if !swapped { + break; + } } ctx.next(Noop(), nums.to_vec()); } diff --git a/src/sorting/mod.rs b/src/sorting/mod.rs index f2efb9c..4c7dad6 100644 --- a/src/sorting/mod.rs +++ b/src/sorting/mod.rs @@ -1,9 +1,13 @@ use std::fmt::Display; pub mod bubble_sort; +pub mod comb_sort; +pub mod heap_sort; pub mod insertion_sort; pub mod merge_sort; +pub mod quick_sort; pub mod selection_sort; +pub mod shell_sort; pub trait AlgorithmContext { fn next(&self, operation: Operation, nums: Vec); @@ -17,6 +21,29 @@ pub enum Operation { Insert(usize), } +impl Operation { + pub fn adjusted(&self) -> Self { + return match *self { + Self::Compare(a, b) => { + if a > b { + Self::Compare(b, a) + } else { + Self::Compare(a, b) + } + } + Self::Swap(a, b) => { + if a > b { + Self::Swap(b, a) + } else { + Self::Swap(a, b) + } + } + Self::Insert(i) => return Self::Insert(i), + Self::Noop() => return Self::Noop(), + }; + } +} + impl Display for Operation { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -40,6 +67,10 @@ pub fn get_algorithms() -> Vec<&'static str> { selection_sort::NAME, insertion_sort::NAME, merge_sort::NAME, + shell_sort::NAME, + heap_sort::NAME, + quick_sort::NAME, + comb_sort::NAME, ]; } @@ -49,6 +80,10 @@ pub fn get_algorithm_func<'a>(s: &str) -> impl FnOnce(&mut [i32], &dyn Algorithm selection_sort::NAME => selection_sort::sort, insertion_sort::NAME => insertion_sort::sort, merge_sort::NAME => merge_sort::sort, + shell_sort::NAME => shell_sort::sort, + heap_sort::NAME => heap_sort::sort, + quick_sort::NAME => quick_sort::sort, + comb_sort::NAME => comb_sort::sort, _ => panic!("algorithm not found"), } }