Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

Permutations program in java


class permutations {

static void print(int v[], int n) {
for (int i = 0; i < n; i++) {
System.out.print(v[i] + " ");
}
System.out.println("\n");
}

static void permute(int v[], int start, int n) {
if (start == (n - 1)) {
//if its the end of the sequence genereated then print them
print(v, n);
}
else {
for (int i = start; i < n; i++) {
//swap the start element woith the ith element to get n first sequeces
int temp = v[start];
v[start] = v[i];
v[i] = temp;
permute(v, start + 1, n);
//of the n the first is kept constant the same is applied for the rest sequence
v[i] = v[start];
v[start] = temp;
}
}

}

public static void main(String[] args) {
System.out.println("learn 2day permutaions!");
int v[] = {
1, 2, 3};
//this is the array which should contain the items to be permuted
permute(v, 0, v.length);
}
}



Simple Permutations program in java

No comments:

Post a Comment

Search 24 Bytes

Loading...