Created
August 21, 2014 22:09
-
-
Save lamida/d82c367d3cc39bd2ad7d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Permut { | |
public void print(int[] ar){ | |
for(int a : ar){ | |
System.out.print(a + " "); | |
} | |
System.out.println(); | |
} | |
public void swap(int[] ar, int a, int b){ | |
int temp = ar[b]; | |
ar[b] = ar[a]; | |
ar[a] = temp; | |
} | |
public void f(int [] ar, int i) | |
{ | |
int j = i; | |
for (j = i; j < ar.length; j++) { | |
swap(ar, i, j); | |
f(ar, i+1); | |
swap(ar, i, j); | |
} | |
if (ar.length-1 == i) { | |
print(ar); | |
return; | |
} | |
} | |
void permute(int[] arr, int k){ | |
for(int i = k; i < arr.length; i++){ | |
swap(arr, i, k); | |
permute(arr, k+1); | |
swap(arr, k, i); | |
} | |
if (k == arr.length -1){ | |
System.out.println(java.util.Arrays.toString(arr)); | |
} | |
} | |
public static void main(String[] args) { | |
Permut p = new Permut(); | |
int[] ar = {1,2,3}; | |
//p.f(ar, 0, ar.length-1); | |
p.f(ar,0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment