Java code for various algorithms