@Test public void testNextPermutation() { int[] arr; assertFalse(Library.nextPermutation(new int[0])); arr = new int[]{0, 0, 1}; assertTrue(Library.nextPermutation(arr)); assertArrayEquals(new int[]{0, 1, 0}, arr); assertTrue(Library.nextPermutation(arr)); assertArrayEquals(new int[]{1, 0, 0}, arr); assertFalse(Library.nextPermutation(arr)); assertArrayEquals(new int[]{1, 0, 0}, arr); arr = new int[]{1, 2, 3, 5, 9}; assertTrue(Library.nextPermutation(arr)); assertArrayEquals(new int[]{1, 2, 3, 9, 5}, arr); assertTrue(Library.nextPermutation(arr)); assertArrayEquals(new int[]{1, 2, 5, 3, 9}, arr); assertTrue(Library.nextPermutation(arr)); assertArrayEquals(new int[]{1, 2, 5, 9, 3}, arr); assertTrue(Library.nextPermutation(arr)); assertArrayEquals(new int[]{1, 2, 9, 3, 5}, arr); assertTrue(Library.nextPermutation(arr)); assertArrayEquals(new int[]{1, 2, 9, 5, 3}, arr); assertTrue(Library.nextPermutation(arr)); assertArrayEquals(new int[]{1, 3, 2, 5, 9}, arr); assertTrue(Library.nextPermutation(arr)); assertArrayEquals(new int[]{1, 3, 2, 9, 5}, arr); assertTrue(Library.nextPermutation(arr)); assertArrayEquals(new int[]{1, 3, 5, 2, 9}, arr); assertTrue(Library.nextPermutation(arr)); assertArrayEquals(new int[]{1, 3, 5, 9, 2}, arr); for (int i = 0; i < 110; i++) assertTrue(Library.nextPermutation(arr)); assertFalse(Library.nextPermutation(arr)); assertArrayEquals(new int[]{9, 5, 3, 2, 1}, arr); }
public static boolean nextPermutation(int[] arr) { int i = arr.length - 1; for (; i > 0 && arr[i - 1] >= arr[i]; i--); if (i <= 0) return false; { int j = arr.length - 1; for (; arr[j] <= arr[i - 1]; j--); int temp = arr[i - 1]; arr[i - 1] = arr[j]; arr[j] = temp; } for (int j = arr.length - 1; i < j; i++, j--) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } return true; }
Library { public static boolean nextPermutation(int[] arr) { int i = arr.length - 1; for (; i > 0 && arr[i - 1] >= arr[i]; i--); if (i <= 0) return false; { int j = arr.length - 1; for (; arr[j] <= arr[i - 1]; j--); int temp = arr[i - 1]; arr[i - 1] = arr[j]; arr[j] = temp; } for (int j = arr.length - 1; i < j; i++, j--) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } return true; } }
Library { public static boolean nextPermutation(int[] arr) { int i = arr.length - 1; for (; i > 0 && arr[i - 1] >= arr[i]; i--); if (i <= 0) return false; { int j = arr.length - 1; for (; arr[j] <= arr[i - 1]; j--); int temp = arr[i - 1]; arr[i - 1] = arr[j]; arr[j] = temp; } for (int j = arr.length - 1; i < j; i++, j--) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } return true; }  }
Library { public static boolean nextPermutation(int[] arr) { int i = arr.length - 1; for (; i > 0 && arr[i - 1] >= arr[i]; i--); if (i <= 0) return false; { int j = arr.length - 1; for (; arr[j] <= arr[i - 1]; j--); int temp = arr[i - 1]; arr[i - 1] = arr[j]; arr[j] = temp; } for (int j = arr.length - 1; i < j; i++, j--) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } return true; }  static String reverse(String s); static boolean isPalindrome(String s); static boolean isPalindrome(int x); static int sqrt(int x); static long sqrt(long x); static BigInteger sqrt(BigInteger x); static boolean isSquare(int x); static int pow(int x, int y); static int powMod(int x, int y, int m); static int reciprocalMod(int x, int m); static BigInteger factorial(int n); static BigInteger binomial(int n, int k); static int gcd(int x, int y); static boolean isPrime(int x); static boolean[] listPrimality(int n); static int[] listPrimes(int n); static int[] listSmallestPrimeFactors(int n); static int totient(int n); static int[] listTotients(int n); static boolean nextPermutation(int[] arr); }
Library { public static boolean nextPermutation(int[] arr) { int i = arr.length - 1; for (; i > 0 && arr[i - 1] >= arr[i]; i--); if (i <= 0) return false; { int j = arr.length - 1; for (; arr[j] <= arr[i - 1]; j--); int temp = arr[i - 1]; arr[i - 1] = arr[j]; arr[j] = temp; } for (int j = arr.length - 1; i < j; i++, j--) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } return true; }  static String reverse(String s); static boolean isPalindrome(String s); static boolean isPalindrome(int x); static int sqrt(int x); static long sqrt(long x); static BigInteger sqrt(BigInteger x); static boolean isSquare(int x); static int pow(int x, int y); static int powMod(int x, int y, int m); static int reciprocalMod(int x, int m); static BigInteger factorial(int n); static BigInteger binomial(int n, int k); static int gcd(int x, int y); static boolean isPrime(int x); static boolean[] listPrimality(int n); static int[] listPrimes(int n); static int[] listSmallestPrimeFactors(int n); static int totient(int n); static int[] listTotients(int n); static boolean nextPermutation(int[] arr);  }