Recursive algorithm for full permutation generation algorithm __

Source: Internet
Author: User
Arrange: Take m elements from n elements and arrange them in a certain order, called permutations, all permutations: When N==m, it is called the whole arrangement;
For example: The full arrangement of {1 2 3} {1 3 2} {2 1 3} {2 3 1} {3 2 1}
method One:
We can draw a graphical representation of this permutation, that is, arrange the enumeration tree, for example, the following image is the enumeration tree of {-----------------------.


Algorithm ideas: (1) n elements of the full arrangement = (n-1 elements of the full arrangement) + (another element as a prefix); (2) Exit: If only one element is fully arranged, then the output array is complete, and (3) each element is continuously placed as the first element, and then the element is prefixed, And the rest of the elements continue to be fully arranged, wait until the exit, exit after the restoration of the array;

public class Test {public static int arr[] = new int[]{1,2,3};
	public static void Main (string[] args) {perm (arr,0,arr.length-1);
		} private static void swap (int i1, int i2) {int temp = Arr[i2];
		ARR[I2] = Arr[i1];
	ARR[I1] = temp;
	 }/** * Begin~end in arr array * * For example: * arr = {} * First step: Execute perm ({1,2,3},0,2), begin=0,end=2;
	 * J=0, so execute perm ({1,2,3},1,2), begin=1,end=2;
	 * J=1,swap (arr,0,0)-->arr={1,2,3}, Perm ({1,2,3},2,2), begin=2,end=2;
	 * Because of begin==end, so the output array {* * *} * Swap (arr,1,1)--arr={1,2,3};
	 * J=2,swap (arr,1,2)-->arr={1,3,2}, Perm ({1,3,2},2,2), begin=2,end=2;
	 * Because of begin==end, the output array {1,3,2} * Swap (arr,2,1)--arr={1,2,3};
	 * J=1,SWAP (arr,0,1)--arr={2,1,3}, Perm ({2,1,3},1,2), begin=1,end=2;
	 * J=1,swap (arr,1,1)-->arr={2,1,3} perm ({2,1,3},2,2), begin=2,end=2;
	 * Because of begin==end, the output array {2,1,3} * Swap (arr,1,1)--arr={2,1,3}; * J=2,SWAP (arr,1,2) after arr={2,3,1}, and execute perm ({2,3, 1},2,2), begin=2,end=2;
	 * Because of begin==end, the output array {2,3,1} * Swap (arr,2,1)--arr={2,1,3};
	 * Swap (arr,1,0)--arr={1,2,3} * J=2,SWAP (arr,2,0)--arr={3,2,1}, execute perm ({3,2,1},1,2), begin=1,end=2;
	 * J=1,SWAP (arr,1,1)--arr={3,2,1}, Perm ({3,2,1},2,2), begin=2,end=2;
	 * Because of begin==end, the output array {3,2,1} * Swap (arr,1,1)--arr={3,2,1};
	 * J=2,SWAP (arr,2,1)--arr={3,1,2}, and execute perm ({2,3,1},2,2), begin=2,end=2;
	 * Because of begin==end, the output array {3,1,2} * Swap (arr,2,1)--arr={3,2,1}; * Swap (arr,0,2)-arr={1,2,3} * * @param arr * @param begin * @param end */public static void per m (int arr[], int begin,int end) {if (End==begin) {///one to the exit of the recursion is the output array, this array is fully arranged for (int i=0;i<=end;i++) {System.out
			. Print (arr[i]+ "");
			} System.out.println ();
		Return		} else{for (int j=begin;j<=end;j++) {swap (BEGIN,J);	The For loop puts each number in the begin~end to the begin position to Perm (arr,begin+1,end); Assuming that the begin position is determined, then the begin+1~The number in end continues the recursive swap (BEGIN,J);
 Change the past and then restore}}}


Method Two:




public class Test2 {public
	static int arr[] = new int[]{0,0,0};
	public static void Main (string[] args) {
		perm (3);
	}
	/**
	 * Array change process:
	 * 3 0 0
	 * 3 2 0
	 * 3 2 1
	 * 3 2 0
	 * 3 0 0
	 * 3 0 2
	 * 3 1 2
	 * 3 0 2
  * 3 0 0
	 * 0 0 0
	 * 0 3 0
	 * 2 3 0
	 * 2 3 1
	 * 2 3 0
	 * 0 3 0
	 * 0 3 2
	 * 1 3 2
	 * 0 3 2
	 * 0 3 0
	 * 0 0 0
	 * 0 0 3
	 * 2 0 3
	 * 2 1 3
	 * 2 0 3
	 * 0 0 3
	 * 0 2 3
	 * 1 2 3< c34/>* 0 2 3
	 * 0 0 3
	 * 0 0 0
	 * @param m
	 *
	/private static void perm (int m) {
		if (m==0) {
			for (int i=0;i<arr.length;i++) {
				System.out.print (arr[i]+ "");
			}
			System.out.println ();
			return;
		}
		else{for
			(int i=0;i<arr.length;i++) {
				if (arr[i]==0) {
					arr[i] = m;
					Perm (m-1);
					Arr[i] = 0;}}}}




References: 1. Fully arranged recursive algorithm Li Pan Rong 2. The importance of the full permutation recursive algorithm in the algorithm teaching Wu Suping 3. Research on algorithm of sorting algorithm and full permutation Chen Weidong, Baususu
Paper Download Address: Http://yunpan.cn/lk/05qsom5mle

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.