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:
Hashing a Password in Java
Serialization và Deserialization trong java
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Java Program to Implement Regular Falsi Algorithm
Life Cycle of a Thread in Java
Java Program to Implement Maximum Length Chain of Pairs
Versioning a REST API
MyBatis with Spring
Java Program to Implement Triply Linked List
A Guide to Apache Commons Collections CollectionUtils
Spring Boot - CORS Support
Automatic Property Expansion with Spring Boot
Toán tử instanceof trong java
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Java Stream Filter with Lambda Expression
Using a Spring Cloud App Starter
Spring Security with Maven
Java Program to Implement Levenshtein Distance Computing Algorithm
Using Optional with Jackson
Getting the Size of an Iterable in Java
Database Migrations with Flyway
“Stream has already been operated upon or closed” Exception in Java
Hướng dẫn sử dụng String Format trong Java
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
How to Define a Spring Boot Filter?
Extract links from an HTML page
Injecting Prototype Beans into a Singleton Instance in Spring
Biến trong java
Error Handling for REST with Spring
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Lớp lồng nhau trong java (Java inner class)
Tạo chương trình Java đầu tiên sử dụng Eclipse