Next Permutation

Next Permutation
Given an array of integers, a[], next(a) sets a[] to the next permutation in increasing order.

Be sure to use hasNext(a) first to confirm that there actually is a next permutation first, or next(a) will crash. public static boolean hasNext(int[] a) { for(int i=0;i < a.length-1;i++) if(a[i] < a[i+1]) return true; return false; } public static void next(int[] a) { int i = a.length - 2; while(a[i] >= a[i+1]) i--; int j = a.length - 1; while(a[j] <= a[i]) j--; int tmp = a[i]; a[i] = a[j]; a[j] = tmp; i++; j = a.length - 1; while(i < j) 	{ tmp = a[i]; a[i] = a[j]; a[j] = tmp; i++; j--; } }