# permutation problem, need help!!!

• 10-01-2001
Eric
permutation problem, need help!!!
Hi, all, I am trying to display a permutation, but I found the result is not correct, and I know where the problem was generated, but I don't know how to fix it, so I put it here, hope somebody will help me.

thanks

void swap(int * A, int i, int j)
{
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}

//function used to exchange an element with the rest elements in the array in one loop
void permute(int *array, int n)
{
for (int i=n; i>0; i--)
swap(array, i-1, Random(i)); //problems occurs here.
}

// the following loop to display the n! times of the array(Fact=n!)
for (int k=Fact;k>0;k--)
{
permute(A, n);

//to display the new array

for (i=0; i<n; i++)
{ cout << A[i];
if (!((i+1)%10))
cout << "\n";
else
cout << " ";
}
here are how the results looks like at different excutives times
when n=3, in the 6 time permutation, some of the permutations are reapted. I know the problem is due to the Random(), it can guarrantees no repates in one loop in the Permut(), but it can't check if the array has the same value in corresponding positions at different Fact values.
---------------
1 0 2
0 2 1
1 2 0
1 0 2
1 0 2
2 1 0
-------------------
1 0 2
2 1 0
0 1 2
2 0 1
2 1 0
2 0 1