Algorithms implementation in Java