Qsort function: Use a Quick Sort routine for sorting
Usage: void qsort (void * base, int nelem, int width, INT (* fcmp) (const void *, const void *));
Parameters: 1 Address at the beginning of the array to be sorted 2 Number of elements to be sorted 3 space occupied by each element 4 pointer to function
There are many sorting methods used to determine the sorting order, such as sorting, Bubble sorting, Merge Sorting, and quick sorting. Quick sorting is a well-recognized sorting algorithm. It is faster than sorting by choice or Bubble sorting. This is because of its fast speed, so the system implements this algorithm in the library for our convenience. This is qsort.
Qsort requires a comparison function to achieve better versatility.. For example, you not only need to sort a number, but you may want to sort several numbers. For example, there is a structure struct num {int A; int B ;}; then I have an array of the num type, num dddd [100]; What should I do if I want to sort the array of the dddd? What should I do if I want to put the largest num element of A + B at the top of the array? This can be done by defining comparison functions. The purpose of the comparison function is to show qsort how to compare the element size. Such a comparison function, inline int mycmp (const void * a, const void * B), has two elements as the parameter and returns an int value. If the comparison function returns a value greater than 0, qsort considers A> B. If the result returned by the comparison function is equal to 0 qsort, the two elements A and B are considered equal. If the return value is smaller than zero qsort, AB is considered ), if your comparison function returns a value of-1 (smaller than zero), then qsort considers a <in this article, sorting is performed from small to large>
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 [2, 100]
// Sort by data values from small to large structured data. There are many types of key data in the sorting structure,
// 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 );
5. List struct
Struct in {int X; int y;} s [2, 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 *) 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 );
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 long line above
Return 1;
Else return-1;
}
PS: The qsort function is included in the header file, and the strcmp is included in the header file.