Java Utililty Methods Integer Array Permutation

List of utility methods to do Integer Array Permutation

Description

The list of methods to do Integer Array Permutation are organized into topic(s).

Method

intnextPermutation(int v)
Permutates the bits with value 1 in bitstring v, return next permutation
if (v == 0)
    return 0;
else {
    int t = (v | (v - 1)) + 1;
    return t | ((((t & -t) / (v & -v)) >> 1) - 1);
int[]nextPermutation(int[] arr)
next Permutation
int[] a = (int[]) arr.clone();
int n = a.length - 1;
int j = n - 1;
while (a[j] > a[j + 1]) {
    if (j == 0) {
        int[] error = new int[] { 0 };
        return error;
    j--;
int k = n;
while (a[j] > a[k]) {
    k--;
int tmp = a[j];
a[j] = a[k];
a[k] = tmp;
int r = n;
int s = j + 1;
while (r > s) {
    tmp = a[r];
    a[r] = a[s];
    a[s] = tmp;
    r--;
    s++;
return a;
booleannextPermutation(int[] is)
next Permutation
int n = is.length;
for (int i = n - 1; i > 0; i--) {
    if (is[i - 1] < is[i]) {
        int j = n;
        while (is[i - 1] >= is[--j])
            ;
        swap(is, i - 1, j);
        rev(is, i, n);
...
int[]nextPermutation(int[] next)
next Permutation
int gt = -1;
for (int idx = 0; idx < next.length - 1; idx++) {
    if (next[idx] < next[idx + 1]) {
        gt = idx;
if (gt == -1) {
    return null;
...
voidnextPermutation(int[] nums)
next Permutation
int n = nums.length;
int i;
for (i = n - 2; i >= 0; --i) {
    if (nums[i] < nums[i + 1]) {
        break;
if (i < 0) {
...