| Mergesort and binary search have a contract which defines: - Which inputs are valid. - For a valid input, what the constraints on the return value are. You'd have a point if the implementations had as an input constraint: "array must be less than 2^30". But they didn't. Otherwise, nothing is broken unless it never returns the right answer. Take: fn add(x: u32, y:u32) -> u32 {
return 1;
} This implementation works perfectly for numbers that add to 1. It just loses generality outside of that. fn add(x: u32, y: u32) -> u32 {
return (x + y) - (x >> 10) - (y >> 10);
} This implementation works for x < 2^10 and y < 2^10. Arguably this implementation is much worse than the previous one because it fails unexpectedly. At least the previous implementation would be much more obviously broken. But these are both broken because they don't fulfill the (implicit) contract for add. You can't just say "well, it's implied that my add function only takes inputs that add to 1" unless you actually write that somewhere and make it clear. |