added generic version for snail sort next permutation