Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,35 @@
|
|||
fn cocktail_sort<T: PartialOrd>(a: &mut [T]) {
|
||||
let len = a.len();
|
||||
loop {
|
||||
let mut swapped = false;
|
||||
let mut i = 0;
|
||||
while i + 1 < len {
|
||||
if a[i] > a[i + 1] {
|
||||
a.swap(i, i + 1);
|
||||
swapped = true;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
if swapped {
|
||||
swapped = false;
|
||||
i = len - 1;
|
||||
while i > 0 {
|
||||
if a[i - 1] > a[i] {
|
||||
a.swap(i - 1, i);
|
||||
swapped = true;
|
||||
}
|
||||
i -= 1;
|
||||
}
|
||||
}
|
||||
if !swapped {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut v = vec![10, 8, 4, 3, 1, 9, 0, 2, 7, 5, 6];
|
||||
println!("before: {:?}", v);
|
||||
cocktail_sort(&mut v);
|
||||
println!("after: {:?}", v);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue