1.1.36 check in disorder. Check whether the out-of-order code in 1.1.10 can produce the expected results. Write a program shuffletest, accept the command line parameters m and n, disrupt the array with the size of M n times and re-initialize the array to a [I] = I before each disruption. print an mxm table. For all column J, row I indicates that I falls into J after being disrupted.
The number of times the location is located. The values of all elements in the array should be close to N/m.
Public class shuffletest
{
Public static void main (string [] ARGs)
{
Int M = integer. parseint (ARGs [0]);
Int n = integer. parseint (ARGs [1]);
Int [] A = new int [m];
Int [] [] info = new int [m] [m];
// Disrupt n times
For (int K = 0; k <n; k ++)
{
// Re-initialize the array to a [I] = I before each disruption
For (INT I = 0; I <m; I ++)
A [I] = I;
// Disrupt
Shuffle ();
// The number of times that the value of row I falls into column J increases by 1
For (INT I = 0; I <A. length; I ++)
Info [A [I] [I] ++;
}
// Print the M * m Array
Printarray (Info );
}
// Disrupt the Array
Public static void shuffle (INT [])
{
Int n = A. length;
For (INT I = 0; I <n; I ++)
{
Int r = I + stdrandom. Uniform (n-I );
Int temp = A [I];
A [I] = A [R];
A [R] = temp;
}
} // End the disruption
// Print the Array
Private Static void printarray (INT [] [] array)
{
Int rowlen = array. length;
Int Collen = array [0]. length;
Stdout. printf ("");
For (INT Col = 0; Col <Collen; Col ++)
Stdout. printf ("% 5d", col );
Stdout. printf ("\ n ");
//
For (int row = 0; row <rowlen; row ++)
{
Stdout. printf ("% d", row );
For (INT Col = 0; Col <Collen; Col ++)
Stdout. printf ("% 5d", array [row] [col]);
Stdout. printf ("\ n ");
}
}
}
Algs4-1.1.36 out-of-order check