Learn R Programming

comphy (version 1.0.5)

oddity: Parity of a permutation

Description

Given a permutation of the integers from 1 to \(n\), this function calculates its parity (+1 or -1), i.e. the number of swapping that take the permutation back to the natural ordering \(1,2,\dots,n\).

Usage

oddity(x)

Value

A real number equal to +1 or -1, indicating the parity of the given permutation.

Arguments

x

A vector containing a permutation of the first \(n\) integers, \(1,2,\dots,n\).

Examples

Run this code
# Identity permutation (10 elements)
x <- 1:10
print(oddity(x))

# One swap
x[2] <- 5
x[5] <- 2
print(oddity(x))

Run the code above in your browser using DataLab