Basic algorithms in Java