Advertisement
coregame

Java Perm

Aug 11th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1. package javaapplication3;
  2.  
  3. public class Permutation {
  4.     public static int count = 0;
  5.  
  6.     public static void permute(char[] a, int l, int r){
  7.         int i;
  8.         if (l == r) {
  9.             count += 1;
  10.             System.out.println(a);
  11.         }
  12.         else {
  13.             for (i = l; i <= r; i++){
  14.                 char temp = a[l];
  15.                 a[l] = a[i];
  16.                 a[i] = temp;
  17.  
  18.                 permute(a, l+1, r);
  19.                
  20.                 char temp2 = a[l];
  21.                 a[l] = a[i];
  22.                 a[i] = temp2;
  23.             }
  24.         }
  25.     }
  26.  
  27.     public static void main(String[] args){
  28.         char[] str = {'c', 'a', 't', 'd', 'o', 'g'};
  29.         int n = str.length;
  30.         permute(str, 0, n-1);
  31.         System.out.println("\nCount: " + count);
  32.     }
  33.    
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement