MDGSF Software Engineer

[Rust] bubble sort

2019-07-11

fn main() {
  let mut v = vec![3, 2, 5, 1, 4];
  println!("before sort, v = {:?}", v);

  bubble_sort(&mut v);
  println!("after sort, v = {:?}", v);
}

fn bubble_sort<T>(v: &mut [T])
where
  T: PartialOrd + Copy,
{
  for i in 1..v.len() {
    for j in 0..(v.len() - i) {
      if v[j] > v[j + 1] {
        v.swap(j, j + 1);
      }
    }
  }
}
fn main() {
  let mut v = vec![3, 2, 5, 1, 4];
  println!("before sort, v = {:?}", v);

  bubble_sort(&mut v);
  println!("after sort, v = {:?}", v);
}

fn bubble_sort<T>(v: &mut [T])
where
  T: PartialOrd + Copy,
{
  let mut i = 0;
  while i < v.len() {
    let mut j = v.len() - 1;
    while j > i {
      if v[j - 1] > v[j] {
        v.swap(j - 1, j);
      }
      j -= 1;
    }
    i += 1;
  }
}

weixingongzhonghao

Similar Posts

Comments