In mathematics, the lexicographical order, (a.k.a. dictionary order or alphabetic order), is a natural order structure of the cartesian product of two ordered sets. Given A and B, two ordered sets, the lexicographical order in the cartesian product A × B is defined as
appears in a dictionary before a sequence
if and only if the first ai which is different from bi comes before bi in the alphabet. That assumes both have the same length; what is usually done is to pad out the shorter word for symbols for 'blanks', and to consider that a blank is a new minimum ('bottom') element.
For the purpose of dictionaries, etc., one may assume that all words have the same length, by adding blank spaces at the end, and considering the blank space as a special character which comes before any other letter in the alphabet. This also allows ordering of phrases. See alphabetical order.
An important property of the lexicographical order is that it preserves well-orders, that is, if A and B are well-ordered sets, then the product set A × B with the lexicographical order is also well-ordered.
An important exploitation of lexicographical ordering is expressed in the ISO 8601 date formatting scheme, which expresses a date as YYYY-MM-DD. This date ordering lends itself to straightforward computerized sorting of dates such that the sorting algorithm does not need to treat the numeric parts of the date string any differently from a string of non-numeric characters, and the dates will be sorted into chronological order. Note, however, that for this to work, there must always be four digits for the year, two for the month, and two for the day, so for example single-digit days must be padded with a zero yielding '01', '02', ... , '09'.
Suppose
The dictionary ordering
That is, if one of the terms
Informally,
This could be more elegantly defined recursively by defining the ordering of any set
represented by
This will satisfy
where
Or, more simply put, compare the first terms if they are equal compare the second — and so on.
In algebra it is traditional to order terms in a polynomial, by ordering the monomials in the indeterminates. This is fundamental, in order to have a normal form. Such matters are typically left implicit in discussion between humans, but must of course be dealt with exactly in computer algebra. In practice one has an alphabet of indeterminates X, Y, ... and orders all monomials formed from them by a variant of lexicographical order. For example if one decides to order the alphabet by
and also to look at higher terms first, that means ordering
and also
There is some flexibility in ordering monomials, and this can be exploited in Gröbner basis theory.
bool lexComp(std::string str_1, std::string str_2 ){
/* In ASCII, codes of Upper and Lower case letters have always a
difference of 32 places as they spread sequentialy, 65-90 for upper
case letters and 97-122 for lower case ones.
E.g. 'A' has code 65, 'a' has code 97 and 97-65=32. So we subtract 32
to turn from lower to upper case.
*/
//turn the first string to upper case
for(int i=0; i
//turn the second string to upper case
for(int i=0; i
Lexikographische Ordnung | Orden lexicográfico | Ordine lessicografico | Porządek leksykograficzny | Lexikografisk ordning
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Lexicographical order".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world