Original: Java Permutation algorithm code implementation
Source code: Http://www.zuidaima.com/share/1550463479024640.htm
Java permutation combination algorithm, the need to study children's shoes can be downloaded, the results of the operation are as follows:
Package com.zuidaima.test;/*** @author Www.zuidaima.com**/public class Pailie {public static void main (string[] args) { Int[] ia = {1, 2, 3, 4,5,6,7,8,9,10}; int n = 4; System.out.println ("permutation result:"); Permutation ("", IA, N);//SYSTEM.OUT.PRINTLN ("combination result:");//combination (IA, n); public static void permutation (String s, int[] ia, int n) {if (n = = 1) {for (int i = 0; i < IA.L Ength; i++) {System.out.println (s+ia[i]); }} else {for (int i = 0; i < ia.length; i++) {String ss = ""; SS = s+ia[i]+ ""; Create a sub-array with no element I int[] II = new INT[IA.LENGTH-1]; int index = 0; for (int j = 0; J < Ia.length; J + +) {if (J! = i) {ii[index++] = ia[j]; }} permutation (ss, II, N-1); }}} Public static void combination (int[] ia, int n) {combination ("", IA, N); public static void combination (String s, int[] ia, int n) {if (n = = 1) {for (int i = 0; i < IA.L Ength; i++) {System.out.println (s+ia[i]); }} else {for (int i = 0; i < ia.length-(n-1); i++) {String ss = ""; SS = s+ia[i]+ ","; Establish a subarray starting from I int[] II = new INT[IA.LENGTH-I-1]; for (int j = 0; J < Ia.length-i-1; J + +) {Ii[j] = ia[i+j+1]; } combination (ss, II, N-1); } } }}
Code implementation of Java permutation combination algorithm