Algorithm note generation arrangement of 03--inductive method

Source: Internet
Author: User

Generating permutations

The generation arrangement is the whole arrangement of n number, obviously the time complexity is n-digit O (n^k)

Assuming that you can generate all permutations of the number of n-1, you can extend the arrangement of Generation,....., N.

For example, the 1 generation arrangement is 1

The resulting permutations of 2,1 and

The generation of a-to-be is arranged on the basis of the generation arrangement:

1 in 1th place, 2,3 's generation arrangement

2 in 1th Place, 1,3 's generation arrangement

3 in 1th place, 2,3 's generation arrangement

Then the generation arrangement that is extended to,..., N is:

1 in 1th position, 2,..., N generation arrangement

2 in 2nd place, 1,3,... N generation arrangement

....

N in 1th Place, 2,..., N generation arrangement

First consider the number 1 in the first place, then the 2,,..., N generation arrangement,

Ideas:

1. On the 0 to n-1 position of the number generation arrangement, the practice from 1,,.., N in order to take out the number I (I from 1 to n) and position J (J from 0 to N-1) on the digital exchange, and then the position j+1 to the n-1 position of the number generation arrangement.

2. A number generation arrangement of J (J from 1) to n-1 position, in which the number I (starting from 2) is sequentially taken out from 2,,.., N and the number on position J is exchanged, and the number generated in position j+1 to n-1 position is arranged.

3 .... When j = N, generates an arrangement and prints

4. Retrace the previous step, take the number of words i+1 and J on the digital exchange, and the sequence after the j+1 to generate a sort

5. Until I>n, end

#include <iostream>  using namespace std;  void permutation (int* array, int ilength, int icurstep) {if (ilength = = icurstep)//If fixed position pointer refers to the last digit printed array {for (int i=0;i< ilength;i++) {cout<<array[i]<< "";} cout<< "\ n"; return;} else{for (int i = icurstep; i<ilength; i++)  //i is the scan pointer from the fixed pointer position to the last digit position {swap (array[icurstep],array[i]); Fixed the number from each position from Icurstep to Icurstep on permutation (array,ilength,icurstep+1); swap (Array[i],array[icurstep]); Recover array}} int main () {     int array[] = {1,2,3,4};     Permutation (array, 4, 0); }

Algorithm note generation arrangement of 03--inductive method

Related Article

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.