Various algorithms in Java