The use of the sort function (#include <algorithm>)
when doing an ACM problem, sorting is a frequently used operation. If each time you write a bubble sort O (n^2), not only the program is easy to time out, and waste valuable game time, but also very likely to write wrong. The STL has a sort function that can be sorted directly into the array, with a complexity of N*LOG2 (n). With this function, you need to include the header file.
This function can pass two parameters or three parameters. The first parameter is the first address of the interval to sort, and the second is the next address of the interval end address. In other words, 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) and the default sort is ascending.
Take my "AC strategy", I need to sort the No. 0 to len-1 elements of the array T, write sort (t,t+len);
similar to vector v, sort (v.begin (), V.end ()); The data type of the
sort is not limited to integers, as long as it is possible to define a type that is less than an operation, such as a string class.
If you do not define a data type that is less than the operation, or if you want to change the order of the sort, use the third parameter-the comparison function. The comparison function is a function of its own definition, the return value is a bool type, it specifies what kind of relationship is "less than". To sort the array of integers 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) when sorting .
Assume that you have defined a struct node
struct node{
int A;
int b;
Double C;
}
There is an array of node type node arr[100], want to sort it: first by a value in ascending order, if the a value is the same, and then the B value in descending order, if B is also the same, in descending order of C. You can write a comparison function like this:
Here is the code snippet:
BOOL CMP (node X,node y)
{
if (X.A!=Y.A) return x.a
if (x.b!=y.b) return x.b>y.b;
return return x.c>y.c;
} Write sort (arr,a+100,cmp);
Qsort (S[0],n,sizeof (s[0]), CMP);
int cmp (const void *a,const void *b)
{
return * (int *) a-* (int *) b;
}
First, sort an array of type int
int num[100];
Sample:
int cmp (const void *a, const void *b)
{
return * (int *) A-* (int *) b;
}
Qsort (Num,100,sizeof (num[0]), CMP);
Second, sort the array of char types (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);
Third, sort the array of double types (especially 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);
Iv. sequencing of structural bodies
struct in
{
Double data;
int other;
}S[100]
According to the value of data from small to large structure of the order, about the structure of the key data types can be many, refer to the above example 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. Structural BODIES
struct in
{
int x;
int y;
}S[100];
Sort by x from small to large, when x is equal by y from largest to smallest
int cmp (const void *a, const void *b)
{
struct in *c = (in *) A;
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);
Six, sort the string
struct in
{
int data;
Char str[100];
}S[100];
Sort by the dictionary order of the string str in the struct
int cmp (const void *a, const void *b)
{
Return strcmp (((*) a)->str, ((in *) b)->str);
}
Qsort (S,100,sizeof (s[0]), CMP);
CMP for convex hull in computational geometry
int cmp (const void *a,const void *b)//Key CMP function, sorting all points except 1 points, rotation angle
{
struct Point *c= (point *) A;
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 you're in a straight line, put it far in front.
return 1;
else return-1;
}
The use of the C + + sort function