Integer permutation using Heap's algorithm:
import java.util.Arrays;
public class IntegerPermutation {
private static void swap(Integer[] integerArray, int i, int j) {
Integer temp = integerArray[i];
integerArray[i] = integerArray[j];
integerArray[j] = temp;
}
private static void permutationHelper(Integer[] integerArray,
int currentPosition) {
if (currentPosition == 1) {
System.out.println(Arrays.toString(integerArray));
} else {
for (int i = 0; i < currentPosition; i++) {
permutation(integerArray, currentPosition - 1);
if (currentPosition % 2 == 0) {
swap(integerArray, i, currentPosition - 1);
} else {
swap(integerArray, 0, currentPosition - 1);
}
}
}
}
public static void permutation(Integer[] integerArray, int lengthOfTheArray) {
permutationHelper(integerArray, lengthOfTheArray);
}
public static void main(String[] args) {
Integer[] a = new Integer[] { 1, 2, 3 };
IntegerPermutation.permutation(a, a.length);
}
}
Can you please critique my code and provide your thoughts on where I should improve my code?
2 Answers 2
permutation
-> permutationHelper
-> permutation
-> ...
As @MartinR pointed out,
the permutation
and permutationHelper
functions calling each other are confusing.
In fact, permutation
is actually pointless,
as it does nothing but call permutationHelper
.
To give permutation
some purpose,
you could do this:
- Make
permutationHelper
call itself instead ofpermutation
- Drop the array length parameter from
permutation
: it can derive it from the array it receives, and pass that topermutationHelper
Why Integer[]
instead of int[]
?
In the posted program, there's really no need for Integer
objects.
You can work with int[]
, which is slightly lighter.
Code duplication
There are several duplicated elements in this code:
permutation(integerArray, currentPosition - 1); if (currentPosition % 2 == 0) { swap(integerArray, i, currentPosition - 1); } else { swap(integerArray, 0, currentPosition - 1); }
First of all, the currentPosition - 1
is duplicated.
It would be good to store it in a local variable.
Secondly, swap
is called with almost the same parameters, except one.
This could be expressed more compactly using a ternary operator.
int nextPosition = currentPosition - 1;
permutation(integerArray, nextPosition);
swap(integerArray, currentPosition % 2 == 0 ? i : 0, nextPosition);
Initializing arrays
A simpler way to initialize arrays is using { ... }
, like this:
int[] a = { 1, 2, 3, 4 };
-
\$\begingroup\$ I am not a Java expert, but isn't
Integer[] a = new Integer[] { 1, 2, 3 };
in the original code already the type of initialization which you are suggesting in your last point? \$\endgroup\$Martin R– Martin R2015年07月25日 20:47:24 +00:00Commented Jul 25, 2015 at 20:47 -
\$\begingroup\$ I forgot to edit after a copy-paste. Fixed now, thanks! \$\endgroup\$janos– janos2015年07月25日 20:50:38 +00:00Commented Jul 25, 2015 at 20:50
-
\$\begingroup\$ "there's really no need for Integer", is it? \$\endgroup\$Gerold Broser– Gerold Broser2015年07月27日 13:07:07 +00:00Commented Jul 27, 2015 at 13:07
One thing I noticed is that permutation()
calls permutationHelper()
with exactly the same arguments. So you don't need two separate methods
but just
public static void permutation(Integer[] integerArray,
int currentPosition) { ... }
which calls itself recursively.