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:
Converting Between a List and a Set in Java
Java Program to Implement Shell Sort
Simple Single Sign-On with Spring Security OAuth2
Spring 5 WebClient
ETags for REST with Spring
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Spring Boot - Unit Test Cases
RestTemplate Post Request with JSON
Java Program to Implement Self Balancing Binary Search Tree
Java Stream Filter with Lambda Expression
Hướng dẫn Java Design Pattern – Service Locator
Deploy a Spring Boot WAR into a Tomcat Server
Spring Boot - Sending Email
Java Program to Implement Disjoint Sets
Hướng dẫn Java Design Pattern – Null Object
Lập trình đa luồng với Callable và Future trong Java
Java Program to Find All Pairs Shortest Path
Giới thiệu Aspect Oriented Programming (AOP)
Java 8 Stream findFirst() vs. findAny()
Hướng dẫn Java Design Pattern – Interpreter
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
String Processing with Apache Commons Lang 3
Interface trong Java 8 – Default method và Static method
Hướng dẫn sử dụng String Format trong Java
Introduction to Spring Method Security
Java Program to Find the Minimum value of Binary Search Tree
Java Program to Implement Cubic convergence 1/pi Algorithm
Java 14 Record Keyword
Java Program to find the peak element of an array using Binary Search approach
New Features in Java 15
Removing all Nulls from a List in Java
Java – Reader to InputStream