Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.45 KB | None | 0 0
  1. #![allow(unused_variables)]
  2.  
  3. extern crate rand;
  4.  
  5. use rand::Rng;
  6. use std::time::Instant;
  7.  
  8. const MAX_LENGTH: i32 = 25;
  9. const SIZE:       i32 = 200000;
  10. const TEST_TIMES: u32 = 100;
  11.  
  12. fn verify(vec1: &Vec::<String>) -> bool {
  13.     for x in 0..(SIZE - 1) as usize {
  14.         match (&vec1[x], &vec1[x+1]) {
  15.             (x, y) if x.chars().count() > y.chars().count()           => { println!("definate: {} > {}", x, y); return false },
  16.             (x, y) if x > y && x.chars().count() == y.chars().count() => { println!("definate: {} > {}", x, y); return false },
  17.             (x, y) if y > x                                           => continue,
  18.             _                                                         => continue
  19.         }
  20.     }
  21.     true
  22. }
  23.  
  24. fn gen_random_vector_data(value: &mut Vec::<String>) {
  25.     let mut rng = rand::thread_rng();
  26.    
  27.     for _ in 0..SIZE {
  28.         value.push((0..rng.gen_range(1, MAX_LENGTH + 1)).map(|_| {
  29.             rng.gen_range::<u8>(97, 123) as char
  30.         }).collect());
  31.     }
  32. }
  33.  
  34. fn counting_sort(value: &Vec::<String>, result: &mut Vec::<String>, i: usize) {  
  35.     let mut table = vec![0 as usize; 27]; //reset table at start of loop
  36.    
  37.     for item in value.iter() {
  38.         let index: usize = match item.chars().rev().nth(i) {
  39.             Some(x) => x as usize - 96,
  40.             None    => 0
  41.         };
  42.    
  43.         table[index] += 1;
  44.     }
  45.  
  46.     for x in 0..26 {
  47.         table[x+1] += table[x];
  48.     }  
  49.     for x in (0..26).rev() {
  50.         table[x+1] = table[x];    
  51.     }
  52.     table[0] = 0;
  53.  
  54.     for item in value.iter() {
  55.         let index: usize = match item.chars().rev().nth(i) {
  56.             Some(x) => x as usize - 96,
  57.             None    => 0
  58.         };
  59.        
  60.         result[table[index]] = item.to_string();
  61.         table[index] += 1;
  62.     }
  63. }
  64.  
  65. fn radix_sort(mut value: Vec::<String>, mut result: &mut Vec::<String>) {
  66.     for i in 0 .. (MAX_LENGTH) as usize {
  67.         if i % 2 == 0 {
  68.             counting_sort(&value, &mut result, i);
  69.         } else {
  70.             counting_sort(&result, &mut value, i);
  71.         }
  72.     }
  73.  
  74.     if MAX_LENGTH % 2 == 0 {
  75.         *result = value;
  76.     }
  77. }
  78.  
  79. fn main() {
  80.     //vars for timiming individual parts of functions
  81.     let mut total_time_sort:   _ = std::time::Duration::new(0, 0);
  82.     let mut total_time_verify: _ = std::time::Duration::new(0, 0);
  83.     let mut total_time_gen:    _ = std::time::Duration::new(0, 0);
  84.     let mut result = vec!["".to_string(); SIZE as usize]; //vec for our results
  85.    
  86.     for x in 0..TEST_TIMES {
  87.         let mut value: Vec<String> = Vec::<String>::with_capacity(SIZE as usize); //vec for our random values
  88.  
  89.         let start_gen = Instant::now();
  90.         gen_random_vector_data(&mut value);
  91.         total_time_gen += Instant::now().duration_since(start_gen);
  92.  
  93.         let start_sort = Instant::now();
  94.         radix_sort(value, &mut result);
  95.         total_time_sort += Instant::now().duration_since(start_sort);
  96.  
  97.         //println!("({}) sorted data: {:?}", x, result);
  98.         let start_verify = Instant::now();
  99.         if !verify(&result) { break; } //verify if data is correct
  100.         total_time_verify += Instant::now().duration_since(start_verify);
  101.     }
  102.     println!("average time to generate data: {:?}", total_time_gen / TEST_TIMES);
  103.     println!("average time to sort data:     {:?}", total_time_sort / TEST_TIMES);
  104.     println!("average time to verify data:   {:?}", total_time_verify / TEST_TIMES);
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement