【c语言】qsort函数

base:需比较值的地址

num:长度

width:单个元素的字节长度

compare:比较规则的函数指针 

compare 函数中,e1,e2是需比较两个元素的地址,int型返回值有具体规则如下 


举例:

int compareInt(const void *e1, const void *e2) {
    int int1 = (*(int *) e1);
    int int2 = (*(int *) e2);
    if (int1 < int2) {
        return -1;
    } else if(int1 == int2){
        return 0;
    } else{
        return 1;
    }
}

int main() {
    int arr[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
    qsort(arr, sizeof arr / sizeof arr[0], sizeof arr[0], compareInt);
    for (int i = 0; i < sizeof arr / sizeof arr[0]; ++i) {
        printf("%d\t", arr[i]);
    }
    return 0;
}

运行结果:

  


案例2:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int compareStructByName(const void * e1, const void * e2){
    return strcmp(((struct Stu *)e1)->name, ((struct Stu *)e2)->name);
//    return ((struct Stu *)e1)->age - ((struct Stu *)e2)->age;
}

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

void compareByQsort2() {
    struct Stu s[] = {
        { "jane", 15 },
        { "otto", 20 },
        {"chloe", 28}
    };
    qsort(s, sizeof s / sizeof s[0], sizeof s[0], compareStructByName);
    for (int i = 0; i < sizeof s / sizeof s[0]; ++i) {
        printf("%s,%d     ", s[i].name, s[i].age);
    }
    printf("\n");
    qsort(s, sizeof s / sizeof s[0], sizeof s[0], compareStructByAge);
    for (int i = 0; i < sizeof s / sizeof s[0]; ++i) {
        printf("%s,%d     ", s[i].name, s[i].age);
    }
}

int main() {
    compareByQsort2();
    return 0;
}
    return 0;
}

运行结果:


qsort型的冒泡排序

int cmp(const void *e1, const void *e2){
    return *((int *)e1) - *((int *)e2);
}

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

// qsort 型的 冒泡排序
void bubble_sort(void *base, int length, int width, int(*cmp)(const void *e1, const void *e2)) {
    for (int i = 0; i < length; ++i) {
        int flag = 0;
        // 如果 在一轮冒泡中 没有进行过交换,证明已不需要再交换了;
        for (int j = 0; j < length - 1 - i; ++j) {
            int cmp_res = cmp((char*)base + j*width, (char*)base + (j+1)*width);
            // for reading;
            if (cmp_res > 0){
                // exchange;
                Swap((char*)base + j*width, (char*)base + (j+1)*width, width);
                flag++;
            } else if (cmp_res == 0){
                // do nothing;
            } else{
                // do nothing;
            }
        }
        if (flag == 0){
            break;
        }
    }
}

// 遍历arr
void printArr(int * arr, int length){
    for (int i = 0; i < length; ++i) {
        printf("%d  ", arr[i]);
    }
    printf("\n");
}

int main() {
    int arr[5] = {2,1,4,3,0};
    bubble_sort(arr, 5, 4, cmp);
    printArr(arr, 5);
    return 0;
}

运行结果:

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值