R, 38 bytes
Try it online!
Adaptation of ovs's answer. Test harness taken from Dominic van Essen's answer.
combn(x,m) in R generates all size
m combinations of the elements of
x1, and has an optional
FUN argument which it applies to each of those combinations. As luck would have it, it's implemented in such a way as to maintain the order of elements in
x in each of those combinations; for
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 3 3 3 1 1 4
[2,] 1 4 2 4 2 2
FUN=diff results in a vector which contains the upper-triangular part of the matrix of comparisons:
 -2 1 -1 3 1 -2
These are negative precisely where there is a swapped pair, so count the negatives.
Unfortunately, R will throw an error for
combn(1,2) since it can't get a combination of 2 out of a single value, so
0 is prepended. This has no impact (since the
diff will always be positive), and neatly fixes that edge case.
x is a single numeric, it instead uses
seq_len(n) which will be
n>=1 and an empty vector otherwise. R functions sometimes have weird special cases for length-one inputs.