Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

permutation array java

public class Permute{
    static void permute(java.util.List<Integer> arr, int k){
        for(int i = k; i < arr.size(); i++){
            java.util.Collections.swap(arr, i, k);
            permute(arr, k+1);
            java.util.Collections.swap(arr, k, i);
        }
        if (k == arr.size() -1){
            System.out.println(java.util.Arrays.toString(arr.toArray()));
        }
    }
    public static void main(String[] args){
        Permute.permute(java.util.Arrays.asList(3,4,6,2,1), 0);
    }
}
Comment

permutation array java


int a[] = {3,4,6,2,1};
int size = sizeof(a)/sizeof(a[0]);
std::sort(a, a+size);
do {
  // print a's elements
} while(std::next_permutation(a, a+size));

Comment

Permutations of array in java

 
package org.arpit.java2blog;
 
import java.util.ArrayList;
import java.util.List;
 
public class PermutateArray {
 
    public static void main(String[] args) {
        PermutateArray pa=new PermutateArray();
 
        int[] arr= {10, 20, 30};
 
        List<List<Integer>> permute = pa.permute(arr);
 
        System.out.println("Permuations of array : [10, 20, 30] are:");
        System.out.println("=========================================");
        for(List<Integer> perm:permute)
        {
            System.out.println(perm);
        }
 
    }
    public List<List<Integer>> permute(int[] arr) {
        List<List<Integer>> list = new ArrayList<>();
        permuteHelper(list, new ArrayList<>(), arr);
        return list;
    }
 
    private void permuteHelper(List<List<Integer>> list, List<Integer> resultList, int [] arr){
 
        // Base case
        if(resultList.size() == arr.length){
            list.add(new ArrayList<>(resultList));
        }
        else{
            for(int i = 0; i < arr.length; i++){
 
                if(resultList.contains(arr[i]))
                {
                    // If element already exists in the list then skip
                    continue;
                }
                // Choose element
                resultList.add(arr[i]);
                // Explore
                permuteHelper(list, resultList, arr);
                // Unchoose element
                resultList.remove(resultList.size() - 1);
            }
        }
    }
 
}
 
Comment

PREVIOUS NEXT
Code Example
Java :: Java Access LinkedList elements 
Java :: save file to disk java 
Java :: java to python 
Java :: Java How to use NavigableMap? 
Java :: int to integer array in java 
Java :: array 2 
Java :: @Async how it works @EnableAsync 
Java :: Main method not found in class Ponga, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application 
Java :: madrid 
Java :: java disable stack trace 
Java :: create variables in java 
Java :: how to find poiwer in java 
Java :: set the content of a Jlist from an other Jlist (Swing) 
Java :: java default access modifier 
Java :: input method manager hide keyboard 
Java :: how to make bidirectional fromated binding 
Java :: Calculator repeat 
Java :: taking user input in array in java using constructor 
Java :: calculate tip and sales tax function 
Java :: i have use tried catch but still prints exception java 
Java :: how to cut a certion part from a string in java 
Java :: Iterating & Adding to a Stack 
Java :: HOW TO CODE WORKING PLUGIN IN MINECRAFT 
Java :: jsp multipart/form-data submition 
Java :: how to get data from array list to set java 
Java :: java scanner equation 
Java :: Scanner library showing element not found exception 
Java :: how to make a java lex analyzer 
Java :: java load config file 
Java :: manifest merger 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =