3 ms·
It's unsafe code manipulating raw pointers, which is "intimidating and verbose" by design. It's also explicitly typing everything, which increases verbosity.
by masklinn 8y ago
It's unsafe code manipulating raw pointers, which is "intimidating and verbose" by design.
It's also explicitly typing everything, which increases verbosity.
The "clean" rust conversion would look something like this:
fn insertion_sort<T: Copy + Ord>(p: &mut [T]) {
for i in 1..p.len() {
let x = p[i];
let mut j = i;
while j > 0 && p[j-1] > x {
p[j] = p[j-1];
j -= 1;
}
p[j] = x;
}
}
Although unless it's really necessary I would expect the simpler version:
fn insertion_sort<T: Ord>(p: &mut [T]) {
for i in 0..p.len() {
for j in (0..i).rev() {
if p[j] < p[j + 1] {
break
}
p.swap(j, j + 1);
}
}
}