Dictionary sorting in full sorting

Source: Internet
Author: User

Research on the lexicographic sorting algorithm

1. Non-recursive algorithms (lexicographic method)

A sequential relationship is defined for the characters in the given character set. On this basis, the two fully arranged characters are compared from left to right one by one.

For example, the character set {1, 2, 3} is smaller than the first digit. In this way, the entire array generated in the Lexicographic Order is

123,132,213,231,312,321

※A full arrangement can be considered as a string, and the string can have a prefix and a suffix.

Generate the next arrangement for the given full arrangement. The next one is that there is no other between this and next. This requires that this one and the next one have the same prefix as long as possible, that is, the change is limited to the suffix as short as possible.

For the arrangement given in alphabetical order, an algorithm for generating the next arrangement is as follows:

[Example] In general, Set P to a full arrangement of [1, N.
P = P1P2... Pn = P1P2... Pj-1PjPj + 1... Pk-1PkPk + 1... Pn
Find: J = max {I | pI <PI + 1}
K = max {I | pi> PJ}
1, switch to PJ, PK,
2. Change PJ + 1... Pk-1PjPk + 1... Pn flip
P' = P1P2... Pj-1PkPn... PK + 1pjpk-1... PJ + 1 is the next of P

[Example] How to get the next 346987521
1, find the position of the first P (I-1) <p (I) from the tail forward
3 4 6 <-9 <-8 <-7 <-5 <-2 <-1
Eventually finding 6 is the first small number to record the position I-1 of 6
2. Locate the last number greater than 6 from position I.
3 4 6-> 9-> 8-> 7 5 2 1
Finally, the location of 7 is found, and the record location is M.
3, switching position I-1 and M Value
3 4 7 9 8 6 5 2 1
4. All data after inverted I
3 4 7 1 2 5 6 8 9
Then 347125689 is the next order of 346987521

Code: use C ++ to sort the following countries in lexicographically

# Include <stdio. h> # include <string. h> int main () {char STR [8] [9] = {"China", "Japan", "Korea", "India", "Canada ", "American", "England", "France"}; char temp [9]; int I, L; for (I = 0; I <8; I ++) {for (L = 0; L <9; L ++) {printf ("% C", STR [I] [l]);} printf ("\ n") ;}// sort printf ("the above eight countries are sorted by dictionary as follows: \ n"); Int J, K; for (j = 0; j <8; j ++) for (k = J + 1; k <8; k ++) {If (strcmp (STR [J], STR [k])> 0) {// exchange strcpy (temp, STR [J]); strcpy (STR [J], STR [k]); strcpy (STR [K], temp) ;}}for (I = 0; I <8; I ++) // outputs printf ("% s \ n ", STR [I]);}

[Algorithm]: lexicographically arranged

Time Limit: 2000 ms memory limit: 65536 K

Total submit: 69 accepted: 13

Description

N elements {1, 2,..., n} have n! . Set this n! Numbers are arranged in lexicographically, and numbered 0, 1 ,..., N! -1. The number of each permutation is its lexicographic value. For example, when n = 3, the lexicographic values of the six different arrays are as follows:

Task: given an arrangement of N and n elements {1, 2,..., n}, calculate the lexicographically ordered value and the next lexicographically ordered value.

Input

The number of elements in the first row is n (n <15 ). The next line is an arrangement of n elements {1, 2,..., n.

Output

The first row is the lexicographically ordered value, and the second row is the next lexicographically ordered value.

Sample Input

8

2 6 4 5 8 1 7 3

Sample output

8227

2 6 4 5 8 3 1 7

Complete the code!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.