[Algorithm] Listing permutations

I will make the program lists the permutations of {1, 2, …, n} Order dictionaries.
For example, with n = 3, I have listed enough 6 conversion:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

So the first permutation is <1, 2, …, a>. Final permutation <n, A-1, …, 1〉.

Generates permutations must be greater than the current permutation, more than enough permutations permutations current larger sense there can be no other permutations in between them when ordering. Assuming the current permutation is a = <3, 2, 6, 5, 4, 1〉, Review 4 The last element, I see we are descending, which means that even if we have permutation 4 How this element, It was a little more permutations permutations current. Thus we must consider a[2] = 2, replace it with a different value. We will replace it with any value?, can not be 1 because then there would be a smaller permutations, can not be 3 because there was a[1] = 3 already (following elements are not selected on the previous value of that element selected). Residual values 4, 5, 6. As to a larger permutation enough current so we chose a[2] = 4. As the value (the[3], the[4], the[5], the[6]) will take in practice {2, 6, 5, 1}. Just because big enough so we will find the smallest representation of 4 This number assigned to a[3], the[4], the[5], the[6] ie <1, 2, 5, 6〉. So the new permutation is <3, 4, 1, 2, 5, 6〉.

What I have commented this example: The last paragraph of the current permutation Descending, of a[5] = 4 the smallest number in the last paragraph descending satisfy conditions over a large[2] = 2. If swapped a[5] for a[2] I will be a[2] = 4 and the end is still to be sorted descending. When it wants to perform for the smallest value in the end, we just reverse end. In the case of the current permutation is <2, 1, 3, 4> The next permutation of <2, 1, 4, 3〉. We can also consider the permutation <2, 1, 3, 4> Have tapered end, This last part consists of 1 element (4) So engineering students from the next permutation permutation can now build as follows:

  1. Determination end the longest descending, i find the index of the element a[in] which began late. This means looking up from the bottom row to the first element 2 (because if you look at the first element that is immediate and final permutations should not need), i first met indices satisfy a[i-1] < the[in].
  2. If you find the index i as above
    – In the end Descending, a search element[to] smallest satisfies conditions a[to] > the[i-1]. Do tapered end, this is done by looking from the top end of the range meets the first index k satisfies a[to] > the[i-1] (can use binary search).
    – Island worth a[to] and a[i-1]
    – Overturned end descending order (from a[in] to a[to]) become increasing.
  3. If you do not find the whole range that is already going to decrease, Here is the final configuration.

C code illustrates.

/*
code by nguyenvanquan7826
*/

#include <cstdio> 
#include <cstdlib>
#include <iostream>
#include <algorithm>		// thu vien chua ham swap (hoan vi 2 so)
using namespace std;

int main(int argc, char **argv)
{
	int n, *a;
	scanf("%d", &n);
	a = new int[n];
	for (int i=0; i<n; i++)			// khoi tao va in ra cau hinh ban dau
	{
		a[i] = i+1;
		printf("%d   ", a[i]);
	}	
	printf("n");
	
	int i = n-1;		// i la vi tri ma doan cuoi giam dan
	while (i>0)			// trong khi doan cuoi khong phai bat dau tu vi tri dau tien
	{
		i = n-1;
		while (a[i] < a[i-1])	// tim vi tri dau tien ma doan cuoi giam dan
		{
			i--; 
		}
		
		if (i>0)		// Neu tim duoc i
		{
			int k;		// tim so a[k] nho nhat trong doan giam dan ma a[k] > a[i-1]
			for (k = n-1; k >= i; k--)		
			{
				if (a[k] > a[i-1])		
				{
					break;
				}
				
			}
			swap(a[k], a[i-1]);		// hoan vi a[k] va a[i-1]
			
			// dao nguoc doan cuoi giam dan thanh tang dan
			for (int k = i; k < i + (n - i) / 2; k++)	
			{
				swap(a[k], a[n - 1 - k + i]);
			}
			
			for (int k = 0; k < n; k++)		// in hoan vi ra
			{
				printf("%d   ", a[k]);
			}
			printf("n");
		}
	}
	
	return 0; 
}

Articles citing Textbook data structures and algorithms of Le Minh Hoang teacher. You can view the curriculum as Pascal code you wrote.