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.
package com.sanfoundry.combinatorial;
import java.util.Scanner;
public class AlexanderBogomolnyPermutation
{
static int level = -1;
public static void print(int[] value, int n)
{
if (value.length != 0)
{
for (int i = 0; i < value.length; i++)
{
System.out.print(value[i] + " ");
}
System.out.println();
}
}
public static void visit(int[] Value, int N, int k)
{
level = level + 1;
Value[k] = level;
if (level == N)
print(Value, N);
else
for (int i = 0; i < N; i++)
if (Value[i] == 0)
visit(Value, N, i);
level = level - 1;
Value[k] = 0;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the sequence:");
int n = sc.nextInt();
int sequence[] = new int[n];
for (int i = 0; i < n; i++)
{
sequence[i] = 0;
}
System.out.println("The permutations are: ");
visit(sequence, n, 0);
sc.close();
}
}
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
Related posts:
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Java – Get Random Item/Element From a List
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Java Program to Implement Treap
JUnit5 Programmatic Extension Registration with @RegisterExtension
Java 8 and Infinite Streams
Java Program to Decode a Message Encoded Using Playfair Cipher
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Java Program to Check Cycle in a Graph using Topological Sort
Java Program to Encode a Message Using Playfair Cipher
Practical Java Examples of the Big O Notation
Java Program to Show the Duality Transformation of Line and Point
Spring Boot - Actuator
Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays
Java Program to Implement Control Table
Java Program to Implement Sparse Matrix
Java Program to Describe the Representation of Graph using Incidence List
Java Program to Generate Random Numbers Using Multiply with Carry Method
How to Return 404 with Spring WebFlux
Converting String to Stream of chars
Java Program to Implement AttributeList API
A Guide to the ResourceBundle
Guide to the ConcurrentSkipListMap
Java Program to Compute DFT Coefficients Directly
Java Program to Implement Stack using Two Queues
Send email with authentication
Java 8 – Powerful Comparison with Lambdas
Bootstrapping Hibernate 5 with Spring
Read an Outlook MSG file
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Java Program to Implement Naor-Reingold Pseudo Random Function
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph