Java Integer Array Permutation nextPermutation(int[] is)

Here you can find the source of nextPermutation(int[] is)

Description

next Permutation

License

Open Source License

Declaration

public static boolean nextPermutation(int[] is) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {

    public static boolean nextPermutation(int[] is) {
        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])
                    ;//  w  ww. j  a  v  a2 s .c  om
                swap(is, i - 1, j);
                rev(is, i, n);
                return true;
            }
        }
        rev(is, 0, n);
        return false;
    }

    public static void swap(int[] is, int i, int j) {
        int t = is[i];
        is[i] = is[j];
        is[j] = t;
    }

    public static void rev(int[] is, int s, int t) {
        while (s < --t)
            swap(is, s++, t);
    }
}

Related

  1. nextPermutation(int v)
  2. nextPermutation(int[] arr)
  3. nextPermutation(int[] next)
  4. nextPermutation(int[] nums)