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:
Jackson JSON Views
Interface trong Java 8 – Default method và Static method
Mảng (Array) trong Java
Overflow and Underflow in Java
Control Structures in Java
Hướng dẫn sử dụng luồng vào ra ký tự trong Java
Java Program to find the maximum subarray sum using Binary Search approach
Life Cycle of a Thread in Java
Java Program to Implement HashSet API
Hướng dẫn Java Design Pattern – Builder
Check If a String Is Numeric in Java
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Spring Security with Maven
Introduction to Apache Commons Text
Date Time trong Java 8
An Intro to Spring Cloud Vault
Java Program to Describe the Representation of Graph using Adjacency Matrix
Quick Guide to Spring MVC with Velocity
Extract links from an HTML page
Java Program to Implement Strassen Algorithm
Examine the internal DNS cache
Difference Between Wait and Sleep in Java
Một số nguyên tắc, định luật trong lập trình
String Initialization in Java
Check If a File or Directory Exists in Java
DynamoDB in a Spring Boot Application Using Spring Data
Spring Boot - Tomcat Deployment
Java Program to Implement a Binary Search Tree using Linked Lists
Java Program to Implement D-ary-Heap
Java Program to Solve any Linear Equations
Injecting Prototype Beans into a Singleton Instance in Spring
Java Program to Implement Unrolled Linked List