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:
How to Manually Authenticate User with Spring Security
How to Return 404 with Spring WebFlux
Converting String to Stream of chars
Java Program to Implement Iterative Deepening
Java Program to Implement Hash Tables Chaining with List Heads
Interface trong Java 8 – Default method và Static method
Batch Processing with Spring Cloud Data Flow
Java Program to Implement HashTable API
Tìm hiểu về Web Service
Java Program to Perform the Shaker Sort
Java Program to Check Cycle in a Graph using Topological Sort
Custom Thread Pools In Java 8 Parallel Streams
Notify User of Login From New Device or Location
How to Count Duplicate Elements in Arraylist
New Features in Java 9
Spring Security Login Page with React
Java Program to Implement Floyd Cycle Algorithm
Java Program to Perform Naive String Matching
Cơ chế Upcasting và Downcasting trong java
Java Program to Implement Hash Trie
Jackson Date
Guava CharMatcher
Prevent Cross-Site Scripting (XSS) in a Spring Application
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Using the Not Operator in If Conditions in Java
Java Program to Implement Levenshtein Distance Computing Algorithm
Spring Boot - Twilio
Java Program to Implement Maximum Length Chain of Pairs
A Guide to WatchService in Java NIO2
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Java Program to Solve Knapsack Problem Using Dynamic Programming
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph