JOIN
Get Time
forums  Revision History
Search My Post History  |  My Watches  |  User Settings
Forums TopCoder Cookbook Algorithm Competitions - Rewriting Phase Re: Iterating Over All Permutations of an Array Revision History (1 edit)
Re: Iterating Over All Permutations of an Array (response to post by Ferlon)
You don't really need a picture. It would suffice to have your discussion relate to some examples of sequences. I was thinking something like this: http://wordaligned.org/articles/next-permutation

By the way, not all coders have access to reverse() method. Here is my Java implementation, which you are free to use:
//elements in a have to be sorted in ascending order
//changes the elements to achieve the next permutation
//returns false if there are no more permutations
public static boolean nextPermutation(int[] a)
{
	int N=a.length;
	int i=N-2;
	for (; i>=0; i--)
		if (a[i]<a[i+1])
			break;
	if (i<0) return false;
	
	for (int j=N-1; j>=i; j--)
	{
		if (a[j]>a[i])
		{
			int temp=a[i];
			a[i]=a[j];
			a[j]=temp;
			break;		
		}
	}
	for (int j=i+1; j<(N+i+1)/2; j++)		//reverse from a[i+1] to a[N-1]
	{
		int temp=a[j];
		a[j]=a[N+i-j];
		a[N+i-j]=temp;
	}
	return true;
}
Re: Iterating Over All Permutations of an Array (response to post by Ferlon)
You don't really need a picture. It would suffice to have your discussion relate to some examples of sequences. I was thinking something like this: http://wordaligned.org/articles/next-permutation