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:
Simple Single Sign-On with Spring Security OAuth2
Batch Processing with Spring Cloud Data Flow
Dockerizing a Spring Boot Application
Java Program to Implement the Checksum Method for Small String Messages and Detect
Wiring in Spring: @Autowired, @Resource and @Inject
Guide to Java 8 groupingBy Collector
A Guide to JUnit 5 Extensions
Converting Between an Array and a Set in Java
Giới thiệu Java 8
Java Program to Generate N Number of Passwords of Length M Each
Java Program to Implement Kosaraju Algorithm
Tạo chương trình Java đầu tiên sử dụng Eclipse
Circular Dependencies in Spring
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
String Joiner trong Java 8
Chương trình Java đầu tiên
Spring @RequestParam Annotation
Mệnh đề Switch-case trong java
Java Program to Perform Deletion in a BST
How to Get a Name of a Method Being Executed?
Java Program to Implement Borwein Algorithm
Spring Data JPA @Modifying Annotation
HttpClient Connection Management
Java Program to Implement Brent Cycle Algorithm
Java Program to Perform Search in a BST
Java Program to Perform Arithmetic Operations on Numbers of Size
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Java String Conversions
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Spring Boot - Admin Client
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Java – Write a Reader to File