Generate random arrangement

Source: Internet
Author: User

Generate a random arrangement:

The permute () function is used to disrupt the order of arrays.

The function of the random () function is to generate a random number 0-> n-1.

The function of swap () is to exchange the values of elements I and J in the array.

 //  Function: generates random sorting.  
# Include < Stdio. h >
# Include < Stdlib. h >
// Declare a function
Void Permute ( Int Array [], Int N );
Int Random ( Int N );
Void Swap ( Int A [], Int I, Int J );

// Randomly Permute the N values of Array
Void Permute ( Int Array [], Int N)
{
For ( Int I = N; I > 0 ; I -- )
{
Swap (array, I - 1 , Random (I ));
}
}

// Return a random value in rang 0 to n-1
Int Random ( Int N)
{
Return Rand () % (N );
}

// Swap two elements in a Generic Array
Void Swap ( Int A [], Int I, Int J)
{
Int Temp = A [I];
A [I] = A [J];
A [J] = Temp;
}

Int Main ()
{
Int A [ 10 ];
Int I;
For (I = 0 ; I < 10 ; I ++ )
{
A [I] = Random (I + 9 );
}
Permute (, 10 );
For (I = 0 ; I < 10 ; I ++ )
{
Printf ( " % D \ t " , A [I]);
}
Return 1 ;
}

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.