Topology { static int parity(int[] vs, int[] rank) { int count = 0; for (int i = 0; i < vs.length; i++) { for (int j = i + 1; j < vs.length; j++) { if (rank[vs[i]] > rank[vs[j]]) count++; } } return (count & 0x1) == 1 ? -1 : 1; }    }
@Test public void permutationParity() { assertThat(Topology.parity(new int[]{0, 1, 2, 3}, new int[]{0, 1, 2, 3}), is(1)); assertThat(Topology.parity(new int[]{0, 1, 2, 3}, new int[]{0, 1, 3, 2}), is(-1)); assertThat(Topology.parity(new int[]{0, 1, 2, 3}, new int[]{1, 0, 3, 2}), is(1)); assertThat(Topology.parity(new int[]{0, 1, 2, 3}, new int[]{2, 0, 3, 1}), is(-1)); }