Skip to content

Commit

Permalink
robert: Add sort compare
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Nov 22, 2023
1 parent ed72d02 commit db4efc3
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 50 deletions.
58 changes: 56 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions robert/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ edition = "2021"
publish = false

[dependencies]
rand = "0.8.5"
sorts = { path = "../sorts" }
54 changes: 54 additions & 0 deletions robert/src/bin/sort_compare.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2020 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

use rand::Rng;
use std::time::Instant;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Variant {
InsertionSort,
SelectionSort,
ShellSort,
}

fn time_sort(array: &mut [f64], variant: Variant) -> u128 {
let instance = Instant::now();
match variant {
Variant::InsertionSort => sorts::insertion_sort(array),
Variant::SelectionSort => sorts::selection_sort(array),
Variant::ShellSort => sorts::shell_sort(array),
}
return instance.elapsed().as_nanos();
}

fn time_random_input(variant: Variant, len: usize, times: i32) -> u128 {
let mut total = 0;
let mut rng = rand::thread_rng();
let mut array = vec![0.0; len];
for _t in 0..times {
for i in 0..len {
array[i] = rng.gen();
}
total += time_sort(&mut array, variant);
}

return total;
}

fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() != 3 {
panic!("Usage: number times");
}
let num: usize = args[1].parse().expect("Invalid total length");
let times: i32 = args[2].parse().expect("Invalid times");
let t1 = time_random_input(Variant::InsertionSort, num, times);
let t2 = time_random_input(Variant::SelectionSort, num, times);
let t3 = time_random_input(Variant::ShellSort, num, times);

println!("Insertion: {t1}, Selection: {t2}, ShellSort: {t3}");
let ratio1: f64 = (t1 as f64) / (t3 as f64);
let ratio2: f64 = t2 as f64 / t3 as f64;
println!("Insertion ratio: {ratio1:.2}, Selection ratio: {ratio2:.2}, Shell ratio: 1.0");
}
48 changes: 0 additions & 48 deletions robert/src/bin/sort_compare.rs.old

This file was deleted.

0 comments on commit db4efc3

Please sign in to comment.