SHARE
TWEET

Untitled

a guest Aug 20th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Author Bill Wood
  2. // in-place order N^2
  3. fn main() {
  4.     let mut x = vec!(1,2,2,3,2,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.     assert_eq!(vec!(1), stalin_sort(&mut vec!(1)));
  16.     assert_eq!(vec!(1,1), stalin_sort(&mut vec!(1,1)));
  17.     let mut x: Vec<i32> = vec!();
  18.     assert_eq!(x.clone(), stalin_sort(&mut x));
  19. }
  20.  
  21. fn remove<T: PartialOrd + Default>(v: &mut [T], mut index: usize) -> &mut [T] {
  22.     while index + 1 < v.len() {
  23.         v[index] = std::mem::replace(&mut v[index + 1], Default::default());
  24.         index += 1;
  25.     }
  26.     &mut v[..index]
  27. }
  28.  
  29. fn stalin_sort<T: PartialOrd + Default>(mut v: &mut [T]) -> &[T] {
  30.     let mut i = 0;
  31.     while i + 1 < v.len() {
  32.         if v[i] > v[i + 1] {
  33.             v = remove(v, i + 1);
  34.         } else {
  35.             i += 1;
  36.         }
  37.     }
  38.     v
  39. }
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