Java Program to Implement Heap’s Algorithm for Permutation of N Numbers

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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]
[8, 2, 7, 3, 4]
[2, 8, 7, 3, 4]
[7, 8, 2, 3, 4]
[8, 7, 2, 3, 4]
[2, 7, 3, 8, 4]
[7, 2, 3, 8, 4]
[3, 2, 7, 8, 4]
[2, 3, 7, 8, 4]
[7, 3, 2, 8, 4]
[3, 7, 2, 8, 4]
[4, 7, 3, 8, 2]
[7, 4, 3, 8, 2]
[3, 4, 7, 8, 2]
[4, 3, 7, 8, 2]
[7, 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:

Notify User of Login From New Device or Location
Lớp LinkedHashMap trong Java
Spring Security Custom AuthenticationFailureHandler
Java Program to Perform Insertion in a BST
Java Program to Find Nearest Neighbor Using Linear Search
Java Program to Perform integer Partition for a Specific Case
Spring REST with a Zuul Proxy
Server-Sent Events in Spring
Introduction to Java Serialization
Java Program to Implement Bubble Sort
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Java Program to Find Strongly Connected Components in Graphs
Mệnh đề if-else trong java
Custom Cascading in Spring Data MongoDB
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Java Program to Check the Connectivity of Graph Using DFS
Java Program to implement Bit Set
Convert String to int or Integer in Java
A Custom Media Type for a Spring REST API
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Java – Rename or Move a File
Spring Security Logout
Lập trình hướng đối tượng (OOPs) trong java
A Guide to the ViewResolver in Spring MVC
Encode a String to UTF-8 in Java
Java Program to Implement Gaussian Elimination Algorithm
Array to String Conversions
Java Program to Check whether Directed Graph is Connected using DFS