1. qsortFunction:
Original Type:Void qsort (void * base, int nelem, int width, INT (* fcmp) (const void *, const void *));
Merit Yes:Sort by Quick Sort routine
Parameters:
1. First address of the array to be sorted
2. Number of elements to be sorted in the array
3. space occupied by each element
4. pointer to the function to determine the order of sorting
Description Ming:The qsort function is provided in the ansi c standard and its declaration is written in the stdlib. h file according to the binary method. The time complexity is N * log (n ).
Qsort requires that the provided function be a self-defined comparison function. Compared functions make qsort more universal. With the comparison function qsort, you can sort arrays, strings, struct, and other structures in ascending or descending order.
For example, int CMP (const void * a, const void * B) has two elements as parameters (the parameter format cannot be changed .) Return an int value. If the comparison function returns a value greater than 0, qsort considers A> B and returns a value less than 0. qsort considers a <B. Qsort knows the size of the element, so you can put the big one before it. If your comparison function returns 1 (A> B) but-1 (less than 0), then qsort considers a <B, put B in the front, but it is actually A> B, so the difference in descending order is caused. To put it simply, the function of comparison is to compare the major and minor issues that specify elements for qsort.
2.QsortCommonCMPFunction:
I. Sorting int-type Arrays
Int num [100];
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];
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? 1:-1;
}
Qsort (S, 100, sizeof (s [0]), CMP );
5. List struct
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. Comparison between sort and qsoort:
1. CMP functions are different from CMP functions in qsort.
Int CMP (const Int & A, const Int & B)
{
Return A> B
}
CMP function parameters in sort can be reference types involved in comparison directly.
2.When CMP functions are compared, qsort uses "-", while SORT uses ">". This is also an important difference.
3.The sort function is a function of the standard template library in C ++ and has been optimized on qsort (). Different templates can be used based on different situations.Algorithm, So fast. Sort () is faster than qsort () when the same element is more than the same. In addition, sort () is a class function that can be used to compare any container, any element, or any condition. <Algorithm>
Sort (begin (), end (), CMP ),