Is there general method to calculate the times of swapping adjacent elements required to arrange a set of numbers into another order?

34 Views Asked by At

Assume I want to change [1,2,...,n] to anthor order, say [n,n-1,...,2,1].

In the exmaple, I know I can do like this

[1,2,...,n] $\rightarrow$ [2,1...,n]$\rightarrow$ [2,3,1,...,n]$\rightarrow \cdots \rightarrow$ [2,...,n,1]

$\rightarrow$ [3,...,n,2,1]$\rightarrow \cdots$ $\rightarrow$ [n,n-1,...,2,1],

so I need to swap adjacent elements $\frac{(0+n-1)n}{2}$ times.

The above process depends on my observation, but if the aim order of the numbers is more complicated, it won't be this easy to know by observation.

I wonder if there is any genral method to know that if the permutation is even or odd?