Java Program to Implement Heap’s Algorithm for Generating Permutations

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.

  1.  
  2. package com.sanfoundry.combinatorial;
  3.  
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6.  
  7. public class HeapsPermutation
  8. {
  9.     private static void swap(int[] v, int i, int j)
  10.     {
  11.         int t = v[i];
  12.         v[i] = v[j];
  13.         v[j] = t;
  14.     }
  15.  
  16.     public void permute(int[] v, int n)
  17.     {
  18.         if (n == 1)
  19.         {
  20.             System.out.println(Arrays.toString(v));
  21.         }
  22.         else
  23.         {
  24.             for (int i = 0; i < n; i++)
  25.             {
  26.                 permute(v, n - 1);
  27.                 if (n % 2 == 1)
  28.                 {
  29.                     swap(v, 0, n - 1);
  30.                 }
  31.                 else
  32.                 {
  33.                     swap(v, i, n - 1);
  34.                 }
  35.             }
  36.         }
  37.     }
  38.  
  39.     public static void main(String[] args)
  40.     {
  41.         System.out.println("Enter the number of elements in a sequence: ");
  42.         Scanner sc = new Scanner(System.in);
  43.         int n = sc.nextInt();
  44.         System.out.println("Enter the sequence: ");
  45.         int sequence[] = new int[n];
  46.         for (int i = 0; i < n; i++)
  47.         {
  48.             sequence[i] = sc.nextInt();
  49.         }
  50.         new HeapsPermutation().permute(sequence, n);
  51.         sc.close();
  52.     }
  53. }

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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.