(1) sort function usage
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. A <Y.;
If (X. B! = Y. B) return X. B> Y. B;
Return return X. c> Y. C;
} Write sort (ARR, A + 100, CMP) during sorting );
Qsort (s [0], n, sizeof (s [0]), CMP );
Int CMP (const void * a, const void * B)
{
Return * (int *) A-* (int *) B;
}
(2) qsort usage
I. Sorting int-type Arrays
Int num [100];
Sample:
Int CMP (const void * a, const void * B)
{
Return * (int *) A-* (int *) B;
}
Qsort (Num, 100, sizeof (Num [0]), CMP );
2. Sort char array (same as int type)
Char word [100];
Sample:
Int CMP (const void * a, const void * B)
{
Return * (char *) A-* (int *) B;
}
Qsort (word, 100, sizeof (word [0]), CMP );
3. Sort arrays of the double type (special note)
Double in [100];
Int CMP (const void * a, const void * B)
{
Return * (double *) A> * (double *) B? 1:-1;
}
Qsort (in, 100, sizeof (in [0]), CMP );
4. sorting the struct at a level
Struct in
{
Double data;
Int Other;
} S [100]
// Sort by data values from small to large struct. There are many types of sorting key data in the Structure Body. refer to the example above to write
Int CMP (const void * a, const void * B)
{
Return (in *) A)-> data-(in *) B)-> data;
}
Qsort (S, 100, sizeof (s [0]), CMP );
V. Structure
Struct in
{
Int X;
Int y;
} S [100];
// Sort by X from small to large, and sort by Y from large to small when X is equal
Int CMP (const void * a, const void * B)
{
Struct in * c = (in *);
Struct in * D = (in *) B;
If (c-> X! = D-> X) return C-> X-D-> X;
Else return D-> Y-C-> Y;
}
Qsort (S, 100, sizeof (s [0]), CMP );
6. Sort strings
Struct in
{
Int data;
Char STR [100];
} S [100];
// Sort the string 'str' in alphabetical order.
Int CMP (const void * a, const void * B)
{
Return strcmp (in *) A)-> STR, (IN *) B)-> Str );
}
Qsort (S, 100, sizeof (s [0]), CMP );
VII. CMP for convex hull Calculation
Int CMP (const void * a, const void * B) // The Key CMP function sorts all vertices and rotation angles except 1.
{
Struct point * c = (point *);
Struct point * D = (point *) B;
If (calc (* C, * D, P [1]) <0) return 1;
Else if (! Calc (* C, * D, P [1]) & DIS (c-> X, C-> Y, P [1]. x, p [1]. y) <DIS (D-> x, D-> Y, P [1]. x, p [1]. y) // if it is in a straight line, place the far one in front of it.
Return 1;
Else return-1;
}
(3) There are also stable_sort sorting functions. The difference between stable_sort and sort is that the sorting of stable_sort is "stable.
The relative positions of the two elements whose values are "same" are not changed.
For example, after sorting 1 2 3 4 5 (1) 6 5 5 (2) 8 7 9, it will become 1 2 3 4 5 (1) 5 (2) 6 7 8 9.
The relative positions of the two 5 (1 and 2) are not changed before and after sorting.
Reprinted from: http://blog.sina.com.cn/s/blog_6439f26f01012xw3.html