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

## Labels

### Permutations program in java

Labels:
algorithms

Reactions: |

Subscribe to:
Post Comments (Atom)

## No comments:

## Post a Comment