java main algorithms naive implementation