[C language] qsort function

qsort

Function : Performs a quick sort.

参数:void qsort( void *base, size_t num, size_t width, int (*cmp )(const void *e1, const void *e2 ) );

Header file : #include <stdlib.h>

Usage :

The first parameter is the address of the first element of the array to be sorted

The second parameter is the number of data to be sorted

The size of each parameter in the third queued data - in bytes

The fourth is to write a function to determine which data to be sorted is large and which is small. The regulations are as follows

A pointer of type void* can receive any type of address

The pointer of type void* cannot be dereferenced (the access byte is uncertain)

A pointer of type void* cannot perform addition and subtraction operations on integers

Let's first look at a simple application and its results

1. int type

#include <stdio.h>
#include <stdlib.h>
int cmp_int(const void* e1, const void* e2)
{
    //比较两个整型类型
    return *(int*)e1 - *(int*)e2;
}
int main()
{
    int arr[10] = { 9,8,7,6,5,4,3,2,1,0 };
    int sz = sizeof(arr) / sizeof(arr[0]);
    qsort(arr, sz, sizeof(arr[0]), cmp_int);
    int i = 0;
    for (i = 0; i < 10; i++)
    {
        printf("%d ",arr[i]);
    }
    return 0;
}

 

●See here, this is in ascending order, if you want to descend in descending order , directly return *(int*)e2-*(int*)e1; 

●For convenience, we remember the test type test for each type, for example, the int type is test1, and so on

2.float type

int cmp_float(const void* e1, const void* e2)
{
    return *(float*)e1 - *(float*)e2;
}
void test2()
{
    float f[] = { 9.0,8.0,7.0,5.0,4.0 };
    int sz = sizeof(f) / sizeof(f[0]);
    qsort(f, sz, sizeof(f[0]), cmp_float);
    int j = 0;
    for (j = 0; j < sz; j++)
    {
        printf("%f ", f[j]);
    }
}
int main()
{
test2();
return 0;
}

3.struct type

Since the members of the struct structure may have many types, I only give a relatively simple example here. The structure members only contain the name and age. Use qsort to sort by age and by name

struct Stu 
{
    char name[20];
    int age;
};

struct-age

int cmp_Stu_by_age(const void*e1,const void*e2)
{
    return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}

void test3()
{
    struct Stu s[3] = { {"zhangsan",20},{"lisi",30},{"wangwu",18} };
    int sz = (sizeof(s) / sizeof(s[0]));
    qsort(s, sz, sizeof(s[0]), cmp_Stu_by_age);
    int z = 0;
    for (z = 0; z < sz; z++)
    {
        printf("%s %d", s[z].name,s[z].age);
    }
}
int main()
{
test3();
return 0;
}

struct-name

int cmp_Stu_by_name(const void* e1, const void* e2)
{
    return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name);
}
void test4()
{
    struct Stu s[3] = { {"zhangsan",20},{"lisi",30},{"wangwu",18} };
    int sz = (sizeof(s) / sizeof(s[0]));
    qsort(s, sz, sizeof(s[0]), cmp_Stu_by_name);
    int z = 0;
    for (z = 0; z < sz; z++)
    {
        printf("%s %d", s[z].name,s[z].age);
    }
}
int main()
{
test 4();
return 0;
}

 There is a point to note here, the strcmp function should be used to compare strings, and the subtraction cannot be performed directly.

 Well, this time about the qsort function is over, give it a thumbs up.

Guess you like

Origin blog.csdn.net/weixin_60478154/article/details/123167986