# Recursive Permutation Loop

**843789**Mar 4, 2010 6:29 AM

I'm working on a function to obtain all permutations of an array, except for the first value (0). Currently, it succeeds in finding all the permutations, but does far to many. I can't seem to figure out what I'm doing wrong. This method also will use bound techniques to minimize the amount of recursive processes. Would appreciate any help!

```
public void getRoute(int[] input, int n) {
nodes++;
sum = myMatrix.getCost(input);
if (n == 1) {
if(sum < min || min == 0) {
finishedRoutes.clear();
finishedRoutes.add(input.clone());
min = sum;
}
else if(sum == min) {
finishedRoutes.add(input.clone());
}
}
else {
for (int i = 1; i < n; i++) {
swap(input, i, n-1);
sum = myMatrix.getCost(input);
//if(sum <= min || min == 0) {
getRoute(input, n-1);
swap(input, i, n-1);
}
//}
}
}
private void swap(int[] input, int i, int j) {
int temp = input
```*;*

input[i] = input[j];

input[j] = temp;

}` `

- 42 Views
- Tags: none (add)