0

How can I make n-level loop similar to my manual 5 level for loop in java

public class TestPermutation
{

  public static void main(String[] args)
  {
    int[] input = {1,2,3,4,5};
    ma(input);
  }

  public static void ma(int[] input)
  {
    int n = input.length;
    for(int i=0;i<n;i++)
    {
      System.out.println(input[i]);
      for(int j=i+1;j<n;j++)
      {
        System.out.println(input[i]+" "+input[j]);
        for(int k=j+1;k<n;k++)
        {
          System.out.println(input[i]+" "+input[j]+" "+input[k]);
          for(int l=k+1;l<n;l++)
          {
            System.out.println(input[i]+" "+input[j]+" "+input[k]+" "+input[l]);
            for(int m=l+1;m<n;m++)
            {
              System.out.println(input[i]+" "+input[j]+" "+input[k]+" "+input[l]+" "+input[m]);
            }
          }
        }
      }
    }
  }
}

How can we do? Anyway this is an output from my code.

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 5
1 2 4
1 2 4 5
1 2 5
1 3
1 3 4
1 3 4 5
1 3 5
1 4
1 4 5
1 5
2
2 3
2 3 4
2 3 4 5
2 3 5
2 4
2 4 5
2 5
3
3 4
3 4 5
3 5
4
4 5
5
johnchen902
  • 9,531
  • 1
  • 27
  • 69

3 Answers3

1

No recursion, only some designed loop. Live demo.

import java.util.Stack;

public class Test {

    public static void main(String[] args) {
        int[] input = { 1, 2, 3, 4, 5 };
        ma(input);
    }

    public static void ma(int[] input) {
        Stack<Boolean> stack = new Stack<>();
        while (true) {
            while (stack.size() < input.length) {
                stack.push(true);
                print(stack, input);
            }
            while (!stack.isEmpty() && !stack.peek())
                stack.pop();
            if (stack.isEmpty())
                break;
            stack.pop();
            stack.push(false);
        }
    }

    public static void print(Stack<Boolean> stack, int[] input) {
        boolean begin = true;
        for (int i = 0; i < stack.size(); i++)
            if (stack.get(i)) {
                if (begin)
                    begin = false;
                else
                    System.out.print(' ');
                System.out.print(input[i]);
            }
        System.out.println();
    }
}

Recursion: Replace ma above with a new ma and ma2:

    public static void ma(int[] input) {
        ma2(input, new Stack<Boolean>());
    }

    public static void ma2(int[] input, Stack<Boolean> stack) {
        if (!stack.isEmpty() && stack.peek())
            print(stack, input);
        if (stack.size() < input.length) {
            stack.push(true);
            ma2(input, stack);
            stack.pop();
            stack.push(false);
            ma2(input, stack);
            stack.pop();
        }
    }
johnchen902
  • 9,531
  • 1
  • 27
  • 69
  • I apply this code into my work that pretty work in minimum input but for 40 input for example 1,2,3,..40 That have out of memory problem. – user2545226 Jul 31 '13 at 21:21
  • @user2545226 Were you trying to hold all of the output in the memory? If you were, I'm afraid that OOME cannot be avoided. – johnchen902 Aug 01 '13 at 01:57
0

The approach is called recursion. For example this m,ight help: generating-all-permutations-of-a-given-string as an example

Community
  • 1
  • 1
Tala
  • 8,888
  • 5
  • 34
  • 38
0

Recursion is what you are looking for.

Recursion has been explained many times in many places, here is one of them: Understanding recursion

Community
  • 1
  • 1
Aurand
  • 5,487
  • 1
  • 25
  • 35