From c07f6d4919915dacb61c5e50da8d6651c67a3fc9 Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Thu, 16 Nov 2023 14:53:01 +0800 Subject: [PATCH] robert: Enable insertion sort --- .../bin/{insertion_sort.rs.old => insertion_sort.rs} | 12 +++++++----- robert/src/bin/selection_sort.rs | 9 ++------- robert/src/util.rs | 12 ++++++++++++ 3 files changed, 21 insertions(+), 12 deletions(-) rename robert/src/bin/{insertion_sort.rs.old => insertion_sort.rs} (53%) diff --git a/robert/src/bin/insertion_sort.rs.old b/robert/src/bin/insertion_sort.rs similarity index 53% rename from robert/src/bin/insertion_sort.rs.old rename to robert/src/bin/insertion_sort.rs index b3a61b17..67b37e72 100644 --- a/robert/src/bin/insertion_sort.rs.old +++ b/robert/src/bin/insertion_sort.rs @@ -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); } diff --git a/robert/src/bin/selection_sort.rs b/robert/src/bin/selection_sort.rs index 4e0c35ed..31fe9cd2 100644 --- a/robert/src/bin/selection_sort.rs +++ b/robert/src/bin/selection_sort.rs @@ -2,7 +2,7 @@ // 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() { @@ -10,10 +10,5 @@ fn main() { 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); } diff --git a/robert/src/util.rs b/robert/src/util.rs index d685c299..6d4a10d6 100644 --- a/robert/src/util.rs +++ b/robert/src/util.rs @@ -32,6 +32,18 @@ where println!(); } +pub fn show_brief(slice: &[T]) +where + T: fmt::Display, +{ + if slice.len() < 128 { + show(slice); + } else { + show(&slice[..128]); + println!("...\n..."); + } +} + /// Read integers from stdin. /// /// # Panics