[Classic algorithm] permutation combination-full sort

Source: Internet
Author: User

Title Description:

Arrange a set of numbers, letters, or symbols to get a different combination order, for example 1 2 3 the full array of three numbers are: 1 2 3, 1 3 2, 2 1 3, 2 3 1, 3 1 2, 3 2 1.

Topic Analysis:

Set Number of P = {r1, r2, R3, ..., RN}, all arranged as Perm (p), pn = P–{rn}.

Then perm (p) = R1perm (p1), R2perm (P2), r3perm (P3), ..., Rnperm (PN). When n = 1 o'clock perm (P} = r1.

such as: all permutations of {1, 2, 3, 4, 5}

1, first look at the last two number 4, 5. Their full arrangement is 4 5 and 5 4, which is the full arrangement of 4 starting with 5 and the full arrangement of 5 beginning with 4.

Because the whole arrangement of a number is itself, the above results are obtained.

2, then look after three number 3, 4, 5. They are all arranged in 3 4 5, 3 5 4, 4 3 5, 4 5 3, 5 3 4, 5 4 36 group numbers.

That is, a fully arranged combination of 3 and 4,5, a fully arranged combination of 4-beginning and 3,5, and a full-aligned set of 5-and 3,4-prefaced.

Program code:

#include <gtest/gtest.h>using namespacestd;//Full arrangementvoidSwap (int* V1,int*v2) {    if(V1 = =v2)return; intValue = *v1; *V1 = *v2; *v2 =value;}voidShowresult (intData[],intSizeintcount) {     for(inti =0; i < size; ++i) {cout<< Data[i] <<" "; }    if(Count%8==0) {cout<<Endl; }    Else{cout<<"  "; }}voidPermutation (intData[],intBeginintEndint&count) {        if(Begin <end) {         for(inti = begin; I < end; ++i) {Swap (&data[begin], &Data[i]); Permutation (data, begin+1, end, count); Swap (&data[begin], &Data[i]); }    }    Else    {        ++count;    Showresult (data, end, count); }}test (Algo, tpermutation) {intData[] = {1,2,3,4}; intNcount =0; //4 Numbers 4! = FivePermutation (data,0,4, ncount); Assert_eq (Ncount, -); //5 Numbers 5! = +    intData1[] = {1,2,3,4,5}; Ncount=0; Permutation (data1,0,5, ncount); Assert_eq (Ncount, -); //6 Numbers 6! = 720    intData2[] = {1,2,3,4,5,6}; Ncount=0; Permutation (data2,0,6, ncount); Assert_eq (Ncount,720);}

[Classic algorithm] permutation combination-full sort

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.