Arrangement Algorithm Analysis and Implementation (C/OC)

Source: Internet
Author: User

In general NExtracted from different elements M( MN) Elements in a certain order into a column, called from NElement (s) MElement. Sequence, Arrangement, Permutation). According to the definition of the arrangement, the two are arranged in the same order. If the two elements are exactly the same, the order of the elements is also the same. For example, AbcAnd AbdThey are arranged differently. AbcAnd AcbAlthough the elements are identical, they are arranged in different order.

For example, the arrangement and combination of the three numbers 1 2 3, 1 3 2, 2 1 3, 2 3 1, 3 1 2, 3 2 1.

Arrangement Algorithm Analysis

Recursion can be used to cut the problem into smaller units for arrangement and combination, for example, the arrangement of 12 3 4 can be divided into 1 [2 3 4], 2 [1 34], 3 [1 24], and 4 [1 23]. Here, the rotation method is used, first, set the rotation interval to 0, rotate the rightmost number to the leftmost, and gradually increase the rotation interval, for example:

1 2 3 4-> rotate 1-> continue to roll back 2 3 4 on the right

2 1 3 4-> rotate 1 2 to 2 1-> continue to roll back 1 3 4 on the right

3 1 2 4-> rotate 1 2 3 to 3 1 2-> continue to roll back the right 1 2 4

4 1 2 3-> rotate 1 2 3 4 to 4 1 2 3-> continue to process the 1 2 3 on the right

Code Implementation (C/OC)

# Define N 5 // Number of array elements

// Main program int num [N + 1]; // when sorting, the subscript starts from 1 for (int tp = 1; tp <= N; tp ++) num [tp] = tp; // The maximum number of tp entries in the array is tpperm (num, 1); // sort and combine

// Arrange and combine num. void perm (int * num, int I) starts from I. {// num is the array to be arranged and the size remains unchanged, where the element order is changing; I is the condition for the nth number int j, k, tmp; if (I <N) {// recursive continuation, when I = N, the last number does not need to be arranged. Therefore, the output result is for (j = I; j <= N; j ++) {// j start from the number to be arranged tmp = num [j]; // save temporarily for (k = j; k> I; k --) // loop, to sort the I position, you can enter any number num [k] = num [k-1]; num [I] = tmp; // assign a value to the leftmost position. I has a value. Recursive I + 1 perm (num, I + 1); for (k = I; k <j; k ++) // restore, because the parent loop needs to sort the I (if there are several numbers after I, each number needs to be sorted once) num [k] = num [k + 1]; // restores num [j] = tmp; // restores, assign a value to the rightmost position} else {// end with a recursion. This permutation is displayed for (j = 1; j <= N; j ++) printf ("% d", num [j]); printf ("\ n ");}}





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.