Arrange the most detailed report

Source: Internet
Author: User

I want to find the full arrangement of any set

Solution: assume that the data is stored in the array [0, 1 ..., in length-1], the number of first = 0 is fixed first, and array [1 ,..., length-1], and so on. When first = length-1 is, the data in the array is output. When array [1 ,..., after the length-1] is fully arranged (first = 0 at this time), you need to set the subscript to 1 ,..., all numbers of length-1 are exchanged with first, that is, each number in the array must be placed at first = 0, and so on.

 

Specific algorithms (Java version)

1 // exchange data marked as M and N in the array. 2 public static void swap (INT [] array, int M, int N) {3 int x = array [m]; 4 array [m] = array [N]; 5 array [N] = X; 6} 7 8 // output data in the array 9 public static void output (INT [] array) {10 for (INT I = 0; I <array. length; I ++) {11 system. out. print (array [I] + "\ t"); 12} 13 system. out. println (); 14} 15 16 public static void permutation (INT [] array, int first) {17 if (first = array. length-1) {// first has reached the last element, that is, all elements have fixed 18 output (array ); 19} else {20 for (INT I = first; I <array. length; I ++) {21 swap (array, I, first); // exchange 22 permutation (array, first + 1); // The previous elements of first are fixed, next, fix the first + 123 swap (array, I, first); // restore 24} 25} 26} 27 28 public static void main (string [] ARGs) {29 int [] array = {1, 2, 3}; // 30 permutation (array, 0); 31}

 

Arrange the most detailed report

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.