SHARE
TWEET

Untitled

a guest May 22nd, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fn merge_sort<T: Ord + Copy>(data: &mut [T]) {
  2.     fn merge<T: Ord + Copy>(left: &mut [T], right: &mut [T], util: &mut [T]) {
  3.         let left = left.iter();
  4.         let right = right.iter();
  5.    
  6.         left.zip(right).fold(0usize, |index, (l, r)| {
  7.             if l < r {
  8.                 util[index] = *l;
  9.                 util[index + 1] = *r;
  10.             }
  11.             else {
  12.                 util[index] = *r;
  13.                 util[index + 1] = *l;
  14.             }
  15.        
  16.             index + 2
  17.         });
  18.     }
  19.    
  20.     fn copy<T: Ord + Copy>(data: &mut [T], util: &mut [T]) {
  21.        
  22.     }
  23.  
  24.     fn tick<T: Ord + Copy>(data: &mut [T], util: &mut [T]) {
  25.         if data.len() < 2 {
  26.             return
  27.         }
  28.        
  29.         let middle = data.len() / 2;
  30.    
  31.         let (data_left, data_right) = data.split_at_mut(middle);
  32.         let (util_left, util_right) = util.split_at_mut(middle);
  33.        
  34.         tick(data_left, util_left);
  35.         tick(data_right, util_right);
  36.        
  37.         merge(data_left, data_right, util);
  38.        
  39.         copy(data, util);
  40.     }
  41.    
  42.     let len = data.len();
  43.    
  44.     if len < 2 {
  45.         return
  46.     }
  47.    
  48.     let ref mut util: Vec<T> = Vec::with_capacity(len);
  49.    
  50.     tick(data, util.as_mut_slice());
  51. }
  52.  
  53. fn main() {
  54.     let ref mut data: [u32; 7] = [38, 27, 43, 3, 9, 82, 10];
  55.    
  56.     merge_sort(data);
  57.    
  58.     println!("{:?}", data);
  59. }
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