Better permutation algorithm