Non-recursive, full sorting of output sets in order

Source: Internet
Author: User

Description
Non-recursive, ordered output sets are arranged in full order, which are frequently used in the test interview. The full arrangement of recursive output sets is relatively simple, rather than Recursive Implementation, which requires some tips.
The full arrangement is to generate all sorted sets by lexicographically based on the elements in the Set (which can be numbers or characters) and output these arrays. Use the distance of the number set, and the {1, 2, 3} of the set are all sorted in order:

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1

In this way, a total of six arrays are generated for the three elements, that is, M is generated for the M elements! In full order.

 

Non-recursive ideas
There is another article about the recursive solution. Here we will introduce the non-recursive approach. Take the number set {1, 2, 3} as an example.

The first sequence in the sorted sequence set generated by this set is 1 2 3, which is easy to see. The question is, how can we generate the next ordered sequence based on this sequence? The next ordered sequence is just greater than the previous sequence in the Lexicographic Order, which should be 1 3 2. It can be seen that the 2 and 3 positions in the first sequence are obtained. The next sequence after 1 3 2 is 2 1 3, which is to put the last 2 before 1. After 2 1 3 is 2 3 1. On the basis of 2 1 3, the last 3 is changed to the front of 1. A very intuitive feeling is to look up from the back to the front and exchange it with a certain number in the front.

The specific algorithm description takes the number set {1, 2, 3} as an example:

1. the first sequence is the elements of the current set connected.

2. Search from the back to the front for a number greater than the front (from small to large, called reverse order ), if no result is found, all the permutation is generated (from 123 to 321). If it is found (for example, 1 and 3 in 2 1 3 are a reverse order), it stops.

3, take 213 as an example, is to remember the position of 3 For I, and then from the forward to find the number, to find just greater than 1 (position for the i-1) number, here is obviously 3.

4, switch the number found in the third step and the number of position for the I-1.

5. Set the number from position I to the end of the set, for example, from *** 321 (position I is 3) to *** 123 (position I is 1 ).

6. repeat this process until the reverse order is not found, all sequences are generated.

 

Simple sample code

[Cpp]
Void swap (int * p, int * q)
{
Int tmp;
Tmp = * p;
* P = * q;
* Q = tmp;
}
 
Void mknewseq (int * data, int start, int last)
{
While (start <last)
{
Swap (& data [start], & data [last]);
Start ++;
Last --;

}
}
 
Void showdata (int * data, int num)
{
Int I = 0;
For (I = 0; I <num; I ++)
{
Printf ("% d", data [I]);
}
Printf ("\ n ");
}
Int findall (int * data, int num)
{
Int I, j;
Int lastdata = num-1;
Int tmp;

For (I = lastdata; I> 0; I --)
{
If (data [I]> data [I-1]) break;
}
If (0 = I) return 0;
Tmp = I;
For (j = lastdata; j> = I; j --)
{
If (data [j]> data [I-1]) & (data [j] <data [tmp])
Tmp = j;
}
 
Swap (& data [tmp], & data [I-1]);
 
Mknewseq (data, I, lastdata );
 
Return 1;

}
 
Int main ()
{
Int data [4] = {1, 2, 4 };
Showdata (data, 4 );
While (findall (data, 4 )){

Showdata (data, 4 );
}
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.