Package edu.princeton.cs.algs4
Class Knuth
- Object
-
- edu.princeton.cs.algs4.Knuth
-
public class Knuth extends Object
TheKnuthclass provides a client for reading in a sequence of strings and shuffling them using the Knuth (or Fisher-Yates) shuffling algorithm. This algorithm guarantees to rearrange the elements in uniformly random order, under the assumption that Math.random() generates independent and uniformly distributed numbers between 0 and 1.For additional documentation, see Section 1.1 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne. See
StdRandomfor versions that shuffle arrays and subarrays of objects, doubles, and ints.- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidmain(String[] args)Reads in a sequence of strings from standard input, shuffles them, and prints out the results.static voidshuffle(Object[] a)Rearranges an array of objects in uniformly random order (under the assumption thatMath.random()generates independent and uniformly distributed numbers between 0 and 1).static voidshuffleAlternate(Object[] a)Rearranges an array of objects in uniformly random order (under the assumption thatMath.random()generates independent and uniformly distributed numbers between 0 and 1).
-
-
-
Method Detail
-
shuffle
public static void shuffle(Object[] a)
Rearranges an array of objects in uniformly random order (under the assumption thatMath.random()generates independent and uniformly distributed numbers between 0 and 1).- Parameters:
a- the array to be shuffled
-
shuffleAlternate
public static void shuffleAlternate(Object[] a)
Rearranges an array of objects in uniformly random order (under the assumption thatMath.random()generates independent and uniformly distributed numbers between 0 and 1).- Parameters:
a- the array to be shuffled
-
main
public static void main(String[] args)
Reads in a sequence of strings from standard input, shuffles them, and prints out the results.- Parameters:
args- the command-line arguments
-
-