How does one compute the sign of a permutation? The sign of a permutation $\sigma\in \mathfrak{S}_n$, written ${\rm sgn}(\sigma)$, is defined to be +1 if the permutation is even and -1 if it is odd, and is given by the formula
$${\rm sgn}(\sigma) = (-1)^m$$
where $m$ is the number of transpositions in the permutation when written as a product of transpositions.
Alternatively the sign is -1 if, when we express $\sigma$ as a product of disjoint cycles, the result contains an odd number of even-length cycles. Otherwise the sign is +1.
My permutations are expressed as tuples $(\sigma_1,\dots,\sigma_n)$, so neither the expression of the tuple as a product of disjoint cycles nor as a product of transpositions are immediately available to me.
This suggests two high-algorithms to compute the sign of a permutation:


*

*Express the permutation as a product of transpositions and count the number of transpositions.

*Express the permutation as a product of disjoint cycles and count the number of even-length cycles.
What are typical algorithms for accomplishing these tasks, and how do they vary in running type depending on $n$? Are there more efficient algorithms for calculating the sign of a permutation? 

Additional info
The motivation is to quickly decide whether instances of the fifteen puzzle are solvable. I want to generate a large number of solvable instances of the fifteen puzzle for testing some search algorithms. At the moment I generate a random instance, and test whether it is solvable by trying to solve it with depth-first search, which is fairly slow going, and won't generalize well to larger puzzles (24 puzzle, 35 puzzle...) due to time and memory limitations. Since solvable instances of the fifteen puzzle are in 1-1 correspondence with even elements of $\mathfrak{S}_{16}$, I figure that there must be a faster way of generating solvable instances.
It has just occurred to me that a better way of generating solvable instances of the puzzle might be to generate an even number of transpositions and multiply them together to generate an even permutation. I'd prefer an algorithm that was guaranteed to return an even distribution over $\mathfrak{S}_n$ though, and in fact I'm now sufficiently interested in the answer to this question in its own right. 
 A: If $c_e(n)$ is the number of even-length cycles in a permutation $p$ of length $n$, then one of the formulas for the sign of a permutation $p$ is $\text{sgn}(p) = (-1)^{c_e(n)}$.
Here is an $O(n)$ Matlab function that computes the sign of a permutation vector $p(1:n)$ by traversing each cycle of $p$ and (implicitly) counting the number of even-length cycles. The number of cycles in a random permutation of length $n$ is $O(H_n)$, where $H_n$ is the $n$-th Harmonic Number. 
function sgn = SignPerm(p);
% ----------------------------------------------------------
% Calculates the sign of a permutation p.
% p is a row vector p(1,n), which represents the permutation.
% sgn(p) = (-1)^(No. of even-length cycles)
% Complexity : O(n + ncyc) ~ O(n + Hn) ~~ O(n+log(n)) steps.
%
% Derek O'Connor 20 March 2011.
% ----------------------------------------------------------
n   = length(p);
visited(1:n) = false;                  % Logical vector which marks all p(k)
                                       % not visited.
sgn = 1;
for k = 1:n
    if ~visited(k)                     % k not visited, start of new cycle
        next = k;
        L = 0;
        while ~visited(next)           % Traverse the current cycle k
            L = L+1;                   % and find its length L
            visited(next) =  true;
            next    = p(next);
        end
        if rem(L,2) == 0               % If L is even, change sign.
            sgn = -sgn;
        end
    end % if ~visited(k)
end % for k 

A: The inverse permutation can be constructed as a sequence of $n-1$ transpositions via Gaussian Elimination with partial pivoting, $P A = L U$, where $A$ is the original permutation matrix, $P=A^{-1}$, and $L=U=I$. Since the signature of the inverse permutation is the same as that of the original permutation, this procedure yields the sign of the permutation.
Thankfully, this algorithm can be run in linear time by maintaining the permutation and its inverse throughout a short-circuited Gaussian Elimination procedure since it can easily be seen that the Schur complement updates will always be zero.
A: $sgn(\sigma)=\prod_{i<j}\frac{\sigma(i)-\sigma(j)}{i-j}$ is quadratic, but straightforward to code.
A: So you have a permutation $f: X \to X$ for which you can efficiently compute $f(x)$ from $x$.
I think a good way to do any of the things you mentioned is to make a checklist for the elements of $X$.
Then you can start with the first unchecked element and follow the chain $x, f(x), f(f(x))$, etc. checking off each element as you find it until you reach
an element that is already checked. You have now traversed one cycle.
Then you can pick the next unchecked element and traverse that cycle, and so
on until all elements are checked.
While you traverse a cycle you can easily 


*

*Count the cycle length

*Record the cycle, or

*Record transpositions 


All this works in roughly linear time. Obviously just counting the cycle lengths
is going to be the fastest.
A: Use the Fisher-Yates-Shuffle in its inside-out version to generate a permutation of the numbers $1\dots16$. You can find the parity of this permutation by counting the number of transpositions you performed. This is easy: Every time $i\neq j$ you perform a transposition. If the puzzle invariant is violated now, simply exchange two arbitrary tiles (not the empty spot) to make the puzzle solvable.
Here is the inside-out Fisher-Yates shuffle in a version that generates a random permutation in an array $a[1\dots n]$:


*

*for $i$ from $1$ to $n$ do:

*$j\leftarrow\mathop{\rm rnd}\{1\ldots i\}$

*if $i\neq j$ then $a[i]\leftarrow a[j]$

*$a[j]\leftarrow i$


Every time the branch in step 3 is taken, you perform a transposition and need to toggle your parity.
A: It's worth mentioning the quadratic time algorithm, since it can be faster for small permutations:
$$\textrm{sgn}(\sigma) = (-1)^{\sum_{0 \le i<j<n} (\sigma_i>\sigma_j)}$$
I.e., the sign is negative iff there are an odd number of misordered pairs of indices.  This algorithm also works if we're interested in the permutation defined by an unsorted list of integers where the cycle structure can't be determined in linear time.
A: Welcome to $O(n)$'s world. Python code
def decomposition(n, sigma):
   lista = list()
   for k in range (1, n + 1):
      boo = False
      for i in range (0, n):
         if sigma[i] == k:
            boo = True
            break
      if not boo:
         print("Error")
         return
   
   contador = 0
   for i in range (0, n - 1):
      if sigma[i] != i + 1:
         k = sigma[i] - 1
         sigma[i], sigma[k] = i + 1, k + 1
         s = "(" + str(i + 1) + ", " + str(k + 1) + ")"
         lista.insert(0, s)
         contador = contador + 1
         
   s = ""
   for cadaUm in lista:
     s = s + cadaUm
   print("sigma =", s)
   print("size =", contador)
   return

