Skip to content

Commit 8d518f5

Browse files
committed
optimize swapBlocks and scramble
1 parent 17c5b3f commit 8d518f5

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/main/java/org/cicirello/permutations/Permutation.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,12 @@ public void scramble(int i, int j, RandomGenerator r) {
539539
if (i == j) {
540540
return;
541541
}
542-
int k = j;
542+
int k;
543543
if (i > j) {
544544
k = i;
545545
i = j;
546+
} else {
547+
k = j;
546548
}
547549
boolean changed = false;
548550
for (; k > i + 1; k--) {
@@ -745,12 +747,12 @@ public void swapBlocks(int a, int b, int i, int j) {
745747
// blocks are adjacent
746748
removeAndInsert(i, j - i + 1, a);
747749
} else {
748-
int[] temp = new int[j - a + 1];
750+
int[] temp = new int[j - b];
749751
int k = j - i + 1;
750752
System.arraycopy(permutation, i, temp, 0, k);
751753
int m = i - b - 1;
752754
System.arraycopy(permutation, b + 1, temp, k, m);
753-
System.arraycopy(permutation, a, temp, k + m, b - a + 1);
755+
System.arraycopy(permutation, a, permutation, a + temp.length, b - a + 1);
754756
System.arraycopy(temp, 0, permutation, a, temp.length);
755757
hashCodeIsCached = false;
756758
}

0 commit comments

Comments
 (0)