Algs4-1.1.36 out-of-order check

Source: Internet
Author: User
Tags shuffle

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.