Basic algorithms in java