This is a java program to generate and print all the permutation of the Numbers. User first enters the element in the set and then actual elements. The notion of permutation relates to the act of permuting, or rearranging, members of a set into a particular sequence or order (unlike combinations, which are selections that disregard order). For example, there are six permutations of the set {1,2,3}, namely (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), and (3,2,1).
Here is the source code of the Java Program to Generate All Possible Combinations of a Given List of Numbers. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | //This is a java program to perform all permutation of given list of numbers of a specific length import java.util.Random; import java.util.Scanner; public class Permute_All_List_Numbers { static void permute( int [] a, int k) { if (k == a.length) { for ( int i = 0 ; i < a.length; i++) { System.out.print( " [" + a[i] + "] " ); } System.out.println(); } else { for ( int i = k; i < a.length; i++) { int temp = a[k]; a[k] = a[i]; a[i] = temp; permute(a, k + 1 ); temp = a[k]; a[k] = a[i]; a[i] = temp; } } } public static void main(String args[]) { Random random = new Random(); Scanner sc = new Scanner(System.in); System.out.println( "Enter the length of list: " ); int N = sc.nextInt(); int [] sequence = new int [N]; for ( int i = 0 ; i < N; i++) sequence[i] = Math.abs(random.nextInt( 100 )); System.out.println( "The original sequence is: " ); for ( int i = 0 ; i < N; i++) System.out.print(sequence[i] + " " ); System.out.println( "\nThe permuted sequences are: " ); permute(sequence, 0 ); sc.close(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | $ java Permute_All_List_Numbers.java $ java Permute_All_List_Numbers Enter the length of list: 3 The original sequence is: 15 61 16 The permuted sequences are: [ 15 ] [ 61 ] [ 16 ] [ 15 ] [ 16 ] [ 61 ] [ 61 ] [ 15 ] [ 16 ] [ 61 ] [ 16 ] [ 15 ] [ 16 ] [ 61 ] [ 15 ] [ 16 ] [ 15 ] [ 61 ] Enter the length of list: 4 The original sequence is: 50 98 4 61 The permuted sequences are: [ 50 ] [ 98 ] [ 4 ] [ 61 ] [ 50 ] [ 98 ] [ 61 ] [ 4 ] [ 50 ] [ 4 ] [ 98 ] [ 61 ] [ 50 ] [ 4 ] [ 61 ] [ 98 ] [ 50 ] [ 61 ] [ 4 ] [ 98 ] [ 50 ] [ 61 ] [ 98 ] [ 4 ] [ 98 ] [ 50 ] [ 4 ] [ 61 ] [ 98 ] [ 50 ] [ 61 ] [ 4 ] [ 98 ] [ 4 ] [ 50 ] [ 61 ] [ 98 ] [ 4 ] [ 61 ] [ 50 ] [ 98 ] [ 61 ] [ 4 ] [ 50 ] [ 98 ] [ 61 ] [ 50 ] [ 4 ] [ 4 ] [ 98 ] [ 50 ] [ 61 ] [ 4 ] [ 98 ] [ 61 ] [ 50 ] [ 4 ] [ 50 ] [ 98 ] [ 61 ] [ 4 ] [ 50 ] [ 61 ] [ 98 ] [ 4 ] [ 61 ] [ 50 ] [ 98 ] [ 4 ] [ 61 ] [ 98 ] [ 50 ] [ 61 ] [ 98 ] [ 4 ] [ 50 ] [ 61 ] [ 98 ] [ 50 ] [ 4 ] [ 61 ] [ 4 ] [ 98 ] [ 50 ] [ 61 ] [ 4 ] [ 50 ] [ 98 ] [ 61 ] [ 50 ] [ 4 ] [ 98 ] [ 61 ] [ 50 ] [ 98 ] [ 4 ] |
Related posts:
Guide to CopyOnWriteArrayList
Java Program to Perform Complex Number Multiplication
Custom Thread Pools In Java 8 Parallel Streams
Guide to the Synchronized Keyword in Java
Lớp Properties trong java
How to Get a Name of a Method Being Executed?
Model, ModelMap, and ModelAndView in Spring MVC
Jackson – Unmarshall to Collection/Array
A Guide to Java HashMap
Giới thiệu Google Guice – Binding
HttpClient Timeout
Java Program to Implement Knapsack Algorithm
The Modulo Operator in Java
Control Structures in Java
Java Program to Implement Direct Addressing Tables
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Java 8 Stream findFirst() vs. findAny()
Java Program to Permute All Letters of an Input String
An Intro to Spring Cloud Contract
Spring’s RequestBody and ResponseBody Annotations
Java Program to Implement ArrayList API
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Java Program to Implement Knight’s Tour Problem
Java Program to Solve a Matching Problem for a Given Specific Case
Spring Boot - Cloud Configuration Client
Hướng dẫn Java Design Pattern – Dependency Injection
Compare Two JSON Objects with Jackson
Java Program to Perform Left Rotation on a Binary Search Tree
Custom Exception trong Java
Introduction to Spring Data REST
Spring Security Authentication Provider
Remove the First Element from a List