Full permutation algorithm (dictionary order method, SJT algorithm, Heap ' s algorithm)

Source: Internet
Author: User

First, the dictionary order method

1) from the right end of the sequence p, scan to the left until you find the first number that is smaller than the number on the right.

2) from the right, find all the smallest numbers in the larger number, ie.

3) Exchange with.

4) Flip the sequence on the right to get the next permutation of the dictionary order.

5) Repeat the above steps until you get the largest order in the dictionary order, that is, the left number is larger than the right.

Dictionary order Method void Dictionary (int length) {int * data = (int *) malloc (sizeof (int) * length); int index;for (index = 0; index < le Ngth; ++index) Data[index] = index + 1;  FILE * fp = fopen ("Dictionary.txt", "W");p rint (FP, data, length), while (nextpermutation (data, 0, length)) {print (FP, data, length);} Fclose (FP); free (data);} void swap (int data[], int i, int j) {//Exchange two elements char temp;temp = data[i];d ata[i] = data[j];d ata[j] = temp;} void reverse (int data[], int first, int last) {//Flip sequence Last--;while (First < last) {swap (data, first++, last--);}} int nextpermutation (int data[], int first, int last) {int I, j;i = last-2;while (i >= 0 && data[i] >= data[ I+1])--i;if (i = =-1) {Reverse (data, first, last); return 0;} j = last-1;while (Data[j] <= data[i]) {--j;} Swap (data, I, j); Reverse (data, i + 1, last); return 1;} void Print (FILE * fp, int data[], int length) {int index;for (index = 0; index < length; ++index) {fprintf (FP, "%d", dat A[index]);} fprintf (FP, "\ n");}

Second, SJT algorithm

The initial state is.

1) Find the maximum movable number m (when a number points to a number smaller than it is, the number is the number of movable)

2) interchange M and m pointing to the number of

3) Change the direction of all numbers larger than M

4) Repeat the above steps until you cannot find the movable number

Neighbor swap void Exchange (int length) {Item * data = (item *) malloc (sizeof (item) * length); int index, indexofmax;for (index = 0; Index < length; ++index) {data[index].digit = index + 1;data[index].direction = -1;data[index].mobile = (Index! = 0)? 1:0;} Indexofmax = length-1; FILE * fp = fopen ("Exchange.txt", "w"); Exprint (data, length, FP); while (1== data[indexofmax].mobile | | existmobile (DATA, L Ength) {if (1== data[indexofmax].mobile) {int direction = Data[indexofmax].direction;exswap (data, Indexofmax, indexofmax+direction); Indexofmax + = Direction;if ((Indexofmax = = 0 && Direction = =-1) | | (Indexofmax = = Length-1 && Direction = = 1)) {tomobileornot (data, length);}}  Else{index = Findmax (data, length); if (index = =-1) Break;int direction = Data[index].direction;exswap (data, index, index + direction); index + = direction;changedirection (data, length, index); tomobileornot (data, length);} Exprint (data, length, FP);} Fclose (FP); free (data);} int Existmobile (Item data[], int length) {//sentencedWhether there is a movable number int index;for (index = 0; index < length; ++index) {if (data[index].mobile = = 1) return 1;} return 0;} int Findmax (Item data[], int length) {//Find the largest movable number int ans = -1;for (int index = 0; index < length; ++index) {if (Data[inde X].mobile = = 1) {if (ans = =-1) ans = index;else if (data[index].digit > data[ans].digit) ans = Index;}} return ans;} void Changedirection (Item data[], int length, int index) {//change the direction of the number greater than the number of moveable for (int i = 0; i < length; ++i) {if (data[i].di git > Data[index].digit) {data[i].direction =-data[i].direction;}}} void Tomobileornot (Item data[], int length) {if (data[0].direction = = 1 && data[0].digit > Data[1].digit) data[0 ].mobile = 1;elsedata[0].mobile = 0;for (int i = 1; i < (length-1); ++i) {int direction = Data[i].direction;if (data[i ].digit > Data[i+direction].digit) data[i].mobile = 1;elsedata[i].mobile = 0;} if (data[length-1].direction = =-1 && data[length-1].digit > Data[length-2].digit) data[length-1].mobile = 1; Elsedata[length-1].mobile = 0;} void Exprint (Item data[], int length, FILE * fp) {for (int index = 0; index < length; ++index) {fprintf (FP, "%d", data[i Ndex].digit);} fprintf (FP, "\ n");} void Exswap (item data[], int i, int j) {Item tmp = Data[i];d ata[i] = data[j];d ata[j] = tmp;}

Third, Heap ' s algorithm

    Procedure generate (N:integer, A:array of any):          if n = 1 then                output (A)          else for              i: = 1; i≤n; i + = 1 Do                  generate (n-1, A)                  if n was odd then                      j←1                  else                      j←i                  swap (A[j], a[n])  

The above algorithm description is excerpted from Wikipedia

Recursive implementation. #include <stdio.h> #include <stdlib.h> #include <time.h>file * fp = NULL; int Len;int str2int (char str[]) {int i = 0;int result = 0;while (str[i]! = ' + ') {result = result * + str[i]-' 0 '; ++i;} return result;} void print (int data[]) {int i;for (i = 0; i < len; ++i) fprintf (FP, "%d", Data[i]); fprintf (FP, "\ n");} void swap (int *x, int *y) {int tmp = *X;*X = *y;*y = tmp;} void generate (int data[], int n) {int i;if (1 = = N) print (data),//return;else{for (i = 0; i < n; ++i) {Generate (data, n-1) if (n% 2 = = 1) {swap (&data[1], &data[n-1]);} else{swap (&data[i], &data[n-1]);}}} void Heapalgorithm (int n) {int * data = (int *) malloc (sizeof (int) * n); int i;for (i = 0; i < n; ++i) Data[i] = i + 1;gener Ate (data, n); free (data);} int main (int argc, char **argv) {fp = fopen ("Heap.txt", "w"); len = (argc > 1)? str2int (Argv[1]): 10;clock_t time = Clo CK (); Heapalgorithm (len); time = Clock ()-time;printf ("Heap ' s algorithm takes%d clocks (%f seconds). \ n ", Time, ((float) time)/clocks_per_sec); return 0;} 

Full permutation algorithm (dictionary order method, SJT algorithm, Heap ' s algorithm)

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.