Java Array Permute permutation(final int start, final int end)

Here you can find the source of permutation(final int start, final int end)

Description

Creates a permutation of all integers in the interval [start, end]

License

Open Source License

Parameter

Parameter Description
start The lowest number which will be included in the permutation
end The highest number which will be included in the permutation

Return

an array of length end - start + 1, or an empty array if start > end.

Declaration

public final static int[] permutation(final int start, final int end) 

Method Source Code

//package com.java2s;
/*/*  w w  w . jav a 2 s . c o  m*/
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 
 * Copyright 2011-2014 Peter G?ttinger
 * 
 */

import java.util.Random;

public class Main {
    private final static Random random = new Random();

    /**
     * Creates a permutation of all integers in the interval [start, end]
     * 
     * @param start The lowest number which will be included in the permutation
     * @param end The highest number which will be included in the permutation
     * @return an array of length end - start + 1, or an empty array if start > end.
     */
    public final static int[] permutation(final int start, final int end) {
        if (start > end)
            return new int[0];
        final int length = end - start + 1;
        final int[] r = new int[length];
        for (int i = 0; i < length; i++)
            r[i] = start + i;
        for (int i = length - 1; i > 0; i--) {
            final int j = random.nextInt(i + 1);
            final int b = r[i];
            r[i] = r[j];
            r[j] = b;
        }
        return r;
    }

    /**
     * Creates a permutation of all bytes in the interval [start, end]
     * 
     * @param start The lowest number which will be included in the permutation
     * @param end The highest number which will be included in the permutation
     * @return an array of length end - start + 1, or an empty array if start > end.
     */
    public final static byte[] permutation(final byte start, final byte end) {
        if (start > end)
            return new byte[0];
        final int length = end - start + 1;
        final byte[] r = new byte[length];
        for (byte i = 0; i < length; i++)
            r[i] = (byte) (start + i);
        for (int i = length - 1; i > 0; i--) {
            final int j = random.nextInt(i + 1);
            final byte b = r[i];
            r[i] = r[j];
            r[j] = b;
        }
        return r;
    }

    /**
     * Shorthand for <code>{@link permutation permutation}(0, length - 1)</code>
     */
    public final static int[] permutation(final int length) {
        return permutation(0, length - 1);
    }
}

Related

  1. permutation(int n)
  2. permute(int k, Object[] os)
  3. permute(int[] elements, int i, int j)
  4. permute(int[] in, int[] idx)