daily pastebin goal
1%
SHARE
TWEET

Untitled

a guest Mar 26th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pub fn merge_no_sentinel<T: Clone + Ord>(
  2.     a: &mut [T],
  3.     p: usize,
  4.     q: usize,
  5.     r: usize,
  6. ) -> Result<(), Error> {
  7.     if !(p <= q && q <= r && r < a.len()) {
  8.         return Err(PQROutOfBounds(p, q, r).into());
  9.     }
  10.  
  11.     let left = a[p..q].to_vec();
  12.     let right = a[q..=r].to_vec();
  13.  
  14.     let mut i = 0;
  15.     let mut j = 0;
  16.  
  17.     for mut k in p..=r {
  18.         if i >= left.len() {
  19.             for item in &right[j..] {
  20.                 a[k] = item.clone();
  21.                 k += 1;
  22.             }
  23.             break;
  24.         }
  25.  
  26.         if j >= right.len() {
  27.             for item in &left[i..] {
  28.                 a[k] = item.clone();
  29.                 k += 1;
  30.             }
  31.             break;
  32.         }
  33.  
  34.         if left[i] <= right[j] {
  35.             a[k] = left[i].clone();
  36.  
  37.             i += 1;
  38.         } else {
  39.             a[k] = right[j].clone();
  40.             j += 1;
  41.         }
  42.     }
  43.  
  44.     Ok(())
  45. }
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