Java Array Shuffle shuffle(final Object[] values)

Here you can find the source of shuffle(final Object[] values)

Description

shuffle

License

Open Source License

Declaration

public static void shuffle(final Object[] values) 

Method Source Code

//package com.java2s;
/*//w  ww.j a va2s  .  com
 * Copyright 2015, Manuel Menezes de Sequeira.
 * 
 * Copyright 2015, Robert Sedgewick and Kevin Wayne.
 * 
 * This file is in part a derivative work of the code which accompanies the
 * textbook
 * 
 * Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne, Addison-Wesley
 * Professional, 2011, ISBN 0-321-57351-X. http://algs4.cs.princeton.edu
 * 
 * Part of the code derives also from:
 * http://www.geeksforgeeks.org/counting-inversions/
 * 
 * This code 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 code 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 code. If not, see http://www.gnu.org/licenses.
 * 
 * Any errors found in this code should be assumed to be the responsibility of
 * the author of the modifications to the original code (viz. Manuel Menezes de
 * Sequeira).
 */

import java.util.Random;

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

    public static void shuffle(final Object[] values) {
        for (int i = 0; i != values.length; i++) {
            final int r = generator.nextInt(i + 1);
            swap(values, i, r);
        }
    }

    private static void swap(final Object[] values,
            final int firstPosition, final int secondPosition) {
        final Object temporary = values[firstPosition];
        values[firstPosition] = values[secondPosition];
        values[secondPosition] = temporary;
    }
}

Related

  1. Shuffle(double[] array)
  2. shuffle(double[] array, Random r)
  3. shuffle(double[][] kernel, long seed)
  4. shuffle(final A source, final Random random)
  5. shuffle(final Object[] array)
  6. shuffle(final T[] arr)
  7. shuffle(final T[] array)
  8. shuffle(final T[] array)
  9. shuffle(int a[])