Generate algorithms in full order

Source: Internet
Author: User
Reference link: Fully-arranged generation algorithm (1)

The original article is very detailed. For the sake of integrity, most of the text in the reference link is pasted here, and on the basis of the original article, "given an arrangement, the algorithm for finding an element in the Lexicographic Order.
Lexicographic Order

An important idea for generating algorithms in full arrangement is to arrange the elements in set a and establish a one-to-one ing relationship with a certain sequence. In this order, all the arrays in the set are output..This order must be ensured, that is, the entire arrangement can be output, and a certain arrangement cannot be output repeatedly, or a part of the output can be arranged in a loop. The Lexicographic Order is a way to output a full arrangement using this idea..Here we use a {, 3, 4} to describe how to output a full arrangement in Lexicographic Order.

First, for the sequence formed by a certain arrangement of set a, the Lexicographic Order is a way to compare the sequence size..Take a {1, 2, 3, 4} as an example. The result is 1234 <1243. The comparison method is to compare the corresponding elements of the two sequences from the beginning to the end. If the corresponding elements of the current position are the same, then, compare the next position until the first element has different positions. The element with a large element value is smaller than the element with a small value in the Lexicographic Order..A1 [1... 4] = 1234 and A2 [1... 4] = 1243, for I = 1, I = 2, the corresponding elements of the two sequences are equal, but when I = 2, there are A1 [2] = 3 <A2 [2] = 4, so 1234 <1243.

Find the next full arrangement of the Lexicographic Order:The idea of outputting full lexicographic orders is to first output the smallest Lexicographic Order, and then output a small sort of lexicographic orders ,......, Finally, the largest Lexicographic Order is output. .This involves a problem. For a known arrangement, how can we find the next arrangement in its Lexicographic Order? .The algorithm is provided here.
  • For a [1... n], find the maximum value of all K that satisfies a [k] <A [k + 1] (0 <k <n-1). If such K does not exist, it indicates that the current sorting is already the lexicographically sorted operator in all the arrays of A, and all the sorting and output are complete..
  • In a [k + 1... in N], find the element l that meets these conditions, so that a [l] gets the minimum value among all the elements of a [l]> A [K. That is to say, a [l]> A [K], but smaller than all other elements greater than a [k].
  • Exchange a [l] And a [K].
  • For a [k + 1 ...N. That is to say, a [k + 1] exchanges with a [n], a [K + 2] exchanges with a [n-1 ,......, In this way, a [1 ...N] In the Lexicographic Order.
Here we arrange a [1 .. .8] = 13876542 is used as an example to explain the above algorithm. First, we found that 1 (38) 76542 and the bracket position is the first position that satisfies a [k] <A [k + 1]. At this time, K = 2 .So in a [3... in the range of 8], find the smallest element greater than a [2] = 3 and find a [7] = 4 to meet the conditions, exchange a [2] and a [7] to get a new 14876532 ~ Returns an 8-digit range. If the range element is reversed, a [3]-A [8], a [4]-A [7], if a [5]-A [6] is exchanged separately, the next element 13876542 in the Lexicographic Order is obtained. .
Find the full sorting of the last Lexicographic Order:From the previous analysis, we can carry out Inverse Derivation and obtain the previous arrangement. The algorithm here is as follows: 1. for a [1 .. n]. Starting from the end, find the first a [k]> A [k + 1] and record it. If K <0, it indicates that a is the smallest Lexicographic Order. 2. in a [k + 1 .. in N], find the largest element a [I], 3. exchange the values of a [k] And a [I], 4. for a [k + 1 .. n] to reverse the order of elements in the interval. Here we use the previous example to illustrate that at the beginning, a = 14235678 first finds 1 (42) 35678, then K = 2, and then the maximum number smaller than 4 is 3, at this time, a [I] = 3, I = 4, exchange the values of a [I] And a [K], get a = 13245678, and finally reverse a [k + 1 .. n], and the final result is a = 13876542.
The C code is as follows:
/* Configure. For example, if a = [3 2 1 4], the next column is a '= [3 2 4 1], the next line of a' is a' = [3 4 1 2], and so on. Http://blog.csdn.net/joylnwang/article/details/7064115#/#include <stdio. h> void swap (char * array, unsigned int I, unsigned Int J) {char T = array [I]; array [I] = array [J]; array [J] = T;} // The full void fullpermutation (char * arr, int Len, int index) of the recursive output sequence {int I; If (index> = Len) printf ("% s \ n", arr); else {for (I = index; I <Len; I ++) {swap (ARR, index, I ); fullpermutation (ARR, Len, index + 1); swap (ARR, index, I) ;}} void reverse (char arr [], int start, int end) {If (Start> = END) return; int Len = start-end + 1; int I; for (I = 0; I <= Len/2; I ++) {swap (ARR, start + I, end-I) ;}} int pre_permutation (char arr [], int Len) {int K, I, Max, max_ I; for (I = len-2; I> = 0; I --) {If (ARR [I]> arr [I + 1]) {k = I; break ;}} if (I <0) {printf ("% s is the first permutation \ n", arr); Return-1 ;}max_ I = k + 1; max = arr [max_ I]; for (I = k + 1; I <Len; I ++) {If (ARR [I] <arr [k] & arr [I]> MAX) {max_ I = I; max = arr [max_ I] ;}} if (max_ I <Len) {swap (ARR, K, max_ I); reverse (ARR, k + 1, len-1);} return 0;} int next_permutation (char arr [], int Len) {int K, I, Min, min_ I; For (k = len-2; k> = 0; k --) {If (ARR [k] <arr [k + 1]) break;} If (k <0) {printf ("% s is the last permutation \ n ", ARR); Return-1;} min = arr [k + 1]; min_ I = k + 1; for (I = k + 1; I <Len; I ++) {If (ARR [I]> arr [k] & arr [I] <min) {min_ I = I; min = arr [I] ;}} if (min_ I <Len) {swap (ARR, K, min_ I); reverse (ARR, k + 1, len-1) ;}return 0 ;}int main () {int I = 1; char arr [] = "1234"; int Len = sizeof (ARR)/sizeof (ARR [0])-1; // fullpermutation (ARR, len, 0); // recursively print the entire layout. // print the print in the lexicographically order. printf ("next_permutation Demo: \ n"); do {printf ("% 02d: % s \ n ", I, arr); I ++;} while (next_permutation (ARR, Len)> = 0); I = 1; printf (" \ npre_permutation Demo: \ n "); do {printf (" % 02d: % s \ n ", I, arr); I ++ ;}while (pre_permutation (ARR, Len)> = 0); Return 0 ;}

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.