Knuth.
java
[Link]
Below is the syntax highlighted version of [Link]
from §1.1 Programming Model.
/******************************************************************************
* Compilation: javac [Link]
* Execution: java Knuth < [Link]
* Dependencies: [Link] [Link]
* Data files: [Link]
* [Link]
*
* Reads in a list of strings and prints them in random order.
* The Knuth (or Fisher-Yates) shuffling algorithm guarantees
* to rearrange the elements in uniformly random order, under
* the assumption that [Link]() generates independent and
* uniformly distributed numbers between 0 and 1.
*
* % more [Link]
* 2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
* 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
* 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
* 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS
*
* % java Knuth < [Link]
* 6H
* 9C
* 8H
* 7C
* JS
* ...
* KH
*
* % more [Link]
* 2♣ 3♣ 4♣ 5♣ 6♣ 7♣ 8♣ 9♣ 10♣ J♣ Q♣ K♣ A♣
* 2♦ 3♦ 4♦ 5♦ 6♦ 7♦ 8♦ 9♦ 10♦ J♦ Q♦ K♦ A♦
* 2♥ 3♥ 4♥ 5♥ 6♥ 7♥ 8♥ 9♥ 10♥ J♥ Q♥ K♥ A♥
* 2♠ 3♠ 4♠ 5♠ 6♠ 7♠ 8♠ 9♠ 10♠ J♠ Q♠ K♠ A♠
*
* % java Knuth < [Link]
* 2♠
* K♥
* 6♥
* 5♣
* J♣
* ...
* A♦
*
******************************************************************************/
/**
* The {@code Knuth} class provides a client for reading in a
* sequence of strings and <em>shuffling</em> them using the Knuth (or Fisher-Yates)
* shuffling algorithm. This algorithm guarantees to rearrange the
* elements in uniformly random order, under
* the assumption that [Link]() generates independent and
[Link] 1:52:38 PM]
[Link]
* uniformly distributed numbers between 0 and 1.
* <p>
* For additional documentation,
* see <a href="[Link] 1.1</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
* See {@link StdRandom} for versions that shuffle arrays and
* subarrays of objects, doubles, and ints.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class Knuth {
// this class should not be instantiated
private Knuth() { }
/**
* Rearranges an array of objects in uniformly random order
* (under the assumption that {@code [Link]()} generates independent
* and uniformly distributed numbers between 0 and 1).
* @param a the array to be shuffled
*/
public static void shuffle(Object[] a) {
int n = [Link];
for (int i = 0; i < n; i++) {
// choose index uniformly in [0, i]
int r = (int) ([Link]() * (i + 1));
Object swap = a[r];
a[r] = a[i];
a[i] = swap;
}
}
/**
* Rearranges an array of objects in uniformly random order
* (under the assumption that {@code [Link]()} generates independent
* and uniformly distributed numbers between 0 and 1).
* @param a the array to be shuffled
*/
public static void shuffleAlternate(Object[] a) {
int n = [Link];
for (int i = 0; i < n; i++) {
// choose index uniformly in [i, n-1]
int r = i + (int) ([Link]() * (n - i));
Object swap = a[r];
a[r] = a[i];
a[i] = swap;
}
}
/**
* Reads in a sequence of strings from standard input, shuffles
* them, and prints out the results.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
// read in the data
String[] a = [Link]();
// shuffle the array
[Link](a);
// print results.
for (int i = 0; i < [Link]; i++)
[Link](a[i]);
}
[Link] 1:52:38 PM]
[Link]
Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 12:50:46 EDT 2017.
[Link] 1:52:38 PM]