Algorithms in Java my codes