Java Array Permute permute(T[] array)

Here you can find the source of permute(T[] array)

Description

Randomly permute the values in array .

License

Open Source License

Parameter

Parameter Description
array the array to permute

Declaration

public static <T> void permute(T[] array) 

Method Source Code

//package com.java2s;
/**/*from  ww  w  .j a  v a2  s.  co  m*/
 * Copyright (c) Zachary Kurmas 2007
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import java.util.Arrays;
import java.util.Collections;
import java.util.Random;

public class Main {
    private static Random r = null;

    /**
     * Randomly permute the values in {@code array}.
     *
     * @param array the array to permute
     * @param rnd the {@code java.util.Random} object used to determine where
     *              values are randomly moved.
     */
    public static <T> void permute(T[] array, java.util.Random rnd) {
        Collections.shuffle(Arrays.asList(array), r);
    }

    /**
     * Randomly permute the values in {@code array}.
     *
     * @param array the array to permute
     */
    public static <T> void permute(T[] array) {
        Collections.shuffle(Arrays.asList(array));
    }

    /**
     * Randomly permute the values in {@code array}.
     *
     * @param array the array to permute
     * @param rnd the {@code java.util.Random} object used to determine where
     *              values are randomly moved.
     */

    public static void permute(int[] array, java.util.Random rnd) {
        if (array.length <= 1) {
            return;
        }

        int to, x;

        /*
           * This is the fisher_yates shuffle as presented in the Perl Cookbook
           * p121
           */
        for (x = array.length - 1; x > 0; x--) {
            to = rnd.nextInt(x + 1);

            if (to == x)
                continue;

            int temp;
            temp = array[to];
            array[to] = array[x];
            array[x] = temp;
        } // end for
    }

    /**
     * Randomly permute the values in {@code array}.
     *
     * @param array the array to permute
     *
     */
    public static void permute(int[] array) {
        permute(array, getRandom());
    }

    static Random getRandom() {
        if (r == null) {
            r = new Random();
        }
        return r;
    }
}

Related

  1. permute(int k, Object[] os)
  2. permute(int[] elements, int i, int j)
  3. permute(int[] in, int[] idx)
  4. permute(int[] p, Object[] data, boolean clone)
  5. permute(String str, int startIndex, int endIndex)
  6. permute(T[] values)
  7. permuteArray(int[] array, Integer[] permutation)
  8. permuteCols(double[][] ary, int[] idx)
  9. permuteList(List inList)