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:
Hướng dẫn Java Design Pattern – Factory Method
Các kiểu dữ liệu trong java
Java Program to Implement Min Heap
A Guide to the ViewResolver in Spring MVC
Java Program to Implement Brent Cycle Algorithm
Changing Annotation Parameters At Runtime
Java Program to Check Cycle in a Graph using Graph traversal
Quick Guide to java.lang.System
Java Program to implement Bit Set
Java Program to Implement the Vigenere Cypher
Testing in Spring Boot
Removing all duplicates from a List in Java
Concrete Class in Java
Immutable ArrayList in Java
Using JWT with Spring Security OAuth
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
A Guide to WatchService in Java NIO2
HttpClient 4 – Follow Redirects for POST
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Using a Spring Cloud App Starter
Java Program to implement Priority Queue
Most commonly used String methods in Java
Spring Boot - Scheduling
JUnit 5 @Test Annotation
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
The Java 8 Stream API Tutorial
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Java Program to Perform Polygon Containment Test
Wiring in Spring: @Autowired, @Resource and @Inject
Convert Time to Milliseconds in Java
Java Program to Implement String Matching Using Vectors