When not in use the system to provide built-in function to achieve full array in c language.
Such as:
Please input n=3
123
132
213
231
312
321
Please input n=4
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321
thanks.

VulpesPosted Nov 20, 2013, 5:22 AM
In the nextperm fiunction I have this line in the wrong place:
if (k == -1) return 0;
It should be like this:
int nextperm(int ia[], int n)
{
int i, j, k = -1, l;
for(i = 0; i < n - 1 ; i++)
{
if(ia[i] < ia[i + 1]) k = i;
}
if (k >= 0)
{
l = -1;
for(i = 0; i < n ; i++)
{
if(ia[k] < ia[i]) l = i;
}
swap(&ia[k], &ia[l]);
for(i = k + 1; i < n; i++)
{
j = n - i + k;
if (i >= j) break;
swap(&ia[i], &ia[j]);
}
}
if (k == -1) return 0; // move above the lines which print out the array
for(i = 0; i < n; i++) printf("%d", ia[i]);
printf("\n");
return 1;
}
Ken HPosted Dec 14, 2013, 3:40 AM
VulpesPosted Dec 3, 2013, 11:32 AM
For a description of how this algorithm works, check out this link:
www.cs.ucf.edu/~dmarino/ucf/cop3502/lec/Permutations.doc?
VulpesPosted Nov 24, 2013, 10:55 AM
j will then be equal to n - (k + 1) + k which simplifies to n - 1, which is the index of the last element of the array.
Consider next the case when i = k + 2.
j will then be equal to n - (k + 2) + k which simplfies to n - 2, which is the index of the second last element of the array.
So, hopefully, you can see now that the expression for j is giving us the indices we need to reverse the elements from k + 1 onwards.
However, your own expression which uses the auxiliary variable 'w' also works. So I'd go with that if you find it easier to understand.
Ken HPosted Nov 24, 2013, 2:07 AM
int w=1;
for(i = k + 1; i < n; i++)
{
// j = n - i + k;
j=n-w;
if (i >= j) break;
swap(&ia[i], &ia[j]);
w++;
}
Ken HPosted Nov 23, 2013, 7:35 PM
VulpesPosted Nov 23, 2013, 6:21 PM
Ken HPosted Nov 23, 2013, 5:50 PM
Can you explain the role of the following iterations do(What does it do)? Thank for you.
// Edited
for(i = k + 1; i < n; i++)
{
j = n - i + k;
if (i >= j) break;
swap(&ia[i], &ia[j]);
}
I tried using the following method to replace the above, but the results are not accurate.
for(i=k+1;i
j=n-1-i;
if(j<=i) break;
swap(&ia[i], &ia[j]);
}
VulpesPosted Nov 21, 2013, 6:28 PM
My solution is based on an old algorithm for generating successive permutations in lexicographic order discovered by the Indian mathematician, Narayana Pandita, in the 14th century.
It's described in this Wikipedia article under the heading "Generation in lexicographic order" :
http://en.wikipedia.org/wiki/Permutation
Ken HPosted Nov 19, 2013, 9:14 PM
You can explain this 'visit' recursive procedure(step) do? Thank for you.
Ken HPosted Nov 19, 2013, 8:19 PM
From the results seem to have an output is repeated.
It runs as follows:
VulpesPosted Nov 19, 2013, 7:37 AM
If you want the permutations in lexographical order (as in your post), then try this:
#include
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int nextperm(int ia[], int n)
{
int i, j, k = -1, l;
for(i = 0; i < n - 1 ; i++)
{
if(ia[i] < ia[i + 1]) k = i;
}
if (k >= 0)
{
l = -1;
for(i = 0; i < n ; i++)
{
if(ia[k] < ia[i]) l = i;
}
swap(&ia[k], &ia[l]);
for(i = k + 1; i < n; i++)
{
j = n - i + k;
if (i >= j) break;
swap(&ia[i], &ia[j]);
}
}
if (k == -1) return 0; // EDITED see below
for(i = 0; i < n; i++) printf("%d", ia[i]);
printf("\n");
return 1;
}
int main()
{
int i, n;
int ia[10];
printf("Please input n (max 9) : ");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
ia[i] = i + 1;
printf("%d", ia[i]);
}
printf("\n");
while (nextperm(ia, n));
return 0;
}
Abhay ShankerPosted Nov 19, 2013, 5:10 AM
#include
void swap(int *,int*);
void permute(void);
int noofpermutations();
void display(void);
int m,n,noofterms,count=0;
int a[100];
void swap(int *p1,int *p2)
{ int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int noofpermutations()
{ int permutations=1,x;
for(x=1;x<=noofterms;x++)
permutations=permutations*x;
return permutations;
}
void display()
{ int x;
for(x=0;x
printf("%d ",a[x]);
printf("\n");
count++;
}
void permute()
{ int x,y;
while(count
{ for(y=0;y
{ swap(&a[y],&a[y+1]);
display();
}
swap(&a[0],&a[1]);
display();
for(y=noofterms-1;y>0;y–)
{ swap(&a[y],&a[y-1]);
display();
}
swap(&a[noofterms-1],&a[noofterms-2]);
display();
}
}
main()
{ int x;
printf("Enter no. of terms : ");
scanf("%d",&noofterms);
printf("Enter the terms : ");
for(x=0;x
scanf("%d",&a[x]);
printf("\nPermutations are : \n");
permute();
printf("\nTotal Permutations : %d",noofpermutations());
return 0;
}
Rohatash KumarPosted Nov 19, 2013, 3:44 AM
Please check the below code:
#include
#include
#include
int lev=-1,n,val[50],a[50];
void main()
{
int i,j;
clrscr();
printf("Enter how many numbers?\n");
scanf("%d",&n);
printf("\nEnter %d numbers:\n\n",n);
for(i=0;i
val[i]=0;
j=i+1;
scanf("%d\n\n",&a[j]);
}
visit(0);
getch();
}
visit(int k)
{
int i;
val[k]=++lev;
if(lev==n)
{
for(i=0;i
printf(" ");
}
for(i=0;i
visit(i);
lev--;
val[k]=0;
}