Rank dictionary order (C language)

Source: Internet
Author: User

enter the following string of 8 country names: China, JAPAN, KOREA, INDIA, CANADA, American, England and France, which are sorted in dictionary order.

 

 

#include <stdio.h>

#include <string.h>

 

void Main ()

{

charstr[8][9]={"", "JAPAN", "KOREA", "INDIA", "CANADA", "American", "ENGLAND", "FRANCE"};

chartemp[9];

inti,l;

For (i=0;i<8;i++)

       {

For (l=0;l<9;l++)

              {

printf ("%c", Str[i][l]);

              }

printf ("\ n");

       }

//Sort

printf ("The above 8 countries are sorted by dictionary as follows: \ n");

intj,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++)//Output

printf ("%s\n", Str[i]);

}

 

1. Dictionary order method
In the dictionary order method, for the number 1, 2, 3......N arrangement, the order of the different arrangement is from left to right to compare the corresponding number of successively to decide. For example for 5 number of permutations 12354 and 12345, arrange 12345 in front, rank 12354 in the rear. According to this rule, the first of all 5 numbers is 12345, and the last is 54321.
The dictionary order algorithm is as follows:
Set p to be a full arrangement of 1~n: P=P1P2......PN=P1P2......PJ-1PJPJ+1......PK-1PKPK+1......PN
1 starting from the right side of the arrangement, find the first number of digits that are smaller than the right number J (J (from the left side), that is, j=max{i|pi<pi+1}
2 in the number on the right of PJ, find all the smallest number PK in the number that is larger than PJ, that is, K=MAX{I|PI>PJ} (the number on the right is incremented from right to left, so K is the highest number in all numbers larger than PJ).

3) Swap PI,PK
4 The PJ+1......PK-1PKPK+1PN inverted to get the permutation P ' =p1p2.....pj-1pjpn.....pk+1pkpk-1.....pj+1, which is the next permutation of the permutation p.

For example 839647521 is an arrangement of the number 1~9. The steps to generate the next arrangement from it are as follows:
from right to left, find the first number in the permutation that is smaller than the right digit. 4839647521
Find the smallest of 5839647521 in the number after the number 4
Swap 5 with 4 for 839657421
Turn 7421 upside down 839651247.
So the next arrangement for 839647521 is 839651247.

 

 

Related Article

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.