Non-Recursive Implementation and Recursive Implementation of Full-rank Algorithms

Source: Internet
Author: User

(1) recursive full-Rank Algorithm

(A, B, C, D) Columns

1. A is followed by B, C, and D.

2. Full arrangement of the backend of B (A, C, D)

3. The full arrangement of C following (a, B, d)

4. Full arrangement of the backend of D (A, B, and C)

And in 1 (B, c, d) can still be divided according to the above form.

/*************************************** ******************************** Compiler: GCC * Last Update: Mon 07 May 2012 07:08:58 pm CST * file name: 1.cpp * description: use next_permutation in STL to arrange all ********************************* ***************************************/ # include <iostream> using namespace STD; template <typename T> void permutation (T array [], int begin, int end) {int I; If (begin = END) {for (I = 0; I <= end; ++ I) {cout <array [I] <"" ;}cout <Endl; return ;} else {// For Loop traverses all possible conditions at the first position in the arrangement for (I = begin; I <= end; ++ I) {swap (array [I], array [begin]); permutation (array, begin + 1, end); swap (array [I], array [begin]) ;}} int main (INT argc, char ** argv) {int A [4] = {1, 2, 3, 4}; permutation (A, 0, sizeof (a)/sizeof (INT) -1); Return 0 ;}


(2) Non-recursive full-ranking algorithms, that is, sorting algorithms in Lexicographic Order.

The basic idea is:
1. Sort the initial queue and find the smallest pmin of all the queues.
2. Find the P (min + 1) which is just smaller than pmin ).
3. Execute Step 2 in a loop until a maximum arrangement is found and the algorithm ends.
For example, ABCDE is arranged, which is the smallest one in all the arrays, and the largest one is abced.
The algorithm is as follows:
Given the known sequence P = a1a2a3...
Sort P in alphabetical order, and get a minimum pmin = a1a2a3... an of P, satisfying AI> A (I-1) (1 <I <= N)
Starting from pmin, find an arrangement of P (min + 1) that is just greater than pmin, and then an arrangement that is just greater than P (min + 1.
1. From the forward (from an-> A1), find the first pair of Adjacent Elements in ascending order, that is, AI <a (I + 1 ).
If such an AI cannot be found, it indicates that the last full arrangement has been found and the result can be returned.
2. From the back to the front, find the first digital AJ larger than Ai, and switch between AI and AJ.
3. sort a (I + 1) a (I + 2 ).... the number of an series is inverted, that is, ..... A (I + 2) a (I + 1 ). As we can see from the above 1st and 2, a (I + 1)> = a (I + 2)> =...> = An, this is an ascending sequence, which should be inverted in descending order. The new arrangement is just bigger than the previous one.
4. Repeat steps 1-3 until the result is returned.

This algorithm is the idea of C ++ STL algorithm next_permutation.

/*************************************** ******************************** Compiler: GCC * Last Update: Mon 07 May 2012 07:08:58 pm CST * file name: 1.cpp * description: **************************************** * *******************************/# include <iostream> # include <cstring> using namespace STD; // exchange the void swap (int * a, int I, Int J) of the two elements marked as I and j in array a {A [I] ^ = A [J]; A [J] ^ = A [I]; A [I] ^ = A [J];} // put all the elements in array a between subscript I and subscript J into reverse order void reverse (int A [], int I, Int J) {for (; I <J; ++ I, -- j) {swap (A, I, j) ;}} void print (int A [], int length) {for (INT I = 0; I <length; ++ I) cout <A [I] <""; cout <Endl;} // obtain the full sorting, print the result void combination (int A [], int length) {If (length <2) return; bool end = false; while (true) {print (A, length ); int I, j; // find the subscript I for (I = length-2; I> = 0; -- I) of the element that does not conform to the trend) {if (a [I] <A [I + 1]) break; else if (I = 0) return ;}for (j = length-1; j> I; -- j) {if (a [J]> A [I]) break;} swap (A, I, j); reverse (A, I + 1, length-1) ;}} int main (INT argc, char ** argv) {int A [4] = {1, 2, 3, 4}; combination (, sizeof (a)/sizeof (INT); Return 0 ;}


Use STL to implement:

STL has a function next_permutation (). Its function is to return true and generate this arrangement if a sequence is sorted by dictionary in the next order, otherwise, false is returned.

/*************************************** ******************************** Compiler: GCC * Last Update: Mon 07 May 2012 07:08:58 pm CST * file name: 1.cpp * description: use next_permutation in STL to arrange all ********************************* ***************************************/ # include <iostream> # include <algorithm> using namespace STD; template <typename bidirectionaliterator> void permutation (bidirealialiterator array, int Len) {sort (array, array + Len); do {for (INT I = 0; I <Len; ++ I) {cout <array [I] <"" ;}cout <Endl ;}while (next_permutation (array, array + Len ));} int main (INT argc, char ** argv) {int A [4] = {1, 2, 3, 4}; permutation (A, sizeof () /sizeof (INT); Return 0 ;}

Source: http://blog.csdn.net/hackbuteer1/article/details/6657435

Http://plutoblog.iteye.com/blog/976216

Http://blog.csdn.net/aipb2008/article/details/2227490


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.