recursive permutation algorithm (numeric)

Hello everyone,

I have a task to write recursive permutation algorithm for C++. Here is what I have got, but when I run it, in the middle of permutation it misses some numbers, I can't figure out where is in my code the problem.

Please help me.

#include <iostream>
using namespace std;

void print(int a[], int s);
void swap(int&, int&);
int Max(int a[], int);
bool nextpermut(int a[], int);


int main() {
int a[4]={1,2,3,4};
nextpermut(a,4);

return 0;
}


void print(int a[], int s) {
for(int i=0;i<s;i++)
cout<<a[i]<<" ";
cout<<endl;
}
bool nextpermut(int a[], int s)
{
print (a,s);
int i;
i=Max(a,s);
if(i!=0)
swap(a[i],a[i-1]);
else
{
int temp=a[0];
for(int i=1;i<s;i++)
swap(a[i],a[i-1]);
a[s]=temp;
}
nextpermut(a,s);
return;
}

void swap(int& a, int& b)
{
int t=a; a=b; b=t;
}

int Max(int a[], int s)
{
int m=0;
for (int i=1; i<s; i++)
if(a[m]<a[i])
m=i;
return m;
}
Topic archived. No new replies allowed.