-2

This question already has an answer here:

I m trying to get the possible combinations of a given set of numbers say for example 123

The possible combinations would be

123 132 213 231 312 321

For this i have written a code as below -

import java.util.ArrayList;

/* Name of the class has to be "Main" only if the class is public. */
public class Main {

/**
 * @param args the command line arguments
 */
static ArrayList list;

public static void main(String[] args) {
    // TODO code application logic here
    Main nm = new Main();
    nm.list = new ArrayList();
    /*
    for(int i=1; i<= 4; i++) {
        list.add(i);
    }

    list.remove(list.indexOf(1));

    for(int i=0; i<= 2; i++) {
        System.out.print(list.get(i));
    }
    */
    nm.test1();        
}

public void test1() {        
    for(int i=1; i<= 4; i++) {
        if(!list.contains(i)) {
            list.add(i);
            test1();
            list.remove(list.indexOf(i));
        }
    }           
    if(list.size() == 4) 
    {
        for(int i=0; i< 4; i++) {
            System.out.print(list.get(i));
        }
        System.out.println("");
    }
}

}

This gives the correct output as shown here in the link

I was thinking about if this the right approach or if there can be any optimizations done so that this code may be used to get combinations of numbers of 10 to 20 digits or more.

Regards

marked as duplicate by user40980, GlenH7, Dan Pichelman, FrustratedWithFormsDesigner, Tulains Córdova Jun 27 '14 at 21:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

I was after the same thing a came to the conclusion that is it permutations that I wanted and this "algorithm to generate permutations explained" did what I wanted. The example is in Ruby but it is quite easy to understand (and quite simple).

Edit:

From the mentioned website above:

First things first, the basics: Permutations are how many times a set of items you have can be ordered. For example, there are 24 permutations of 1234: 1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431, 3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321. If some of the items are the same, then there are fewer permutations of sets of 4. For example, there are only 4 permutations of 1112: 1112, 1121, 1211, 2111

The algorithm to generate all of the permutations requires a way of comparing and ordering the items. The first permutation in the series is the one with all of the permutations in ascending order, for example, 12345 or ABCDE. It then uses this permutation to generate the next permutation in the series until the last one which has all of the items in descending order: 54321 or EDCBA.

In words, the algorithm goes as follows:

  1. Starting from the right hand side of the list, go backwards and find the first item which is less than the item immediately after it and let that be i. list[i] < list[i+1]. If none of the items satisfy the criteria (i.e. the whole list is in descending order, then you’ve finished).
  2. Starting from the right hand side of the list, go backwards and find the first item which is larger than item i. Mark this as j.
  3. Swap the items at i and j.
  4. Reverse the items from i+1 to the end of the list.
  • 3
    Could you expand on the answer? Providing a link to another off site page doesn't really explain the answer here, and if the source disappears your answer becomes very difficult to use. Furthermore, for ruby, why not just do [1,2,3].permutation.to_a? – user40980 Jun 27 '14 at 19:26
  • @MichaelT Answer expanded - Satisfactory? For Ruby Why not... Because that can't be used in other programming languages (try it in C#) :) My suggestion can be implemented in most programming languages. For me, the link to This question already has an answer here: is useless - I can't use it in any other language other than Python – Nicholas Ring Jun 28 '14 at 3:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.