1.qsort function:
Primitive : void qsort (void *base, int nelem, int width, int (*fcmp) (const void *,const void *));
power : sorting using a quick sort routine
Parameters
1 array first address to sort
2 number of elements to be sorted in the array
3 space size of each element
4 A pointer to a function that determines the order of the sort
stating that :the Qsort function is provided in the ANSI C standard, and its declaration in the Stdlib.h file is written according to the dichotomy, with a time complexity of N*log (n).
The function required by qsort is a comparison function that needs to be defined by itself, and the comparison function makes the qsort more versatile. With the comparison function, Qsort can be sorted in ascending or descending order for structures such as arrays, strings, structures, etc.
such as the int cmp (const void *a, const void *b) has two elements as parameters (the format of the parameter cannot be changed.) Returns an int value that, if the comparison function returns greater than 0,qsort, is considered a > b, and returns less than 0,qsort is considered a < B. Qsort know the size of the element, you can put the large front to go. If your comparison function returns to be 1 (that is, a > B), but returns 1 (less than 0), then Qsort thinks a < B, puts B in front, but actually a > b, so it makes a difference in descending order. In simple terms, the function of the comparison function is to show qsort how the size of the element is compared.
2. Several common cmp functions in Qsort:
first, sort an array of type int
int num[100];
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];
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? 1:-1;
}
Qsort (S,100,sizeof (s[0]), CMP);
Five, the structure of the two-level ranking
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 ((* (in *) 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;
}
3.sort and Qsoort comparison :
1 . The difference between CMP functions and CMP functions in Qsort
int cmp (const int &A,CONST int &b)
{
Return a>b
}
The CMP function parameter in sort can be directly a reference type that participates in the comparison.
2 . The CMP function compares qsort with "-" and sort with ">". This is also an important difference.
3 . the sort function is a function of the standard template Library in C + +, which has been optimized on qsort () and can take different algorithms depending on the situation, so it is faster. The sort () executes faster than Qsort () under the same elements and the same comparison conditions. In addition, sort () is a generic function that can be used to compare any container, any element, any condition. Call <algorithm> When you use
Sort (begin (), End (), CMP),
Qsort and sort learning and comparison