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:
Explain about URL and HTTPS protocol
Introduction to the Java NIO2 File API
Java Program to Implement the String Search Algorithm for Short Text Sizes
Java TreeMap vs HashMap
A Guide to Spring Boot Admin
Java Program to Describe the Representation of Graph using Adjacency List
Concurrent Test Execution in Spring 5
Configure a Spring Boot Web Application
Java Program to Check whether Graph is a Bipartite using BFS
Collect a Java Stream to an Immutable Collection
Zipping Collections in Java
Class Loaders in Java
String Processing with Apache Commons Lang 3
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Spring Security – Reset Your Password
Java Program to find the peak element of an array using Binary Search approach
Java Program to Construct an Expression Tree for an Prefix Expression
Java IO vs NIO
Java Program to Perform the Shaker Sort
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Introduction to Java 8 Streams
Java Program to Implement the Program Used in grep/egrep/fgrep
Java Program to Implement Ternary Search Tree
Hướng dẫn Java Design Pattern – Intercepting Filter
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Java Program to Perform LU Decomposition of any Matrix
Performance Difference Between save() and saveAll() in Spring Data
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Java Program to Implement RoleUnresolvedList API
Marker Interface trong Java
Spring WebFlux Filters