Decoding permutation algorithm with tests