Java permutation and combination algorithm

Source: Internet
Author: User
1. Full Sorting Algorithm
Import java. util. ArrayList;/*** full Sorting Algorithm **/public class Arrange {private int total = 0; private ArrayList
 ArrangeList = new ArrayList
 (); Public Arrange () {} private void swap (String list [], int k, int I) {String c3 = list [k]; list [k] = list [I]; list [I] = c3;} public void perm (String list [], int k, int m) {if (k> m) {StringBuffer sb = new StringBuffer (); for (int I = 0; I <= m; I ++) {sb. append (list [I]). append (",");} if (sb. length ()> 0) {sb. setLength (sb. length ()-1);} arrangeList. add (sb. toString (); total ++;} else {for (int I = k; I <= m; I ++) {swap (list, k, I ); perm (list, k + 1, m); swap (list, k, I) ;}} public int getTotal () {return total;} public ArrayList
 GetArrangeList () {return arrangeList;} public static void main (String args []) {String list [] = {"1", "2", "3 ", "4", "5"}; Arrange ts = new Arrange (); ts. perm (list, 0, list. length-1); for (int I = 0; I <ts. getArrangeList (). size (); I ++) {System. out. println (ts. getArrangeList (). get (I);} System. out. println ("total:" + ts. total );}}

2. combination algorithms

Import java. util. ArrayList; import java. util. BitSet; public class Combination {private ArrayList
 CombList = new ArrayList
 (); Public void mn (String [] array, int n) {int m = array. length; if (m <n) throw new IllegalArgumentException ("Error m <n"); BitSet bs = new BitSet (m); for (int I = 0; I <n; I ++) {bs. set (I, true) ;}do {printAll (array, bs) ;}while (moveNext (bs, m ));} /*** 1. start the start position of the first true segment, end position * 2. Set the first true segment to false * 3. Set the value of the array from 0 subscript to true * 4 when the number of elements in the first true segment is reduced to the position of the next object. set the end position of the first true segment to true ** @ param bs indicates whether the flag is displayed * @ pa Ram m array length * @ return boolean whether there are other combinations */private boolean moveNext (BitSet bs, int m) {int start =-1; while (start <m) if (bs. get (++ start) break; if (start> = m) return false; int end = start; while (end <m) if (! Bs. get (++ end) break; if (end> = m) return false; for (int I = start; I <end; I ++) bs. set (I, false); for (int I = 0; I <end-start-1; I ++) bs. set (I); bs. set (end); return true ;} /*** output the generated combination result ** @ param array * @ param bs array element indicates whether the flag set is displayed */private void printAll (String [] array, bitSet bs) {StringBuffer sb = new StringBuffer (); for (int I = 0; I <array. length; I ++) if (bs. get (I) {sb. append (array [I]). append (',');} sb. setLength (sb. length ()-1); combList. add (sb. toString ();} public ArrayList
 GetCombList () {return combList;} public static void main (String [] args) throws Exception {Combination comb = new Combination (); comb. mn (new String [] {"1", "2", "3", "4", "5", "6"}, 3 ); for (int I = 0; I <comb. getCombList (). size (); I ++) {System. out. println (comb. getCombList (). get (I); String [] list = comb. getCombList (). get (I ). split (","); Arrange ts = new Arrange (); ts. perm (list, 0, list. length-1); for (int j = 0; j <ts. getArrangeList (). size (); j ++) {System. out. println ("/u0009" + ts. getArrangeList (). get (j ));}}}}
3. Call the permutation and combination algorithm tool class
import java.util.ArrayList;public class ArrangeCombine {public static ArrayList
   getArrangeOrCombine(String[] args,int n, boolean isArrange) throws Exception{if (args.length<=0) {throw new Exception("array.length<=0");}if (n>args.length) {throw new Exception(" n>array.length");}Combination comb = new Combination();comb.mn(args, n);if (!isArrange) {return comb.getCombList();}ArrayList
   arrangeList = new ArrayList
  ();for (int i = 0; i < comb.getCombList().size(); i++) {String[] list = comb.getCombList().get(i).split(",");Arrange ts = new Arrange();ts.perm(list, 0, list.length-1);for (int j = 0; j < ts.getArrangeList().size(); j++) {arrangeList.add(ts.getArrangeList().get(j));}}return arrangeList;}public static void main(String[] args) {try {ArrayList
   arrangeOrCombine = ArrangeCombine.getArrangeOrCombine(new String[]{"1","2","3","4","5","6"}, 3, false);ArrayList
   arrangeOrCombine2 = ArrangeCombine.getArrangeOrCombine(new String[]{"1","2","3","4","5","6"}, 3, true);for (int i = 0; i < arrangeOrCombine.size(); i++) {System.out.println(arrangeOrCombine.get(i));}System.out.println("Total:"+arrangeOrCombine.size());System.out.println("=============================");for (int i = 0; i < arrangeOrCombine2.size(); i++) {System.out.println(arrangeOrCombine2.get(i));}System.out.println("Total:"+arrangeOrCombine2.size());} catch (Exception e) {e.printStackTrace();}}}

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.