Efficient java version of permutation and combination algorithm

  • 2021-01-19 22:16:29
  • OfStack

This article is an example of java permutation and combination algorithm to share the specific code, for your reference, the specific content is as follows


package BeanUtil;
import java.util.ArrayList;
import java.util.List;
import com.work.core.exception.OurException;
/**
 *  Statistics of 3 The combination of the most probable occurrences 
 * 
 * @author wangmingjie
 * @date 2009-1-1 In the afternoon 01:22:19
 */
public class Copy_2_of_StatisAnyThree {
//  Combination algorithm   
//   The idea of this procedure is to open 1 Is an array whose subscripts denote 1 to m The number of elements in the array 1 That's its subscript   
//   The number represented is selected 0 It's not selected.    
//   First initialize, put the array before n An element set 1 , said the first 1 The combination is the front n Number.    
//   Then scan the array element values from left to right. 10 "Combination, find the first 1 A" 10 "And then change it to   
//   " 01 "Combine it all to the left at the same time." 1 "Move everything to the leftmost end of the array.    
//   When the first 1 A" 1 "Moves to the array m-n Is, i.e n A" 1 "When you move everything to the far right, you have to   
//   At the end of the day 1 A combination.    
//   For example, o 5 select 3 A combination of:    
//  1  1  1  0  0  //1,2,3   
//  1  1  0  1  0  //1,2,4   
//  1  0  1  1  0  //1,3,4   
//  0  1  1  1  0  //2,3,4   
//  1  1  0  0  1  //1,2,5   
//  1  0  1  0  1  //1,3,5   
//  0  1  1  0  1  //2,3,5   
//  1  0  0  1  1  //1,4,5   
//  0  1  0  1  1  //2,4,5   
//  0  0  1  1  1  //3,4,5  
  public static void main(String[] args) {
    Copy_2_of_StatisAnyThree s = new Copy_2_of_StatisAnyThree();
    s.printAnyThree();   
  }
  
  /**
   * 
   */
  public void printAnyThree(){
    int[] num = new int[]{1,2,3,4,5,6};
    print(combine(num,3));
  }
  /**
   *  from n Choose from two numbers m A digital 
   * @param a
   * @param m
   * @return
   */
  public List combine(int[] a,int m){
    int n = a.length;
    if(m>n){
      throw new OurException(" Error! An array of a Only in the "+n+" An element. "+m+" Is greater than "+2+"!!!");
    }
    
    List result = new ArrayList();
    
    int[] bs = new int[n];
    for(int i=0;i<n;i++){
      bs[i]=0;
    }
    // Initialize the 
    for(int i=0;i<m;i++){
      bs[i]=1;
    }
    boolean flag = true;
    boolean tempFlag = false;
    int pos = 0;
    int sum = 0;
    // So let's go to the first 1 a 10 Combine it, and then it becomes 01 And I'm going to take all of the left-hand side 1 Move to the leftmost part of the array 
    do{
      sum = 0;
      pos = 0;
      tempFlag = true; 
      result.add(print(bs,a,m));
      
      for(int i=0;i<n-1;i++){
        if(bs[i]==1 && bs[i+1]==0 ){
          bs[i]=0;
          bs[i+1]=1;
          pos = i;
          break;
        }
      }
      // To the left of the 1 Move everything to the leftmost part of the array 
      
      for(int i=0;i<pos;i++){
        if(bs[i]==1){
          sum++;
        }
      }
      for(int i=0;i<pos;i++){
        if(i<sum){
          bs[i]=1;
        }else{
          bs[i]=0;
        }
      }
      
      // Check if all 1 They've all moved to the far right 
      for(int i= n-m;i<n;i++){
        if(bs[i]==0){
          tempFlag = false;
          break;
        }
      }
      if(tempFlag==false){
        flag = true;
      }else{
        flag = false;
      }
      
    }while(flag);
    result.add(print(bs,a,m));
    
    return result;
  }
  
  private int[] print(int[] bs,int[] a,int m){
    int[] result = new int[m];
    int pos= 0;
    for(int i=0;i<bs.length;i++){
      if(bs[i]==1){
        result[pos]=a[i];
        pos++;
      }
    }
    return result ;
  }
  
  private void print(List l){
    for(int i=0;i<l.size();i++){
      int[] a = (int[])l.get(i);
      for(int j=0;j<a.length;j++){
        System.out.print(a[j]+"/t");
      }
      System.out.println();
    }
  }
}

Related articles: