C++: recursively computer all permutaions of digits 0 - 9
- by Nate
I have a homework assignment where part of the requirement is to recursively compute all the permutations of integers 0 - 9. The professor actually gave us the algorithm for this part of the question. I've finished the rest of the assignment, but I can't get the permute function working...I'm implementing it exactly like it was shown on the assignment information. However, when I run it each permutation is repeated multiple times (and I'm not sure if I'm even getting all the correct permutations.)
I think he must've made a mistake on the assignment instructions. I've been working on this for a couple of hours and can't seem to figure out where I'm going wrong. Can anybody help point me in the right direction?
Here's the current code:
void permute(int v[], int curr) {
for (int i = curr; i < MAX; i++) {
swap(v[i], v[curr]);
permute(v, curr + 1);
swap(v[curr], v[i]);
}
}
EDIT: Actually, right after posting this I realized it has to do with the swap, right? Because right now i and curr are the same, so I'm swapping identical numbers. Hm, should it be swap(v[i], v[curr+1])?