qsort函数的使用及推广 C语言

目录

一、qsort函数介绍

1、参数分析

void* base

size_t num

size_t size

int (*compar)(const void*, const void*)

2、使用方法

代码样例

运行结果

3、注意事项

二、模仿qsort函数实现一个冒泡排序的通用算法

1、bubble_sort函数

2、使用方法

代码内容

运行结果


一、qsort函数介绍

void qsort(void* base, size_t num, size_t size, int (*compar)(const void*, const void*));

1、参数分析

库函数qsort有四个参数

void* base

base中存放的是待排序数据中第一个对象的地址

size_t num

待排序数据中的元素个数

size_t size

待排序数据中,每个元素占的字节数

int (*compar)(const void*, const void*)

函数指针compar,指向用来比较待排序数据中的两个元素的函数

第一个const void*,存放第一个元素的地址

第二个const void*,存放第二个元素的地址

2、使用方法

qsort排序函数,需要引头文件#include <stdlib.h>

并且需自定义一个比较函数

代码样例

以整形数据和结构体数据排序为例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//void qsort(void* base, size_t num, size_t size, int (*compar)(const void*, const void*));
//库函数qsort有四个参数
//void* base,   base中存放的是待排序数据中第一个对象的地址
//size_t num,   待排序数据中的元素个数
//size_t size,  待排序数据中,每个元素占的字节数
//int (*compar)(const void*, const void*)  函数指针compar,指向用来比较待排序数据中的两个元素的函数
               //第一个const void*,存放第一个元素的地址
               //第二个const void*,存放第二个元素的地址
//qsort排序函数,需要引头文件#include <stdlib.h>

int compar_int(const void* e1, const void* e2)//整形数组排序
{
    return *(int*)e1 - *(int*)e2;//强制类型转换成int
}



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

int sort_by_age(const void* e1, const void* e2)//结构体数组排序,按年龄排序
{
    return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}

int sort_by_name(const void* e1, const void* e2)//结构体数组排序,按名字排序
{
    return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name);
}



void print1(int arr[], int sz)
{
    int i = 0;
    for (i = 0; i < sz; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
}


int main()
{
    //排序整形数组
    int arr1[] = { 1,3,5,7,9,2,4,6,8,0 };
    int sz = sizeof(arr1) / sizeof(arr1[0]);
    print1(arr1, sz);
    //排序
    qsort(arr1,sz,sizeof(arr1[0]),compar_int);
    //打印
    print1(arr1, sz);
    printf("\n");

    


    //排序结构体数据
    struct Stu s[3] = { {"zhangsan",30},{"lisi",34},{"wangwu",20}};
    sz = sizeof(s) / sizeof(s[0]);
    struct Stu* p = s;
    for (p = s; p < &s[3]; p++)
    {
        printf("%s,%d\n", p->name, p->age);
    }
    printf("\n");
    //按照年龄排序
    qsort(s, sz, sizeof(s[0]),sort_by_age);
    for (p = s; p < &s[3]; p++)
    {
        printf("%s,%d\n", p->name, p->age);
    }
    printf("\n");
    //按照名字排序
    qsort(s, sz, sizeof(s[0]), sort_by_name);
    for (p = s; p < &s[3]; p++)
    {
        printf("%s,%d\n", p->name, p->age);
    }
    printf("\n");
    return 0;
}

运行结果

3、注意事项

以上代码默认为升序排列

如果需要降序排列,只需将比较函数中的返回值改为e2-e1即可(交换e1和e2)

char字符串比较时,需定义的比较函数,如下所示

int cmp_char(const void*e1,const void*e2)
{
    return strcmp((char*)e1,(char*)e2);
}

二、模仿qsort函数实现一个冒泡排序的通用算法

1、bubble_sort函数

void bubble_sort(void* base, int sz, int width, int (*cmp)(const void* e1, const void* e2))//包含四个参数

2、使用方法

需自定义一个cmp比较函数

int cmp(const void* e1, const void* e2)//需自定义一个比较函数
{
    return *(int*)e1 - *(int*)e2;//以整形比较函数为例
}

代码内容

以整形数组为例

//模仿qsort函数实现一个冒泡排序的通用算法 

#include <stdio.h>


void Swap(char* buf1, char* buf2, int width)
{
    int i = 0;
    for (i = 0; i < width; i++)
    {
        char tmp = *buf1;
        *buf1 = *buf2;
        *buf2 = tmp;
        buf1++;
        buf2++;
    }
}


int cmp(const void* e1, const void* e2)//需自定义一个比较函数
{
    return *(int*)e1 - *(int*)e2;//以整形比较函数为例
}

void bubble_sort(void* base, int sz, int width, int (*cmp)(const void* e1, const void* e2))
{
    int i = 0;
    //确定趟数
    for (i = 0; i < sz - 1; i++)
    {
        //一趟排序
        int j = 0;
        for (j = 0; j < sz - 1; j++)
        {
            //两个元素比较
            if (cmp((char*)base + j * width, (char*)base + (j + 1) * width) > 0)//升序
            {
                //将base转换成char*类型是因为,char一次只访问一个字节
                //交换
                Swap((char*)base + j * width, (char*)base + (j + 1) * width, width);

            }
            
            
        }
    }
}



void print(int arr[], int sz)
{
    int i = 0;
    for (i = 0; i < sz; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
}
int main()
{
    int arr[10] = { 1,9,3,8,6,4,7,5,0,2 };
    int sz = sizeof(arr) / sizeof(arr[0]);
    print(arr, sz);
    printf("\n");
    bubble_sort(arr, sz, sizeof(arr[0]), cmp);
    print(arr, sz);
    printf("\n");
    return 0;
}

运行结果

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

摸鱼哥myg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值