用冒泡排序模拟qsort函数

qsort函数的使用

http://t.csdn.cn/U0C16

通过模拟qsort函数希望能帮助大家更深入的了解qsort函数的实现逻辑

1.根据参数先写好程序主体

void bu_sort(void* base, size_t sz, size_t width, int (*cmp)(const void* e1, const void* e2))
{
    //趟数
    size_t i = 0;
    for (i = 0; i < sz - 1; i++)
    {
        //一趟冒泡排序的过程
        size_t j = 0;
        for (j = 0; j < sz - 1 - i; j++)
        {
            if (cmp( (char*)base + j * width, (char*)base + (j + 1) * width ) > 0)
            {
                //交换
                Swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
            }
        }
    }
}

2.写交换函数

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++;
    }
}

3.用户自己写好cmp函数

#define _CRT_SECURE_NO_WARNINGS 1
struct stu
{
    char name[20];
    float GPA;
};
int cmp_str(const void* e1, const void* e2)
{
    return strcmp((char*)e1, (char*)e2);
}
int cmp_str_len(const void* e1, const void* e2)
{
    return strlen((char*)e1) - strlen((char*)e2);
}
int cmp_struct_stu_name(const void* e1, const void* e2)
{
    return strcmp(   ((struct stu*)e1)->name, ((struct stu*)e2)->name   );
}
int cmp_struct_stu_GPA(const void* e1, const void* e2)
{
    return (  ((struct stu*)e2)->GPA - ((struct stu*)e1)->GPA  );
}

4.调用函数

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void my_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++;
    }
}

void bu_sort(void* base, size_t sz, size_t width, int (*cmp)(const void* e1, const void* e2))
{
    //趟数
    size_t i = 0;
    for (i = 0; i < sz - 1; i++)
    {
        //一趟冒泡排序的过程
        size_t j = 0;
        for (j = 0; j < sz - 1 - i; j++)
        {
            if (cmp( (char*)base + j * width, (char*)base + (j + 1) * width ) > 0)
            {
                //交换
                my_swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
            }
        }
    }
}

typedef struct stu
{
    char name[20];
    float GPA;
}stu;
int compare_str(const void* e1, const void* e2)
{
    return strcmp((char*)e1, (char*)e2);
}
int cmp_str_len(const void* e1, const void* e2)
{
    return strlen((char*)e1) - strlen((char*)e2);
}
int cmp_struct_stu_name(const void* e1, const void* e2)
{
    return strcmp(((struct stu*)e1)->name, ((struct stu*)e2)->name);
}
int cmp_struct_stu_GPA(const void* e1, const void* e2)
{
    return (((struct stu*)e2)->GPA - ((struct stu*)e1)->GPA);
}
int main()
{
    int i = 0;
    stu s[3] = { {"zhangsan",3.4} ,{"zhangmazi",4.0} ,{"dingzhen",1.0} };
    bu_sort(s, 3, sizeof(s[0]), cmp_struct_stu_name);
    for (i = 0; i < 3; i++)
    {
        printf("%s  %f\n", s[i].name, s[i].GPA);
    }
    bu_sort(s, 3, sizeof(s[0]), cmp_struct_stu_GPA);
    for (i = 0; i < 3; i++)
    {
        printf("%s  %f\n", s[i].name, s[i].GPA);
    }

}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

脆皮骷髏人

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

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

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

打赏作者

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

抵扣说明:

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

余额充值