SHARE
TWEET

Untitled

a guest Aug 21st, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Author Bill Wood
  2. // in-place, order N
  3. fn main() {
  4.     let mut x = vec!(1,2,2,3,2,1,5,8,4,9,);
  5.     println!("{:?}", stalin_sort(&mut x));
  6.     let mut x = vec!('a','b','c','b','e','g','d','x');
  7.     println!("{:?}", stalin_sort(&mut x));
  8.     let mut ideologies = vec!(
  9.         "Fully Automated Luxury Space Communism",
  10.         "Socialism",
  11.         "Capitalism",
  12.         "Communism",
  13.     );
  14.     println!("{:?}", stalin_sort(&mut ideologies));
  15. }
  16.  
  17. fn stalin_sort<T: PartialOrd>(v: &mut [T]) -> &[T] {
  18.     let mut i = 1;
  19.     let mut len = v.len();
  20.     while i < len {
  21.         v.swap(i, i + v.len() - len);
  22.         if v[i] >= v[i - 1] {
  23.             i += 1;
  24.         } else {
  25.             len -= 1;
  26.         }
  27.     }
  28.     &v[..len]
  29. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top