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.


VulpesRank #55 · 29.2K reputationPosted Nov 20, 2013, 12:22 AM EST
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;
}
VulpesRank #55 · 29.2K reputationPosted Nov 24, 2013, 5:55 AM EST
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.
Rohatash KumarRank #12 · 57.7K reputationPosted Nov 18, 2013, 10:44 PM EST
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;
}
Abhay ShankerRank #375 · 6.7K reputationPosted Nov 19, 2013, 12:10 AM EST
#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;
}
VulpesRank #55 · 29.2K reputationPosted Nov 19, 2013, 2:37 AM EST
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;
}
Ken HRank #2,315 · 610 reputationPosted Nov 19, 2013, 3:19 PM EST
From the results seem to have an output is repeated.
It runs as follows:
Ken HRank #2,315 · 610 reputationPosted Nov 19, 2013, 4:14 PM EST
You can explain this 'visit' recursive procedure(step) do? Thank for you.
VulpesRank #55 · 29.2K reputationPosted Nov 21, 2013, 1:28 PM EST
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 HRank #2,315 · 610 reputationPosted Nov 23, 2013, 12:50 PM EST
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]);
}
VulpesRank #55 · 29.2K reputationPosted Nov 23, 2013, 1:21 PM EST
Ken HRank #2,315 · 610 reputationPosted Nov 23, 2013, 2:35 PM EST
Ken HRank #2,315 · 610 reputationPosted Nov 23, 2013, 9:07 PM EST
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++;
}
VulpesRank #55 · 29.2K reputationPosted Dec 3, 2013, 6:32 AM EST
For a description of how this algorithm works, check out this link:
www.cs.ucf.edu/~dmarino/ucf/cop3502/lec/Permutations.doc?
Ken HRank #2,315 · 610 reputationPosted Dec 13, 2013, 10:40 PM EST