Non-recursive full-permutation algorithm C ++

Source: Internet
Author: User

// Full Sorting Algorithm: <br/> // The idea is very simple and classic. The goal is to output results in ascending order; non-recursive method <br/> // you do not know how to describe it, so here is an example: <br/> // If dbeca is currently arranged, perform the following sorting (after manual sorting, the result is obviously dcabe ). <Br/> // 1) right-to-left search for the first string from small to large (2 in length). Here is be <br/> // 2) B is followed by the string ECA, where the minimum character greater than B is C <br/> // 3) Exchanges B with C, the current arrangement is dceba <br/> // 4) sort the string EBA after C from small to large, then the current arrangement is dcabe (this is the result) <br/> // 5) Repeat Step 1 <br/> // language used: C ++ <br/> // tested: it takes about 8 minutes to arrange the 10 characters in full mode, and it mainly takes about 10 seconds to print the results on the screen. <br/> // it only takes about 10 seconds to print the results; CPU usage is amd 3500 + <br/> // If any error occurs, please correct it! </P> <p> # include "stdafx. H "<br/> # include <iostream> <br/> # include <vector> <br/> # include <cassert> <br/> # include <algorithm> </ p> <p> typedef char kind; // define the type of comparative data. Here is Char. Only for ease of modification </P> <p> bool compare (const STD: vector <kind> & array, int nindex1, int nindex2) <br/> {<br/> // compare the values of array [nindex1] and array [nindex2]. If the value is greater than, true is returned. Otherwise, false is returned; <br/> // [in] array: An array <br/> // [in] nindex1, nindex2: subscript of array <br/> // attached: define this function only to enhance the ease of modification; If kind is of another type, such as string type, <br/> // modify only the comparison rules of this function. <br/> // Note: equal conditions are obviously ignored, assume that the elements are not equal </P> <p> assert (array [nindex1]! = Array [nindex2]); </P> <p> return (array [nindex1]> array [nindex2]); <br/>}</P> <p> int firstsmalltobig (const STD: vector <kind> & array) <br/> {<br/> // in the array, search for the first string (2 in length) from the right to the left, returns the position of the string in <br/> // array <br/> // [in] array: An arrangement <br/> // return value: the position of the searched string in arrray. If the returned value is-1, it indicates that no value is found. </P> <p> int NPOs =-1; // return value </P> <p> for (INT I = array. size ()-1; I >=1; -- I) <br/>{< br/> If (compare (array, I, I-1) <br/> {<Br/> NPOs = I-1; <br/> break; <br/>}</P> <p> return NPOs; <br/>}</P> <p> int minbigthan (const STD: vector <kind> & array, int nindex) <br/>{< br/> // obtain array [nindex + 1] To the end of array, location of the smallest element larger than array [nindex] in array <br/> // [in] array: An arrangement <br/> // [in] nindex: subscript of array <br/> // return value: array [nindex + 1] To the end of array, <br/> // position (I .e. subscript) of the smallest element larger than array [nindex] in array </P> <p> int NPOs =-1; // return value <br/> STD: vector <kind> V; // Minimum element used for storage (array [nindex + 1] To the end of array) greater than array [nindex] <br/> v. push_back (array [nindex]); <br/> STD: vector <kind >:: iterator it = v. begin (); </P> <p> // find the first element larger than array [nindex] <br/> for (INT I = nindex + 1; I <array. size (); ++ I) <br/>{< br/> If (compare (array, I, nindex )) <br/> {<br/> V [0] = array [I]; <br/> NPOs = I; <br/> break; <br/>}< br/> + + I; <br/> if (I = array. size () <br/>{< br/> SERT (NPOs! =-1); <br/> return NPOs; <br/>}</P> <p> assert (V [0]! = Array [nindex]); </P> <p> // obtain array [nindex + 1] To the end of array, location of the smallest element larger than array [nindex] in array <br/> for (; I <= array. size (); ++ I) <br/>{< br/> If (compare (array, I, nindex) & array [I] <V [0]) <br/>{< br/> V [0] = array [I]; <br/> NPOs = I; <br/>}</P> <p> assert (NPOs! =-1); <br/> return NPOs; <br/>}</P> <p> void swap (STD: vector <kind> & array, int nindex1, int nindex2) <br/> {<br/> // array [nindex1] and array [nindex2] exchange elements </P> <p> assert (nindex1> = 0 & nindex1 <Array. size (); <br/> assert (nindex2> = 0 & nindex2 <array. size (); </P> <p> STD: vector <kind> temp; <br/> temp. push_back (array [nindex1]); <br/> array [nindex1] = array [nindex2]; <br/> array [nindex2] = temp [0]; </P> <P >}</P> <p> void print (const STD: vector <kind> & array) <br/>{< br/> // print the result </P> <p> Using STD: cout; <br/> Using STD: Endl; </P> <p> for (INT I = 0; I <array. size (); ++ I) <br/> cout <array [I]; <br/> cout <Endl; </P> <p >}< br/> void permutation (STD: vector <kind> & array) <br/> {<br/> // arrange all <br/> // [in] array: An array in full order; requires Ascending Order </P> <p> int nindex1, nindex2; </P> <p> int nnum = 0; // number of statistical results </P> <p> Print (array); // print a knot Result <br/> nnum ++; <br/> while (1) <br/>{< br/> nindex1 = firstsmalltobig (array ); // from the right to the left, find the first string from small to large (2 in length) <br/> If (nindex1 =-1) break; // if it cannot be found, it indicates that the program has ended in descending order. <Br/> nindex2 = minbigthan (array, nindex1); // get array [nindex + 1] To the end of array, location of the smallest element larger than array [nindex] in the array <br/> swap (array, nindex1, nindex2 ); // array [nindex1] and array [nindex2] exchange elements <br/> STD: Sort (array. begin () + nindex1 + 1, array. end (); // sorts the elements from array [nindex + 1] To the end of array </P> <p> Print (array ); // print a result <br/> nnum ++; <br/>}</P> <p> STD: cout <"number of results: "<nnum <STD: Endl; </P> <p >}</P> <p> const int nsize = 10; // number of elements <br/> int main (INT argc, char * argv []) <br/> {<br/> kind a [20] = {'A', 'B', 'C', 'D', 'E', 'F ', 'G', 'h', 'I', 'J', 'k', 'l', 'M', 'n'}; <br/> STD :: vector <kind> array (A, A + nsize); </P> <p> permutation (array); </P> <p> return 0; <br/>}

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.