Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Perm {
- private static void reverse(int[] values, int first, int last){
- int temp;
- if(first == last)
- return;
- --last;
- while (first < last){
- // swap first, last
- temp = values[first];
- values[first] = values[last];
- values[last] = temp;
- ++first;
- --last;
- }
- }
- private static boolean nextPerm(int[] values){
- int i = values.length - 1;
- if(values.length < 2)
- return false;
- for(;;) {
- int ii = i;
- --i;
- int temp;
- if (values[i] < values[ii]) {
- int j = values.length;
- while (values[i] >= values[--j]);
- // swap i, j
- temp = values[i];
- values[i] = values[j];
- values[j] = temp;
- reverse(values, ii, values.length);
- return true;
- }
- if (i == 0) {
- reverse(values, 0, values.length);
- return false;
- }
- }
- }
- public static void main(String[] args){
- int[] vals = {1,2,3,4,5, 6, 7 , 8 , 9, 10, 11, 12, 13};
- int count=0;
- do {
- count++;
- } while(nextPerm(vals));
- System.out.println("Found "+count+" Permutations");
- }
- }
Add Comment
Please, Sign In to add comment