Posts

Showing posts from December, 2017

Steinhaus–Johnson–Trotter algorithm

package permutation; import java.util.HashSet; import java.util.Scanner; public class JohnsonTrotter { static HashSet<String> permutations = new HashSet<>(); public static void main(String args []) { // Incorrect(); System. out .println( "Enter the count of integers whose all " + "possible orders will be permuted using Johnson and Trotter" ); Scanner scanner = new Scanner(System. in ); int N = scanner .nextInt(); scanner .close(); int arr [] = new int [ N ]; for ( int i = 1; i <= N ; i ++) { arr [ i - 1] = i ; } boolean [] direction = new boolean [ N ]; // I am using a boolean array of the same size as the number of // integers. A true indicates right, and false indicates left. Don't // judge me for choosing right as true, I am right handed :P. There is // another reason why I chose right as true, by default a new boolean // array in Java