Output of sorted data (2) Cyclic Processing

Source: Internet
Author: User

Arrangement and combination are the knowledge learned in high school. It is very clear in theory, but I have never tried how to implement it using code. Due to the needs of projects in the near future, I wrote an algorithm for arrangement, unexpectedly, it took a lot of time.

Several output methods are listed in the output document of data arrangement, but they are all recursive methods, but there is no need for recursion. What about the loop method? So I started to think about it.

To output all the four elements in an arrangement, it must be recursive four times, corresponding to a four-layer loop. to output all the combinations of the five elements, it must be recursive five times, it corresponds to a 5-layer loop, and at that time, I did not find a way to change recursion to a loop. So I thought about the arrangement of elements, the arrangement of two elements, and the arrangement of three elements in the simplest way.

I was inspired by the idea of output (2) of combined data.

Example: 1, 2, 3

Test: the arrangement of 1 is only {1}

Test: the arrangement of, 2 can be inserted to the front side of 1, or to the back of 1, there are two types of {1, 2}; {2, 1}

Test: the arrangement of 1, 2, and 3 can be inserted to the front and back of any element in the {1, 2} set, you can also insert it to the front and back of any element in the set {2, 1 }.


Code Implementation

Public static void creatsumdata5 (list <string> columnnamelist) {list <string> List = new list <string> (); string namefirst = columnnamelist [0]; // list <string> itemtemp3 = new list <string> (); itemtemp3.add (namefirst); list. add (itemtemp3); For (INT I = 1; I <columnnamelist. count; I ++) // skip the first processed element {string name = columnnamelist [I]; // when the first element is processed, int COUNT = List. count; List <list <string> listlast = new list <string> (); // save the last generated collection data listlast. addrange (list); // puts the collection data generated last time into the listlast list. clear (); // clear the previous data, because the set generated each time is different from the previous one for (Int J = 0; j <count; j ++) // traverse the last generated collection {list <string> itemlistlast = new list <string> (); // process the collection itemlistlast. addrange (listlast [J]); int count2 = itemlistlast. count; For (int K = 0; k <count2; k ++) // Insert a new element {list <string> itemtemp = new list <string> (); itemtemp. addrange (itemlistlast); itemtemp. insert (K, name); list. add (itemtemp) ;}list <string> itemtemp2 = new list <string> (); // Add the new element itemtemp2.addrange (itemlistlast) in the last position; itemtemp2.add (name ); list. add (itemtemp2) ;}} stringbuilder STRs = new stringbuilder (); For (Int J = 0; j <list. count; j ++) {list <string> item = list [J]; for (INT I = 0; I <item. count; I ++) {STRs. append (item [I] + ",");} STRs. appendline ("");} wriedata (STRs. tostring ();} Private Static void wriedata (string text) {string file = "zuhetestout. JS "; streamwriter sr = new streamwriter (File); Sr. writeline (text); Sr. close () ;}public static void test () {list <string> Columns = new list <string> (); columns. add ("C0"); columns. add ("C1"); columns. add ("C2"); columns. add ("C3"); // columns. add ("C4"); // creatsumdata1 (columns); // string text = createallgroupingdata (columns); // wriedata (text); creatsumdata5 (columns );}


Output of sorted data (2) Cyclic Processing

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.