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 ;
}