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:
Guide to ThreadLocalRandom in Java
Hướng dẫn Java Design Pattern – Adapter
Format ZonedDateTime to String
Java Program to Generate Random Hexadecimal Byte
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
JUnit 5 for Kotlin Developers
New Features in Java 13
Lập trình hướng đối tượng (OOPs) trong java
Merging Two Maps with Java 8
Guide to WeakHashMap in Java
Spring Boot with Multiple SQL Import Files
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Java Program to Implement LinkedHashMap API
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Convert a Map to an Array, List or Set in Java
Giới thiệu thư viện Apache Commons Chain
Performance Difference Between save() and saveAll() in Spring Data
Easy Ways to Write a Java InputStream to an OutputStream
Spring Security Authentication Provider
Quick Guide to java.lang.System
Apache Commons Collections BidiMap
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
An Intro to Spring Cloud Contract
Guide to Java 8 groupingBy Collector
Collect a Java Stream to an Immutable Collection
Java Program to Solve any Linear Equations
Hướng dẫn Java Design Pattern – Abstract Factory
Adding a Newline Character to a String in Java
Spring Boot - Batch Service
Hướng dẫn Java Design Pattern – DAO
Send email with authentication