Qsort () should be a fast sort. It seems that data is moved in blocks, which is faster.
Prototype:
_ Cribd void _ cdecl qsort (void *, size_t, size_t, INT (*) (const void *, const void *));
Explanation: qsort (array name, number of elements, space occupied by elements (sizeof), comparison function)
A comparison function is a self-written function that follows the int COM (const void * a, const void * B) format.
When the relationship between A and B is greater than or equal to <=, negative values (or opposite values) are returned respectively ).
When using a B, you must forcibly convert the type. After you convert from void * to a response type, perform the operation.
Array subscript starts from zero, number is N, subscript 0-(n-1 ).
Example:
Int NN [100], n = 100;
Int COM (const void * a, const void * B)
{
Return * (int *) A-* (int *) B;
}
Qsort (void *) nn, N, sizeof (INT), com );
Related:
Why must you give the number of elements? (Because the array does not know how many elements it has.) Why must you give the double size? (Because qsort does not know that the unit of sorting is doubles.) Why do you have to write the ugly function used to compare doubles values? (Because qsort needs an indicator to point to a function, because it does not know the element type to be sorted) why does the qsort comparison function accept the const void * Reference instead of the char * reference? (Because qsort can sort non-string values) Bjarne stroustrup
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? 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 );
Write a pure string by yourself: (temp is a two-dimensional string array, and J is the total number of strings to be sorted)
Int cmpstr (const void * a, const void * B)
{
Return strcmp (char *) A, (char *) B );
}
Qsort (temp, J, sizeof (temp [0]), cmpstr );
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;
}
Qsort () is C Program A function in the library stdlib. H, which needs to be sorted by comparison functions;
Sort () is a standard algorithm in STL.
# Include <stdlib. h>
Int CMP (const void * a, const void * B)
{
Return * (int *) B)-* (int *) );
}
.
.
.
Qsort (Q, N, sizeof (INT), CMP );
.
.
.
}
Qsort sorts one-dimensional arrays and string Arrays:
# Include "stdio. H"
# Include "stdlib. H"
Int A [100];
Int CMP (const void * P, const void * q)
{
Return (* (int *) P)-* (int *) q );
}
Int main ()
{
Int N;
Scanf ("% d", & N );
For (INT I = 0; I <n; I ++)
Scanf ("% d", & A [I]);
Qsort (void *) A, N, sizeof (A [0]), CMP );
For (INT I = 0; I <n; I ++)
Printf ("% d \ n", a [I]);
// While (1 );
Int CMP (const void * P, const void * q)
{
Return strcmp (char *) P, (char *) q );
}
Int main () return 0;
}
# Include "stdio. H"
# Include "stdlib. H"
# Include "string. H"
Char A [20005] [25];
{
Int n, m, I, J;
While (1)
{
Scanf ("% d", & N, & M );
If (n = 0 & M = 0) break;
For (I = 0; I <n; I ++)
{
Scanf ("% s", a [I]);
}
Qsort (void *) A, N, sizeof (A [0]), CMP );
For (I = 0; I <n; I ++)
Printf ("% s \ n", a [I]);
}
}