Skip to content

Commit

Permalink
robert: Enable insertion sort
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Nov 16, 2023
1 parent 783d023 commit c07f6d4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

use base::ints;
use robert::util::{is_sorted, read_ints, show_brief};
use sorts::insertion_sort;

fn main() {
let mut list = ints::read_ints();
let mut list = read_ints();
println!("[InsertionSort] LIST:");
sort::generics::show(&list);
sort::insertion_sort(&mut list);
show_brief(&list);
insertion_sort(&mut list);
println!("RESULT:");
sort::generics::show(&list);
assert!(is_sorted(&list));
show_brief(&list);
}
9 changes: 2 additions & 7 deletions robert/src/bin/selection_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

use robert::util::{is_sorted, read_ints, show};
use robert::util::{is_sorted, read_ints, show_brief};
use sorts::selection_sort;

fn main() {
let mut list = read_ints();
selection_sort(&mut list);
assert!(is_sorted(&list));
println!("RESULT:");
if list.len() < 128 {
show(&list);
} else {
show(&list[..128]);
println!("...\n...");
}
show_brief(&list);
}
12 changes: 12 additions & 0 deletions robert/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ where
println!();
}

pub fn show_brief<T>(slice: &[T])
where
T: fmt::Display,
{
if slice.len() < 128 {
show(slice);
} else {
show(&slice[..128]);
println!("...\n...");
}
}

/// Read integers from stdin.
///
/// # Panics
Expand Down

0 comments on commit c07f6d4

Please sign in to comment.