Full permutation algorithm-java

Source: Internet
Author: User

The first step, the essence of permutation is exchange and sequential processing, for example, consider [1,2,3,4] arrangement, actually, 1 and 1,2,3,4 respectively exchanged
1 and 1 Exchange: [1],[2,3,4]
1 and 2 Exchange: [2],[1,3,4]
1 and 3 Exchange: [3],[2,1,4]
1 and 4 Exchange: [4],[2,3,1]

Then consider the above four results respectively, for example, consider
[1],[2,3,4]

In the second step, we fix [1], consider [2,3,4], think of it as 2 and 2,3,4 respectively, get the following result:
[1] fixed, 2 and 2 exchange: [1],[2],[3,4]
[1] fixed, 2 and 3 exchange: [1],[3],[2,4]
[1] fixed, 2 and 4 Exchange: [1],[4],[3,2]

At this point, consider the three results after the second step, for example, considering
[1],[2],[3,4]

The third step, we fixed [1],[2], consider [3,4], think of it as 3 and 3,4 respectively, get the following results:
[1] [2] fixed, 3 and 3 exchange: [1],[2],[3],[4]
[1] [2] fixed, 3 and 4 Exchange: [1],[2],[4],[3]

From the above analysis can be seen, in fact, the second step is the first step of the partial recursion, the same, the third step is the second part of the recursion, when the rightmost only a number, we will output

Code:

ImportJava.util.Arrays;/** * * <p> * ClassName mypermutation * </p> * <p> * Description arrangement, * </p> * * @author /c1> tkpad [email protected] * <p> * Date March 20, 2015 PM 9:48:02 * </p> * @version 
        V1.0.0 * * * *m (m≤n) elements from n different elements, arranged in a certain order, are called an arrangement of extracting m elements from n different elements. All permutations are called when m=n.  Public  class mypermutation {    //Exchange two numbers     Public void Swap(intNums[],intIintj) {if(i = = j) {return; }inttemp = Nums[i];        Nums[i] = Nums[j];    NUMS[J] = temp; } Public void permutation(intNums[],intStartintEnd) {if(start = = end) {//Direct OutputSystem.out.println (arrays.tostring (nums)); }Else{//Recursive invocation             for(inti = start; I < end; i++) {swap (nums, start, i);//Start ExchangePermutation (nums, start +1, end); Swap (Nums, I, start);//Restore the array to its original state}        }    } Public Static void Main(string[] args) {intNums[] =New int[] {1,2,3,4}; System.out.println ("full line:");NewMypermutation (). Permutation (Nums,0, nums.length); }}

Full permutation algorithm-java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.