This is a java program to find permutation of N numbers using Heap’s Algorithm. Heap’s algorithm is an algorithm used for generating all possible permutations of some given length.
Here is the source code of the Java Program to Implement Heap’s Algorithm for Permutation of N Numbers. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
package com.sanfoundry.combinatorial; import java.util.Arrays; import java.util.Scanner; public class HeapsPermutation { private static void swap(int[] v, int i, int j) { int t = v[i]; v[i] = v[j]; v[j] = t; } public void permute(int[] v, int n) { if (n == 1) { System.out.println(Arrays.toString(v)); } else { for (int i = 0; i < n; i++) { permute(v, n - 1); if (n % 2 == 1) { swap(v, 0, n - 1); } else { swap(v, i, n - 1); } } } } public static void main(String[] args) { System.out.println("Enter the number of elements in a sequence: "); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); System.out.println("Enter the sequence: "); int sequence[] = new int[n]; for (int i = 0; i < n; i++) { sequence[i] = sc.nextInt(); } new HeapsPermutation().permute(sequence, n); sc.close(); } }
Output:
$ javac HeapsPermutation.java $ java HeapsPermutation Enter the number of elements in a sequence: 5 Enter the sequence: 2 4 7 3 8 [2, 4, 7, 3, 8] [4, 2, 7, 3, 8] [7, 2, 4, 3, 8] [2, 7, 4, 3, 8] [4, 7, 2, 3, 8] [7, 4, 2, 3, 8] [3, 4, 7, 2, 8] [4, 3, 7, 2, 8] [7, 3, 4, 2, 8] [3, 7, 4, 2, 8] [4, 7, 3, 2, 8] [7, 4, 3, 2, 8] [3, 2, 7, 4, 8] [2, 3, 7, 4, 8] [7, 3, 2, 4, 8] [3, 7, 2, 4, 8] [2, 7, 3, 4, 8] [7, 2, 3, 4, 8] [3, 2, 4, 7, 8] [2, 3, 4, 7, 8] [4, 3, 2, 7, 8] [3, 4, 2, 7, 8] [2, 4, 3, 7, 8] [4, 2, 3, 7, 8] [8, 2, 4, 7, 3] [2, 8, 4, 7, 3] [4, 8, 2, 7, 3] [8, 4, 2, 7, 3] [2, 4, 8, 7, 3] [4, 2, 8, 7, 3] [7, 2, 4, 8, 3] [2, 7, 4, 8, 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, 3, 4, 8, 2] [3, 7, 4, 8, 2] [8, 7, 3, 4, 2] [7, 8, 3, 4, 2] [3, 8, 7, 4, 2] [8, 3, 7, 4, 2] [7, 3, 8, 4, 2] [3, 7, 8, 4, 2] [8, 4, 3, 7, 2] [4, 8, 3, 7, 2] [3, 8, 4, 7, 2] [8, 3, 4, 7, 2] [4, 3, 8, 7, 2] [3, 4, 8, 7, 2] [8, 4, 7, 3, 2] [4, 8, 7, 3, 2] [7, 8, 4, 3, 2] [8, 7, 4, 3, 2] [4, 7, 8, 3, 2] [7, 4, 8, 3, 2]
Related posts:
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Java Program to Perform Arithmetic Operations on Numbers of Size
Entity To DTO Conversion for a Spring REST API
Java Program to Perform Complex Number Multiplication
Java Program to Find the Mode in a Data Set
Number Formatting in Java
A Guide to Iterator in Java
Java Program to Implement Doubly Linked List
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Running Spring Boot Applications With Minikube
Handle EML file with JavaMail
An Intro to Spring Cloud Zookeeper
Introduction to Using FreeMarker in Spring MVC
Guide to Selenium with JUnit / TestNG
Java Program to Implement Hash Tables with Double Hashing
Overview of Spring Boot Dev Tools
Cài đặt và sử dụng Swagger UI
Converting String to Stream of chars
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Java Program to Optimize Wire Length in Electrical Circuit
Quick Guide to @RestClientTest in Spring Boot
Java Program to Compute DFT Coefficients Directly
Java Program to Implement Variable length array
Registration – Activate a New Account by Email
Lập trình đa luồng trong Java (Java Multi-threading)
Guide to java.util.Formatter
Java Program for Douglas-Peucker Algorithm Implementation
New Features in Java 14
Java Program to Implement Pollard Rho Algorithm
Optional trong Java 8
Java Program to Construct an Expression Tree for an Infix Expression