cmp function in qsort()

Basic form:

int cmp(const void *a ,const void *b);

Analyze this basic form:
The return value of the function is int
The formal parameters are two const void *, which means that the two parameters are of const void * type, that is, a pointer. Const represents that the value of the memory unit pointed to by the pointer cannot be changed. Void *, a typeless pointer, can be converted to any type of pointer through forced type conversion, or even directly get the data value pointed to.

usage method

For qsort

int cmp(const void *a ,const void *b)
{
	...
}

cmp() has three return values (take qsort as an example):
1. Returns a positive number: A is arranged after b;
2. Return 0: a and b are equal;
3. Returns a negative number: a comes before b;

Let's take a look at some examples
1. Sort int arrays in ascending order

int cmp(const void *a ,const void *b)
{
	return *(int *)a-*(int *)b;//The value pointed to by pointer a minus the value pointed to by pointer b
}

Explain why this is ascending. This is a one-dimensional array. Assuming that the subscript of a is 0, then the subscript of B is 1, that is, a is the number in front of B. If A-B > 0 and returns a positive number, the function will exchange the two numbers. On the contrary, if a-b < 0, the function will not exchange, so as to achieve the purpose of ascending sorting.
2. Sort the double array in ascending order
The above method is not applicable to double type, because the cmp return value is int type. If the difference between two decimals is very small, for example: a=0.15, b=0.14, it will be forcibly converted to 0 return without exchange

int cmp(const void * a,const void *b)
{
    if( *( double* )a > *( double * )b )
        return 1;
    else if( *( double* )a < *( double * )b )
        return -1;
    return 0;
}

3. For the two-dimensional array obtained by directly opening up the array space instead of malloc secondary pointer, sort from small to large:

int comp(const void*a, const void*b) 
        return((int*)a)[0]-((int*)b)[0];//Here, replace the [] subscript with your own sorting basis

By comparing one-dimensional arrays, we can find that one-dimensional arrays have a reference operation, but two-dimensional arrays do not. According to my understanding, two-dimensional arrays are written in the form of (int*)a)[0], and [0] has a function similar to dereference.

3.1. Sort the two-dimensional array obtained from malloc from small to large:

int comp(const void *a, const void *b)
{
    if ((*(int**)a)[0] == (*(int**)b)[0])
        return (*(int**)a)[1] - (*(int**)b)[1];//Here, it means sorting by the first item first. If the first item is the same, it means sorting by the second item
    return (*(int**)a)[0] - (*(int**)b)[0];
}

4. Sort strings in dictionary order

#include <string.h>
int cmp(const void *a ,const void *b)
{
	return strcmp( (char *)a , (char *)b );
}

Example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(const void *a ,const void *b)
{
	return strcmp( (char *)a , (char *)b );
}
int main()
{
    char A[4][6]={
    "abcde",
    "aacde",
    "accde",
    "bcdef"
    };
    qsort(A,4,sizeof(A[0]),cmp);
    for(int i=0 ; i<4 ; i++)
        printf("%s\n",A[i]);
}
/*Output:
aacde
abcde
accde
bcdef
*/

5. Sort structures in ascending order

typedef struct
{
    int A;
    double B;
    char C[];
}Sructure;

int cmp(const void * a,const void *b)
{
    //Reference to int
    return (*(Sructure *)a).A-(*(Sructure *)a).B;
   	//Reference to double
   	if( (*( Sructure* )a).B > (*( Sructure* )b).B )
        return 1;
    else  if( (*( Sructure* )a).B < (*( Sructure* )b).B )
        return -1;
    return 0;
    //Sort string dictionary order
   	return strcmp( (*(Sructure *)a).C , (*(Sructure *)b).C )
}

Refer to:
Different cases of C language qsort function sorting two-dimensional arrays
Some usage methods of cmp function in qsort() (C language)

Keywords: C++ Back-end

Added by Siggles on Wed, 10 Nov 2021 11:43:45 +0200