daily pastebin goal
78%
SHARE
TWEET

Untitled

a guest Jan 20th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fn main() {
  2.     let mut output: Vec<i16> = Vec::new();
  3.     let m = merge_sort(&vec![2,5,3,1,0], &mut output);
  4.     // let m = merge(&vec![2,5,3,1,0], &vec![-1,4,6]);
  5.     // print!("{:?}", m)
  6. }
  7.  
  8. fn build_vector() -> Vec<i16> {
  9.     let mut v = Vec::new();
  10.     v.push(11);
  11.     v.push(12);
  12.     v
  13. }
  14.  
  15. fn overflow() {
  16.     let max = std::i32::MAX;
  17.     let max_plus_one = max.wrapping_add(2);
  18.     print!("{}", max_plus_one);
  19. }
  20.  
  21. fn byte_values() {
  22.     print!("{}", b'?');
  23. }
  24.  
  25. fn math() {
  26.     // println!("{}", 2i16.pow(11));
  27.     // println!("{}", (-2i8).abs()); // Brackets important
  28.     // println!("{}", 64f32.sqrt());
  29.     // println!("{}", f32::sqrt(3.32));
  30. }
  31.  
  32. fn merge_sort(vec: &Vec<i16>, output: &mut Vec<i16>) {
  33.     // println!("Input = {:?}", vec);
  34.    
  35.     if(vec.len() <2 ){
  36.         return;
  37.     }
  38.     let mid_size = vec.len()/2;
  39.    
  40.     // println!("{}", mid_size);
  41.    
  42.     let mut left_vec = Vec::new();
  43.     let mut right_vec = Vec::new();
  44.    
  45.     for i in 0..mid_size {
  46.         left_vec.push(vec[i]);
  47.     }
  48.    
  49.     for i in (mid_size)..vec.len(){
  50.         right_vec.push(vec[i]);
  51.     }
  52.    
  53.     // println!("Left vector = {:?}", left_vec);
  54.     // println!("Rigth vector = {:?}", right_vec);
  55.    
  56.     merge_sort(&left_vec, output);
  57.     merge_sort(&right_vec, output);
  58.    
  59.     merge(&left_vec, &right_vec, output);
  60.     println!("Output : {:?}", output);
  61. }
  62.  
  63. fn merge(left: &Vec<i16>, right: &Vec<i16>, output: &mut Vec<i16>) {
  64.         println!("Left = {:?}, Right = {:?}", left, right);
  65.         let mut k = 0;
  66.         let n_left = left.len();  
  67.         let n_right = right.len();
  68.         let (mut i, mut j) = (0, 0);
  69.         while(i< n_left && j< n_right){
  70.             // println!("Left: {}, Right: {}", left[i], right[j]);
  71.             if(left[i] <= right[j]){
  72.                 output.push(left[i]);
  73.                 i += 1
  74.             } else {
  75.                 output.push(right[j]);
  76.                 j += 1;
  77.             }
  78.             k += 1;
  79.         }
  80.        
  81.         while(i < n_left){
  82.             output.push(left[i]);
  83.             i += 1;
  84.             k += 1;
  85.         }
  86.        
  87.         while(j < n_right){
  88.             output.push(right[j]);
  89.             j += 1;
  90.             k += 1;
  91.         }
  92.        
  93.        
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top