get permutation

 

package algorithm.weixin;

public class Perm {

    public static void swap(int[] v, int i, int j) {
        int t;
        t = v[i];
        v[i] = v[j];
        v[j] = t;
    }

    public static void perm(int[] v, int n, int i) {
        int j;
        if (i == n) {
            for (j = 0; j < n; j++) {
                System.out.print(" " + v[j] + "" );
            }
            System.out.println("=====");
        } else {
            for (j = i; j < n; j++) {
                swap(v, i, j);
                perm(v, n, i + 1);
                swap(v, i, j);
            }
        }

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int[] v = new int[5];
        for( int i = 0; i < 5; i++ )
        {
            v[i] = i+1;
        }
        perm(v, 5, 0);
    }

}

the result would be :

 

1 2 3 4 5=====
1 2 3 5 4=====
1 2 4 3 5=====
1 2 4 5 3=====
1 2 5 4 3=====
1 2 5 3 4=====
1 3 2 4 5=====
1 3 2 5 4=====
1 3 4 2 5=====
1 3 4 5 2=====
1 3 5 4 2=====
1 3 5 2 4=====
1 4 3 2 5=====
1 4 3 5 2=====
1 4 2 3 5=====
1 4 2 5 3=====
1 4 5 2 3=====
1 4 5 3 2=====
1 5 3 4 2=====
1 5 3 2 4=====
1 5 4 3 2=====
1 5 4 2 3=====
1 5 2 4 3=====
1 5 2 3 4=====
2 1 3 4 5=====
2 1 3 5 4=====
2 1 4 3 5=====
2 1 4 5 3=====
2 1 5 4 3=====
2 1 5 3 4=====
2 3 1 4 5=====
2 3 1 5 4=====
2 3 4 1 5=====
2 3 4 5 1=====
2 3 5 4 1=====
2 3 5 1 4=====
2 4 3 1 5=====
2 4 3 5 1=====
2 4 1 3 5=====
2 4 1 5 3=====
2 4 5 1 3=====
2 4 5 3 1=====
2 5 3 4 1=====
2 5 3 1 4=====
2 5 4 3 1=====
2 5 4 1 3=====
2 5 1 4 3=====
2 5 1 3 4=====
3 2 1 4 5=====
3 2 1 5 4=====
3 2 4 1 5=====
3 2 4 5 1=====
3 2 5 4 1=====
3 2 5 1 4=====
3 1 2 4 5=====
3 1 2 5 4=====
3 1 4 2 5=====
3 1 4 5 2=====
3 1 5 4 2=====
3 1 5 2 4=====
3 4 1 2 5=====
3 4 1 5 2=====
3 4 2 1 5=====
3 4 2 5 1=====
3 4 5 2 1=====
3 4 5 1 2=====
3 5 1 4 2=====
3 5 1 2 4=====
3 5 4 1 2=====
3 5 4 2 1=====
3 5 2 4 1=====
3 5 2 1 4=====
4 2 3 1 5=====
4 2 3 5 1=====
4 2 1 3 5=====
4 2 1 5 3=====
4 2 5 1 3=====
4 2 5 3 1=====
4 3 2 1 5=====
4 3 2 5 1=====
4 3 1 2 5=====
4 3 1 5 2=====
4 3 5 1 2=====
4 3 5 2 1=====
4 1 3 2 5=====
4 1 3 5 2=====
4 1 2 3 5=====
4 1 2 5 3=====
4 1 5 2 3=====
4 1 5 3 2=====
4 5 3 1 2=====
4 5 3 2 1=====
4 5 1 3 2=====
4 5 1 2 3=====
4 5 2 1 3=====
4 5 2 3 1=====
5 2 3 4 1=====
5 2 3 1 4=====
5 2 4 3 1=====
5 2 4 1 3=====
5 2 1 4 3=====
5 2 1 3 4=====
5 3 2 4 1=====
5 3 2 1 4=====
5 3 4 2 1=====
5 3 4 1 2=====
5 3 1 4 2=====
5 3 1 2 4=====
5 4 3 2 1=====
5 4 3 1 2=====
5 4 2 3 1=====
5 4 2 1 3=====
5 4 1 2 3=====
5 4 1 3 2=====
5 1 3 4 2=====
5 1 3 2 4=====
5 1 4 3 2=====
5 1 4 2 3=====
5 1 2 4 3=====
5 1 2 3 4=====

Advertisements
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s