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:
Show Hibernate/JPA SQL Statements from Spring Boot
Apache Commons Collections SetUtils
Guide to UUID in Java
Introduction to Spring Cloud Netflix – Eureka
Getting Started with Custom Deserialization in Jackson
wait() and notify() Methods in Java
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Java – File to Reader
The “final” Keyword in Java
The Registration Process With Spring Security
Java Program to Implement the Checksum Method for Small String Messages and Detect
Creating a Custom Starter with Spring Boot
Java CyclicBarrier vs CountDownLatch
Java Program to Implement CopyOnWriteArrayList API
Zipping Collections in Java
Hướng dẫn sử dụng lớp Console trong java
Hướng dẫn Java Design Pattern – Strategy
Biến trong java
Generate Spring Boot REST Client with Swagger
Java Program to Implement Heap
Spring Boot - Google Cloud Platform
Java Program to Perform Quick Sort on Large Number of Elements
Spring Boot: Customize Whitelabel Error Page
Netflix Archaius with Various Database Configurations
Java Program to Describe the Representation of Graph using Incidence List
Java Program to Check whether Undirected Graph is Connected using DFS
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
HttpClient with SSL
Service Registration with Eureka
Java Program to Implement Circular Doubly Linked List
Guide to Apache Commons CircularFifoQueue
TreeSet và sử dụng Comparable, Comparator trong java