Java List Reverse reversePermutation(final List permutation)

Here you can find the source of reversePermutation(final List permutation)

Description

Return the reverse permutation b of a permutation a of the numbers [0..n-1].

License

Apache License

Parameter

Parameter Description
permutation permutation of the numbers <code>[0..n-1]</code>, as list

Return

reverse permutation

Declaration

public static int[] reversePermutation(final List<Integer> permutation) 

Method Source Code

//package com.java2s;
/**// ww w.  j a v a 2 s .  com
 * Copyright (C) [2013] [The FURTHeR Project]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.List;

public class Main {
    /**
     * Return the reverse permutation <code>b</code> of a permutation <code>a</code> of
     * the numbers <code>[0..n-1]</code>. This means that <code>a o b = b o a = </code>
     * the identity permutation.
     * <p>
     * For instance, if <code>n = 5, a = {4,0,3,1,2} then
     * <code>b = {1,3,4,2,0}.
     *
     * @param permutation
     *            permutation of the numbers <code>[0..n-1]</code>
     * @return reverse permutation
     */
    public static int[] reversePermutation(final int[] permutation) {
        final int n = permutation.length;
        final int[] reverse = new int[n];
        for (int i = 0; i < n; i++) {
            reverse[permutation[i]] = i;
        }
        return reverse;
    }

    /**
     * Return the reverse permutation <code>b</code> of a permutation <code>a</code> of
     * the numbers <code>[0..n-1]</code>. This means that <code>a o b = b o a = </code>
     * the identity permutation.
     * <p>
     * For instance, if <code>n = 5, a = {4,0,3,1,2} then
     * <code>b = {1,3,4,2,0}.
     *
     * @param permutation
     *            permutation of the numbers <code>[0..n-1]</code>, as list
     * @return reverse permutation
     */
    public static int[] reversePermutation(final List<Integer> permutation) {
        final int n = permutation.size();
        final int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = permutation.get(i);
        }

        return reversePermutation(array);
    }
}

Related

  1. reverseList(List list, boolean shallowCopy)
  2. reverseList2(List list)
  3. reverseListShallowCopy(List list)
  4. reverseOrder(List list)
  5. reversePath(List pathParts)
  6. reversePermutation(T[] arr, List p)
  7. reverseSort(final List lines)
  8. reverseUseList(int[] values)
  9. reverseVector(List vector)