Repository navigation
Expand file tree
/
Copy pathPermutationsLex.java
More file actions
163 lines (129 loc) · 3.71 KB
/
Copy pathPermutationsLex.java
File metadata and controls
163 lines (129 loc) · 3.71 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*package string.codes;
*//******************************************************************************
* Compilation: javac PermutationsLex.java
* Execution: java PermutationsLex N
*
* Generate all N! permutations of N elements in lexicographic order.
*
* This program is a Java version based on the program Permlex.c
* writen by Frank Ruskey and Joe Sawada.
*
* http://theory.cs.uvic.ca/inf/perm/PermInfo.html
*
* % java PermutationsLex 3
* 012
* 021
* 102
* 120
* 201
* 210
*
******************************************************************************//*
public class PermutationsLex {
public static void show(int[] a) {
for (int i = 0; i < a.length; i++)
System.out.printf("%d", a[i]);
System.out.printf("\n");
}
public static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static boolean hasNext(int[] a) {
int N = a.length;
// find rightmost element a[k] that is smaller than element to its right
int k;
for (k = N-2; k >= 0; k--)
if (a[k] < a[k+1]) break;
if (k == -1) return false;
// find rightmost element a[j] that is larger than a[k]
int j = N-1;
while (a[k] > a[j])
j--;
swap(a, j, k);
for (int r = N-1, s = k+1; r > s; r--, s++)
swap(a, r, s);
return true;
}
public static void perm(int N) {
// initialize permutation
int[] a = new int[N];
for (int i = 0; i < N; i++)
a[i] = i;
// print permutations
show(a);
while (hasNext(a))
show(a);
}
public static void main(String[] args) {
int N = 9;//Integer.parseInt(args[0]);
perm(N);
}
}*/
package string.codes;
import java.util.Arrays;
/******************************************************************************
* Compilation: javac PermutationsLex.java
* Execution: java PermutationsLex N
*
* Generate all N! permutations of N elements in lexicographic order.
*
* This program is a Java version based on the program Permlex.c
* writen by Frank Ruskey and Joe Sawada.
*
* http://theory.cs.uvic.ca/inf/perm/PermInfo.html
*
* % java PermutationsLex 3
* 012
* 021
* 102
* 120
* 201
* 210
*
******************************************************************************/
public class PermutationsLex {
public static void show(char[] a) {
for (int i = 0; i < a.length; i++)
System.out.print(a[i]);
System.out.println(" ");
}
public static void swap(char[] a, int i, int j) {
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static boolean hasNext(char[] a) {
int N = a.length;
// find rightmost element a[k] that is smaller than element to its right
int k;
for (k = N-2; k >= 0; k--)
if (a[k] < a[k+1]) break;
if (k == -1) return false;
// find rightmost element a[j] that is larger than a[k]
int j = N-1;
while (a[k] > a[j])
j--;
swap(a, j, k);
for (int r = N-1, s = k+1; r > s; r--, s++)
swap(a, r, s);
return true;
}
public static void perm(char[] N) {
// initialize permutation
char[] a = new char[N.length];
for (int i = 0; i < N.length; i++)
a[i] = N[i];
// print permutations
show(a);
while (hasNext(a))
show(a);
}
public static void main(String[] args) {
String s = "string";
char[] arr = s.toCharArray();
Arrays.sort(arr);
perm(arr);
}
}