Java Array Sort sortPerm(final int[] w)

Here you can find the source of sortPerm(final int[] w)

Description

sort Perm

License

Open Source License

Parameter

Parameter Description
w a parameter

Return

the sorting permutation perm of w, i.e. w[perm[0]],w[perm[1]],...,w[perm[w.length-1]] is sorted increasingly

Declaration

public static int[] sortPerm(final int[] w) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * OscaR is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version./*  w w  w .  ja  v  a2s  . c  o m*/
 *   
 * OscaR is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License  for more details.
 *   
 * You should have received a copy of the GNU Lesser General Public License along with OscaR.
 * If not, see http://www.gnu.org/licenses/lgpl-3.0.en.html
 ******************************************************************************/

import java.util.Arrays;
import java.util.Comparator;

public class Main {
    /**
     *
     * @param w
     * @return the sorting permutation perm of w, i.e. w[perm[0]],w[perm[1]],...,w[perm[w.length-1]] is sorted increasingly
     */
    public static int[] sortPerm(final int[] w) {
        Integer[] perm = new Integer[w.length];
        for (int i = 0; i < perm.length; i++) {
            perm[i] = i;
        }
        Arrays.sort(perm, new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return w[o1] - w[o2];
            }
        });
        int[] res = new int[w.length];
        for (int i = 0; i < perm.length; i++) {
            res[i] = perm[i];
        }
        return res;
    }

    /**
     * sorts x increasingly according to the weights w
     * @param x
     * @param w
     */
    public static <E> void sort(E[] x, int[] w) {
        assert (x.length == w.length);
        applyPermutation(x, sortPerm(w));
    }

    /**
     *
     * @param x
     * @param permutation a valid permutation of x (all numbers from 0 to x.length-1 present), <br>
     *        permutation[i] represents the index of the entry of x that must come in position i in the permuted array
     * @param <E>
     */
    public static <E> void applyPermutation(E[] x, int[] permutation) {
        assert (x.length == permutation.length);
        Object[] objs = new Object[x.length];
        int sum = 0;
        for (int i = 0; i < permutation.length; i++) {
            sum += permutation[i];
            objs[i] = x[i];
        }
        assert (sum == (x.length - 1) * (x.length - 2) / 2); // check the permutation is valid
        for (int i = 0; i < permutation.length; i++) {
            x[i] = (E) objs[permutation[i]];
        }
        objs = null;
    }

    /**
     *
     * @param x
     * @param permutation
     * @see ArrayUtils.applyPermutation
     */
    public static void applyPermutation(int[] x, int[] permutation) {
        int[] xcopy = Arrays.copyOf(x, x.length);
        for (int i = 0; i < permutation.length; i++) {
            x[i] = xcopy[permutation[i]];
        }
    }
}

Related

  1. sortNames(String[] names)
  2. sortNames(String[] names)
  3. sortNames(String[] names)
  4. sortNames(String[] names)
  5. sortNullElements(Object[] array)
  6. sortPermutation(final int[] A)
  7. sortPixels(double[] data, int numPixel)
  8. sortPopup(Integer[] integers)
  9. sortRetain(long[] a)