Java Program to Implement Alexander Bogomolny’s UnOrdered Permutation Algorithm

This is a java program to implement Alexander Bogomolny’s permutation algorithm. This version of program computes all possible permutations of numbers from 1 to N using Alexander Bogomolyn’s algorithm.

Here is the source code of the Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements From 1 to N. 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.Scanner;
  5.  
  6. public class AlexanderBogomolnyPermutation
  7. {
  8.     static int level = -1;
  9.  
  10.     public static void print(int[] value, int n)
  11.     {
  12.         if (value.length != 0)
  13.         {
  14.             for (int i = 0; i < value.length; i++)
  15.             {
  16.                 System.out.print(value[i] + " ");
  17.             }
  18.             System.out.println();
  19.         }
  20.     }
  21.  
  22.     public static void visit(int[] Value, int N, int k)
  23.     {
  24.         level = level + 1;
  25.         Value[k] = level;
  26.         if (level == N)
  27.             print(Value, N);
  28.         else
  29.             for (int i = 0; i < N; i++)
  30.                 if (Value[i] == 0)
  31.                     visit(Value, N, i);
  32.         level = level - 1;
  33.         Value[k] = 0;
  34.     }
  35.  
  36.     public static void main(String[] args)
  37.     {
  38.         Scanner sc = new Scanner(System.in);
  39.         System.out.println("Enter the size of the sequence:");
  40.         int n = sc.nextInt();
  41.         int sequence[] = new int[n];
  42.         for (int i = 0; i < n; i++)
  43.         {
  44.             sequence[i] = 0;
  45.         }
  46.         System.out.println("The permutations are: ");
  47.         visit(sequence, n, 0);
  48.         sc.close();
  49.     }
  50. }

Output:

$ javac AlexanderBogomolnyPermutation.java
$ java AlexanderBogomolnyPermutation
 
Enter the size of the sequence:
4
The permutations are: 
1 2 3 4 
1 2 4 3 
1 3 2 4 
1 4 2 3 
1 3 4 2 
1 4 3 2 
2 1 3 4 
2 1 4 3 
3 1 2 4 
4 1 2 3 
3 1 4 2 
4 1 3 2 
2 3 1 4 
2 4 1 3 
3 2 1 4 
4 2 1 3 
3 4 1 2 
4 3 1 2 
2 3 4 1 
2 4 3 1 
3 2 4 1 
4 2 3 1 
3 4 2 1 
4 3 2 1

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

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

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest 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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.