nextpermutation funciton in java code example
Example: java nextpermutation
public static boolean next_permutation(int[] arr) {
int len = arr.length;
int i = len - 1;
while (i > 0) {
if (arr[i - 1] < arr[i]) break;
i--;
}
if (i <= 0) return false;
int j = len - 1;
while (j >= i) {
if (arr[i - 1] < arr[j]) break;
j--;
}
swap(i - 1, j, arr);
len--;
while (i < len) {
swap(i, len, arr);
len--;
i++;
}
return true;
}
public static void swap(int x, int y, int[] arr) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}