Skip to content

Instantly share code, notes, and snippets.

@aita
Created January 2, 2020 09:39
Show Gist options
  • Select an option

  • Save aita/79682019eeb74fb1f8c63900cd0b1762 to your computer and use it in GitHub Desktop.

Select an option

Save aita/79682019eeb74fb1f8c63900cd0b1762 to your computer and use it in GitHub Desktop.
fn gnome_sort<T: PartialOrd>(v: &mut [T]) {
let len = v.len();
let mut i = 1;
while i < len {
if v[i - 1] <= v[i] {
i += 1;
} else {
v.swap(i - 1, i);
i -= 1;
if i == 0 {
i += 1;
}
}
}
}
fn main() {
let mut v = [5, 1, 4, 2, 6, 3];
gnome_sort(&mut v);
println!("{:?}", v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment