Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements From 1 to N

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:

Java Program to Implement LinkedTransferQueue API
Removing all Nulls from a List in Java
Java Program to Check whether Undirected Graph is Connected using BFS
Java Program to Solve Tower of Hanoi Problem using Stacks
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
Java Program to Implement Control Table
Luồng Daemon (Daemon Thread) trong Java
Guide to the Java Queue Interface
An Intro to Spring Cloud Security
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Hướng dẫn Java Design Pattern – Singleton
Marker Interface trong Java
Remove HTML tags from a file to extract only the TEXT
A Guide to TreeSet in Java
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Sắp xếp trong Java 8
Comparing Dates in Java
Java Program to Represent Graph Using 2D Arrays
Quick Guide to Spring MVC with Velocity
Java Program to Implement Merge Sort Algorithm on Linked List
Java Program to Implement ArrayList API
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Java Program to Implement D-ary-Heap
Spring Boot Configuration with Jasypt
Java Program to Implement ConcurrentSkipListMap API
The Modulo Operator in Java
Java Program to Implement Fermat Factorization Algorithm
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Custom Thread Pools In Java 8 Parallel Streams
Java Program to Implement Gauss Seidel Method
Quick Guide on Loading Initial Data with Spring Boot