-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextPermCombMain.java
More file actions
38 lines (31 loc) · 601 Bytes
/
NextPermCombMain.java
File metadata and controls
38 lines (31 loc) · 601 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
28
29
30
31
32
33
34
35
36
37
38
package algorithm;
import java.util.*;
public class NextPermCombMain {
static int N = 4, R = 3, C = 0;
static int[] a = { 1, 2, 3, 4 };
static boolean np() {
int i = N - 1;
while (i > 0 && a[i - 1] >= a[i])
i--;
if (i == 0)
return false;
int j = N - 1;
while (a[i - 1] >= a[j])
j--;
swap(i - 1, j);
int k = N - 1;
while (i < k)
swap(i++, k--);
return true;
}
static void swap(int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void main(String[] args) {
do {
System.out.println(Arrays.toString(a));
} while (np());
}
}