栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言库函数qsort(快速排序)用法以及自设计改进后的冒泡排序(可对任意类型数据排序)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言库函数qsort(快速排序)用法以及自设计改进后的冒泡排序(可对任意类型数据排序)

#include
#include
void bubble_sort(int arr[], int sz) //冒泡排序(仅适用于整型数组)
{
    int i = 0;
    for (i = 0; i < sz-1; i++)
    {
        int j = 0;
        for (j = 0; j < sz - i-1; j++)
        {
            if (arr[j] > arr[j+1])
            {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}
void Bubble_sort(void* base, int sz, int width, int (*p)(const void* e1, const void* e2))
{
    int i = 0;
    for (i = 0; i < sz - 1; i++)
    {
        int j = 0;
        for (j = 0; j < sz - i - 1; j++)
        {
            //两个元素的比较
            int flag=p((char*)base + width * j, (char*)base + width * (j + 1));
            if (flag > 0)
            {
                //交换
                void Swap(char*, char*,int);//声明函数
                Swap((char*)base + width * j, (char*)base + width * (j + 1), width);//调用函数
            }
        }
    }
}void Swap(char* p, char* q, int width)//交换两个元素
{
    char temp = ' ';
    int i = 0;
    for (i = 0; i < width; i++)
    {
        temp = *p;
        *p = *q;
        *q = temp;
        p++;
        q++;
    }
}
void Print(int* p, int sz)//打印整型数组
{
    int i = 0;
    for (i=0;i < sz;i++)
    {
        printf("%d ",*p++);
    }
}
//对整数数组排序
int cmp_int(const void* e1, const void* e2)
{
    return *(int*)e1 - *(int*)e2;
}
void test1()
{
    int arr[] = { 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 < sz; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("n");
}
//对浮点型数组排序
int cmp_float(const void* e1, const void* e2)
{
    float temp = *(float*)e1 - *(float*)e2;
    if (temp == 0)
    {
        return 0;
    }
    else if (temp > 0)
    {
        return 1;
    }
    else
    {
        return -1;
    }
}
test2()
{
    float arr[] = { 9.0,8.0,7.0,6.0,5.0,4.0,3.0,2.0,1.0,0.0 };
    int sz = sizeof(arr) / sizeof(arr[0]);
    qsort(arr, sz, sizeof(arr[0]), cmp_float);
    int i = 0;
    for (i = 0; i < sz; i++)
    {
        printf("%.2f ", arr[i]);
    }
    printf("n");
}
//对结构体排序
struct Stu //定义结构体
{
    char name[20];
    int age;
    char Tele[15];
};
int cmp_Stu_by_name(const void* e1, const void* e2)//按名字排序
{
    return (strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name));
}
int cmp_Stu_by_age(const void* e1, const void* e2)//按年龄排序
{
    return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}
int cmp_Stu_by_Tele(const void* e1, const void* e2)//按电话号排序
{
    return (strcmp(((struct Stu*)e1)->Tele,((struct Stu*)e2)->Tele));
}
void Print1(struct Stu S[],int sz)
{
    int i = 0;
    for (i = 0; i < sz; i++)
    {
        printf("%st", S[i].name);
        printf("%dt", S[i].age);
        printf("%sn", S[i].Tele);
    }
    printf("*********************n");
}
void test3()
{
    struct Stu S[3] = { {"张三",20,"13934124510"},{"李四",10,"13935124860"},{"王五",30,"13615241950"} };
    int sz = sizeof(S) / sizeof(S[0]);
    qsort(S, sz, sizeof(S[0]), cmp_Stu_by_name);
    Print1(S, sz);
    qsort(S, sz, sizeof(S[0]), cmp_Stu_by_age);
    Print1(S, sz);
    qsort(S, sz, sizeof(S[0]), cmp_Stu_by_Tele);
    Print1(S, sz);
}
//用改进后的冒泡排序进行排序
void test4()
{
    struct Stu S[3] = { {"张一",23,"13734124510"},{"张四",17,"13935124860"},{"李三",19,"13815241950"} };
    int sz = sizeof(S) / sizeof(S[0]);
    Bubble_sort(S, sz, sizeof(S[0]), cmp_Stu_by_name);
    Print1(S, sz);
    Bubble_sort(S, sz, sizeof(S[0]), cmp_Stu_by_age);
    Print1(S, sz);
    Bubble_sort(S, sz, sizeof(S[0]), cmp_Stu_by_Tele);
    Print1(S, sz);
}
int main()
{
    //冒泡排序
    //int arr[] = { 9,8,7,6,5,4,3,2,1,0 };
    //int sz = sizeof(arr) / sizeof(arr[0]);
    //bubble_sort(arr, sz);
    //Print(arr, sz);
    
    //库函数qsort(快速排序) 升序排序   头文件stdlib.h
    //qsort(void* base,size_t num,size_t width,int (*cmp)(const void* e1,const void* e2))
    //第一个参数:要排序的起始位置
    //第二个参数:要排序的元素个数
    //第三个参数:每个元素的大小,单位是字节
    //第四个参数是一个函数指针,是一个比较函数,使用户自己定义的函数
    //void* 是无类型的指针,可以接收任意类型的地址
    //void* 类型的指针不能进行解引用操作
    //void* 类型的指针不能进行加减操作

    test1();//测试qsort对整型数组的排序
    printf("------------------------n");
    test2();//测试qsort对浮点型数组的排序
    printf("------------------------n");
    test3();//测试对结构体的排序
    printf("------------------------n");
    test4();//测试改进后的冒泡排序
    return 0;
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/664093.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号