java algorithm