qsort的用法

C/C++中有一个快速排序的标准库函数qsort,在stdlib.h中声明,其原型为:

void qsort(void *base, int nelem, unsigned int width, int (* pfCompare)(const void *,const void *));

其中base为待排序的数组,nelem为数组元素个数,width为数组每个元素的大小(以字节为单位),pfCompare为函数指针,指向一个“比较函数”,需要根据自己的需求编写。

“比较函数”原型:

int 函数名(const void * elem1, const void * elem2);

elem1和elem2是两个待比较的元素

1)如果*elem1应该排在*elem2前面,则函数返回值为负整数

2)如果*elem1和*elem2哪个排在前面都行,则函数返回值为0

3)如果*elem1应该排在elem2后面,则函数返回值为正整数

下面是几个例子:

1.按个位数从小到大排序

#include <iostream>
#include <stdlib.h>
using namespace std;
#define NUM 5
int MyCompare(const void * elem1,const void * elem2)
{
    int * p1 = (int *) elem1;
    int * p2 = (int *) elem2;
    return *p1%10-*p2%10;
}
int main()
{
    int arr[NUM]={1,55,7,192,32};
    qsort(arr,NUM,sizeof(int),MyCompare);
    for(int i=0;i<NUM;i++){
        cout<<arr[i]<<" ";
    }
    return 0;
}

2.从大到小排序

#include <iostream>
#include <stdlib.h>
using namespace std;
#define NUM 5
int MyCompare(const void * elem1, const void * elem2)
{
    int * p1 = (int *)elem1;
    int * p2 = (int *)elem2;
    return *p2-*p1;
}
int main()
{
    int arr[NUM]={5,88,2,13,71};
    qsort(arr,NUM,sizeof(int), MyCompare);
    for(int i=0;i<NUM;i++)
        cout<<arr[i]<<" ";
    return 0;
}

3.给结构体student排序

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define NUM 5
struct Student{
    unsigned ID;
    char szName[20];
    float fGPA;
};
Student MyClass[NUM]={
    {1234,"Tom",3.78},
    {1345,"Sam",2.12},
    {1235,"Bob",4.77},
    {1456,"Echo",1.34},
    {1578,"Amy",3.04},
};

int IDCompare(const void * elem1,const void * elem2)
{
    Student * stu1 = (Student*)elem1;
    Student * stu2 = (Student*)elem2;
    return (*stu1).ID-(*stu2).ID;
}
int NameCompare(const void * elem1, const void * elem2)
{
    Student * stu1 = (Student*)elem1;
    Student * stu2 = (Student*)elem2;
    return strcmp(stu1->szName,stu2->szName);
}
int main()
{
    qsort(MyClass,NUM,sizeof(Student),IDCompare);
    for(int i=0;i<NUM;i++)
        cout<<MyClass[i].szName<<" ";
    cout<<endl;
    qsort(MyClass,NUM,sizeof(Student),NameCompare);
    for(int i=0;i<NUM;i++)
        cout<<MyClass[i].szName<<" ";
    return 0;
}

4.自定制qsort

内部采用冒泡排序

#include <iostream>
#include <math.h>
using namespace std;
#define NUM 5
int MyCompare(const void * elem1, const void * elem2)
{
    int * p1 = (int *)elem1;
    int * p2 = (int *)elem2;
    return *p1-*p2;
}
void Myqsort(void * arr,int num,int size,int (*pfcompare)(const void * ele1, const void * ele2))
{
    char * a = (char *)arr;
    //冒泡排序
    for(int i=num-1;i>0;i--){
        for(int j=0;j<i;j++){
            char * p1 = a + j*size;
            char * p2 = a + (j+1)*size;
            if(pfcompare(p1,p2)>0)
                for(int k=0;k<size;k++)
                    swap(*(p1+k),*(p2+k));
        }
    }
}
int main()
{
    int arr[NUM]={5,37,2,18,99};
    Myqsort(arr,NUM,sizeof(int),MyCompare);
    for(int i=0;i<NUM;i++)
        cout<<arr[i]<<" ";
    return 0;
}

 

  • 35
    点赞
  • 98
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值