Java List Permutate permute(List arr)

Here you can find the source of permute(List arr)

Description

permute

License

Open Source License

Declaration

public static <E> List<List<E>> permute(List<E> arr) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.Collections;
import java.util.List;

public class Main {
    public static <E> List<List<E>> permute(List<E> arr) {
        return permute(arr, 0);
    }//w w w  . j  a  v  a 2  s  .c  o  m

    public static <E> List<List<E>> permute(List<E> arr, int k) {
        List<List<E>> permutations = new ArrayList<>();
        for (int i = k; i < arr.size(); i++) {
            Collections.swap(arr, i, k);
            permutations.addAll(permute(arr, k + 1));
            Collections.swap(arr, k, i);
        }
        if (k == arr.size() - 1) {
            permutations.add(new ArrayList<>(arr));
        }
        return permutations;
    }
}

Related

  1. permutation(String prefix, String s, List list)
  2. permutations(List list)
  3. permutations(List listings)
  4. permutations(Map> parameterValues)
  5. permute(List s1, List s2)
  6. permute(List list1)
  7. permute(List list, int index, List> result)
  8. permute(T[] arr, List p)
  9. permutedGroups(List> elementLists)