-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermut.java
More file actions
27 lines (27 loc) · 717 Bytes
/
permut.java
File metadata and controls
27 lines (27 loc) · 717 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class permut{
public static void foo(int arr[], int l, int r){
if (l == r)
printArr(arr);
else {
for (int i = l; i <= r; i++) {
arr = swap(arr, l, i);
foo(arr, l + 1, r);
arr = swap(arr, l, i);
}
}
}
public static int[] swap(int[] a, int i, int j){
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
return a;
}
public static void printArr(int arr[]){
for(int i =0 ;i<arr.length; i++) System.out.print(arr[i] + ", ");
System.out.println();
}
public static void main(){
int[] a = {1,2,3,4};
foo(a, 0, a.length-1);
}
}