Sort ()

Source: Internet
Author: User

Sorting is a frequently used operation for ACM questions. If you write a bubble-like O (N ^ 2) sorting by yourself each time, not only will the program easily time out, but will also waste valuable time and may write errors. STL contains a sort function that can be used to sort arrays directly. The complexity is N * log2 (n ). To use this function, you must include the header file.
This function can contain two or three parameters. The first parameter is the first address of the interval to be sorted, and the second parameter is the next address of the end of the interval. That is to say, the sorting interval is [a, B ). Simply put, there is an array int A [100]. To sort the elements from a [0] to a [99], just write sort (A, A + 100) the default sorting method is ascending.
Take my out of the "AC policy" question, need to sort the elements of the array t 0th to the len-1, write sort (t, t + Len );
The sorting of vector V is similar, sort (V. Begin (), V. End ());
The data type of sorting is not limited to integers, as long as it is defined to be smaller than the operation type, such as string type.
If you do not define a data type smaller than the operation, or want to change the sorting order, you need to use the third parameter-comparison function. A comparison function is a self-defined function with a return value of the bool type. It specifies the relationship that is "less ". To sort the integer arrays in descending order, you can first define a comparison function CMP.
Bool CMP (int A, int B)
{
Return A> B;
}
Write sort (A, A + 100, CMP) during sorting );

Suppose you have defined a struct node.
Struct node {
Int;
Int B;
Double C;
}
There is a node-type array node arr [100]. You want to sort it: first sort it in ascending order of the value. If the value is the same, then sort it in descending order of the B value, if B is the same, it is arranged in descending order of C. You can write such a comparison function:
The following is a code snippet:
Bool CMP (node X, node y)
{
If (X.! = Y. a) Return X.

If (X. B! = Y. B) return X. B> Y. B;
Return return X. c> Y. C;
} Write sort (ARR, A + 100, CMP) during sorting );

Finally, let's look at a complete example of a question "Sort file names" during the preliminary competition ".
The following is a code snippet:
# Include <iostream>
# Include <algorithm>
# Include <string>
Using namespace STD;
// Define a struct to indicate the file. A indicates the file name, and B indicates the file type (either "file" or "dir ")
Struct node {
String A, B;
};
// In the ASCII code, all uppercase letters are placed before all lowercase letters, and 'A' <'Z' <'A' <'Z'
// This case is case-insensitive, so it cannot be compared directly with strings. A user-defined lt function means less.
// Convert all the two strings to lowercase and then compare the size (lexicographically)
Bool LT (string X, string y)
{
Int I;
For (I = 0; I <X. Length (); I ++)
If (X [I]> = 'A' & X [I] <= 'Z ')
X [I] = 'A' + (X [I]-'A ');
For (I = 0; I <Y. Length (); I ++)
If (Y [I]> = 'A' & Y [I] <= 'Z ')
Y [I] = 'A' + (Y [I]-'A ');
Return x <Y;
}
// User-Defined comparison function, first arranged in ascending order of B values (that is, "dir" placed before "file)
// If the value of B is the same, sort it in ascending order of A, using the defined lt Function
Bool comp (node X, node y)
{
If (X. B! = Y. B) return X. B <Y. B;
Return LT (X. A, Y. );
}
Int main ()
{
Node arr [10001];
Int size = 0;
While (CIN> arr [size]. A> arr [size]. B)
Size ++;
Sort (ARR, arr + size, comp );
For (INT I = 0; I <size; I ++)
Cout <arr [I]. A <"" <arr [I]. B <Endl;
Return 0;
}


This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/s030501408/archive/2010/02/26/5329477.aspx

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.