Java recursion implements full string arrangement and full combination, and java Recursion

Source: Internet
Author: User

Java recursion implements full string arrangement and full combination, and java Recursion

Permutation and combination algorithms are widely used and need to be mastered. To lower the threshold, This article focuses on the logic and simplicity of algorithms and does not focus on algorithm efficiency. in combination with the Implementation of Online books and their own needs, there are four goals listed here:

1. Full arrangement of all elements: AB and ba are in full order );
2. Full combination of all elements: the full combination of AB is a, B, AB (Order-independent );
3. Determine the combination of m elements selected from n elements. Which of the following are the combination of AB, ac, and bc elements selected in abc;
4. Determine the arrangement of m elements selected from n elements: AB, ba, ac, ca, bc, and cb are selected in abc;

We can find that the arrangement of m elements selected from n elements is actually after finding the combination of m elements selected from n elements, the element set (array) composed of each combination is fully arranged. Therefore, it is an assembly function and examples are not listed. For the other three objectives, see the code:

Public final class PermutationCombinationHolder {/** full combination of array elements */static void combination (char [] chars) {char [] subchars = new char [chars. length]; // The array used to store the child combination data // The full combination problem is the combination of one element in all elements (marked as n), plus the combination of the two elements selected... and for (int I = 0; I <chars. length; ++ I) {final int m = I + 1; combination (chars, chars. length, m, subchars, m) ;}/ *** how to combine m elements for n elements. the principle is as follows: * from the back to select, after selecting the position I, and then in the front of the I-1 to select the m-1. * For example, select 3 elements from 1, 2, 3, 4, and 5. * 1) Select 5, and then select 2 from the first four, and then select 2 from the first four as a subproblem. Recursive: * 2) If 5 is not included, select 4 directly, then select 2 in the first three, and then select 2 in the first three as a subproblem, recursively; * 3) If 4 is not included, select 3 directly, then select 2 in the first two, just two. * vertically, 1 and 2 and 3 are exactly a for loop. The initial value is 5 and the final value is m. * horizontally, this problem is a recursion of the first I-1. */static void combination (char [] chars, int n, int m, char [] subchars, int subn) {if (m = 0) {// exit for (int I = 0; I <subn; ++ I) {System. out. print (subchars [I]);} System. out. println () ;}else {for (int I = n; I> = m; -- I) {// select a subchars [m-1] = chars [I-1] from the back and forth; // select a post-combination (chars, I-1, m-1, subchars, subn); // from the first I-1 selected on the first To Recursive}/** array elements in the full arrangement */static void permutation (char [] chars) {permutation (chars, 0, chars. length-1);}/** the sub-array from index begin to index end participates in the full arrangement */static void permutation (char [] chars, int begin, int end) {if (begin = end) {// exit for (int I = 0; I <chars. length; ++ I) {System. out. print (chars [I]);} System. out. println () ;}else {for (int I = begin; I <= end; ++ I) {// each character is invariably set to the first if (canSwap (chars, begin, I) of the array or sub-array {// deduplicated swap (chars, begin, I ); // exchange permutation (chars, begin + 1, end); // recursively obtain the full swap (chars, begin, I) of the subarray ); // restore }}} static void swap (char [] chars, int from, int to) {char temp = chars [from]; chars [from] = chars [to]; chars [to] = temp;} static boolean canSwap (char [] chars, int begin, int end) {for (int I = begin; I <end; ++ I) {if (chars [I] = chars [end]) {return false ;}} return true ;} public static void main (String [] args) {final char [] chars = new char [] {'A', 'B', 'C'}; permutation (chars ); system. out. println ("=========================="); combination (chars );}}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.