[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:
- 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].
- 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. - 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.
Welcome, Your articles are very or, I want to ask some of you have written an algorithm, may not involve that much permutation problem finding all subsets of a set of n elements as {1,2,3,4,…n} the algorithm can be used according to you is how .and if the user is not recursive algorithm for this problem is like his nao.theo learn the majority of others are using Binary counting
Thank you for your interest in blog posts.
For this post I have not had time to write a blog, but you can see how ” lists the k element subsets of the set {1, 2, …, n} Order from filling” as in his books at the site of the Royal 20 (document link above). That is 1 good way. I'll try to write as soon as.
selfless misread de.minh do then that k Me,to the code view,if its still update later :d
Here is my code C,you can refer to it anyway:
Welcome! Thanks for your article. You can write code in pascal permutation problem without using subroutine ko?
Code in Pascal curriculum Queen of Leon show has got you. In his code only 1 segment subroutine is swap – conversion 2 number, you can instead directly without the use of subroutine.
Welcome, his book to do this but its not very good at setting tr2inh for you can not help yourself, find all sets of serial numbers to enter into such 012 out 012,021,102,120,201,210 you help me write for details thank you
This is exactly the same as you. Just replace the number entered by the characters in your string only. 😀
dev = c its use your offline
cho minh xin mau ben pascal duoc khong
Bạn xem trong giáo trình nhé
cho e hỏi độ phức tạp của thuật toán này là gì có phải là O(n.n!) k ạ
– Lật ngược thứ tự đoạn cuối giảm dần (from a[in] to a[to]) become increasing.
cái dòng này của anh em thấy nó kì kì, đúng ra phải thay chữ k thành chữ n mới đúng
Ah đúng rồi bạn. Mình lộn, tài a[in] to a[n].