Java Array Sort sortWithInds(int[] x, int[] idx)

Here you can find the source of sortWithInds(int[] x, int[] idx)

Description

sort With Inds

License

Apache License

Declaration

public static final int[] sortWithInds(int[] x, int[] idx) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Arrays;

public class Main {
    public static final int[] sortWithInds(int[] x, int[] idx) {
        int[] out = x.clone();
        Arrays.sort(out);/*w  ww .j a  v  a2  s. c o  m*/
        //populate the index array
        for (int i = 0; i < out.length; i++)
            idx[i] = indexOf(out[i], x);

        return out;
    }

    public static final float[] sortWithInds(float[] x, int[] idx) {
        float[] out = x.clone();
        Arrays.sort(out);
        //populate the index array
        for (int i = 0; i < out.length; i++)
            idx[i] = indexOf(out[i], x);

        return out;
    }

    public static final double[] sortWithInds(double[] x, int[] idx, int[] rev) {
        double[] out = x.clone();
        Arrays.sort(out);
        //populate the index array
        for (int i = 0; i < out.length; i++)
            idx[i] = indexOf(out[i], x);

        return out;
    }

    public static final int[] sortWithInds(int[] x, int[] idx, int[] rev) {
        int[] out = x.clone();
        Arrays.sort(out);
        //populate the index array
        for (int i = 0; i < out.length; i++)
            idx[i] = indexOf(out[i], x);

        return out;
    }

    public static final float[] sortWithInds(float[] x, int[] idx, int[] rev) {
        float[] out = x.clone();
        Arrays.sort(out);
        //populate the index array
        for (int i = 0; i < out.length; i++)
            idx[i] = indexOf(out[i], x);

        return out;
    }

    public static final double[] sortWithInds(double[] x, int[] idx) {
        double[] out = x.clone();
        Arrays.sort(out);
        //populate the index array
        for (int i = 0; i < out.length; i++)
            idx[i] = indexOf(out[i], x);

        return out;
    }

    public static final int[] clone(int[] in) {
        int nx = in.length;
        int[] out = new int[nx];
        for (int i = 0; i < nx; i++) {
            out[i] = in[i];
        }
        return out;
    }

    public static final float[] clone(float[] in) {
        int nx = in.length;
        float[] out = new float[nx];
        for (int i = 0; i < nx; i++) {
            out[i] = in[i];
        }
        return out;
    }

    public static final byte[] clone(byte[] in) {
        int nx = in.length;
        byte[] out = new byte[nx];
        for (int i = 0; i < nx; i++) {
            out[i] = in[i];
        }
        return out;
    }

    public static final double[] clone(double[] in) {
        int nx = in.length;
        double[] out = new double[nx];
        for (int i = 0; i < nx; i++) {
            out[i] = in[i];
        }
        return out;
    }

    public static final int[][] clone(int[][] in) {
        int nx = in.length;
        int ny = in[0].length;
        int[][] out = new int[nx][ny];
        for (int i = 0; i < nx; i++)
            for (int j = 0; j < ny; j++) {
                out[i][j] = in[i][j];
            }
        return out;
    }

    public static final float[][] clone(float[][] in) {
        int nx = in.length;
        int ny = in[0].length;
        float[][] out = new float[nx][ny];
        for (int i = 0; i < nx; i++)
            for (int j = 0; j < ny; j++) {
                out[i][j] = in[i][j];
            }
        return out;
    }

    public static final double[][] clone(double[][] in) {
        int nx = in.length;
        int ny = in[0].length;
        double[][] out = new double[nx][ny];
        for (int i = 0; i < nx; i++)
            for (int j = 0; j < ny; j++) {
                out[i][j] = in[i][j];
            }
        return out;
    }

    public static final int[][][] clone(int[][][] in) {
        int nx = in.length;
        int ny = in[0].length;
        int nz = in[0][0].length;
        int[][][] out = new int[nx][ny][nz];
        for (int i = 0; i < nx; i++)
            for (int j = 0; j < ny; j++)
                for (int k = 0; k < nz; k++) {
                    out[i][j][k] = in[i][j][k];
                }
        return out;
    }

    public static final double[][][] clone(double[][][] in) {
        int nx = in.length;
        int ny = in[0].length;
        int nz = in[0][0].length;
        double[][][] out = new double[nx][ny][nz];
        for (int i = 0; i < nx; i++)
            for (int j = 0; j < ny; j++)
                for (int k = 0; k < nz; k++) {
                    out[i][j][k] = in[i][j][k];
                }
        return out;
    }

    public static final float[][][] clone(float[][][] in) {
        int nx = in.length;
        int ny = in[0].length;
        int nz = in[0][0].length;
        float[][][] out = new float[nx][ny][nz];
        for (int i = 0; i < nx; i++)
            for (int j = 0; j < ny; j++)
                for (int k = 0; k < nz; k++) {
                    out[i][j][k] = in[i][j][k];
                }
        return out;
    }

    public static final double[][][][] clone(double[][][][] in) {
        int nx = in.length;
        int ny = in[0].length;
        int nz = in[0][0].length;
        int nc = in[0][0][0].length;
        double[][][][] out = new double[nx][ny][nz][nc];
        for (int i = 0; i < nx; i++)
            for (int j = 0; j < ny; j++)
                for (int k = 0; k < nz; k++)
                    for (int l = 0; l < nc; l++) {
                        out[i][j][k][l] = in[i][j][k][l];
                    }
        return out;
    }

    public static final float[][][][] clone(float[][][][] in) {
        int nx = in.length;
        int ny = in[0].length;
        int nz = in[0][0].length;
        int nc = in[0][0][0].length;
        float[][][][] out = new float[nx][ny][nz][nc];
        for (int i = 0; i < nx; i++)
            for (int j = 0; j < ny; j++)
                for (int k = 0; k < nz; k++)
                    for (int l = 0; l < nc; l++) {
                        out[i][j][k][l] = in[i][j][k][l];
                    }
        return out;
    }

    public static final int indexOf(int x, int[] list) {
        for (int i = 0; i < list.length; i++) {
            if (x == list[i])
                return i;
        }
        return -1;
    }

    public static final int indexOf(float x, float[] list) {
        for (int i = 0; i < list.length; i++) {
            if (x == list[i])
                return i;
        }
        return -1;
    }

    public static final int indexOf(double x, double[] list) {
        for (int i = 0; i < list.length; i++) {
            if (x == list[i])
                return i;
        }
        return -1;
    }

    public static final int indexOf(String x, String[] list) {
        for (int i = 0; i < list.length; i++) {
            if (x.equals(list[i]))
                return i;
        }
        return -1;
    }
}

Related

  1. sortTable(String[][] data, int index)
  2. sortToFXYSumOrder(final double[] coeffs)
  3. sortTwoArrays(A[] firstArray, B[] secondArray)
  4. sortWith(final int[] ary, int[] ary2)
  5. SortWithIndex(double[] arr, Integer[] i)
  6. sortWithNoMissingValues( double[] array)
  7. toSortedArray(Collection collection)
  8. toSortedArray(Set groups)
  9. toSortedIntArray(Collection ints)