[C/C++语法]—qsort及sort函数

C语言和C++中,对sort函数的使用不同。C语言中没有预制的sort函数,如果在c语言中,要调用sort函数,就需要自定义一个用于排序的函数,或者使用c语言自有的qsort函数,其头文件为stdlib.h。而C++中qsort和sort函数皆可使用

qsort函数

qsort的头文件为<stdlib.h>,为C语言中的函数
运用基本的快速排序的方法,时间复杂度为O(n*logn),来了解一下qsort函数如何使用叭
函数原型:void qsort( void *base, size_t num, size_t width, int (__cdecl *compare );
比较函数原型:int compare (const void *elem1, const void *elem2 ) );
参数含义:

  1. 待比较数组的首地址
  2. 待比较元素个数
  3. 待比较的每个元素的大小
  4. 指向比较函数的指针(qsort函数中一定需要自己写)
    比较函数的参数含义:
  5. 待比较数组首地址
  6. 待比较数组的尾地址

对int类型数组升序排序

int num[105];
int cmp(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;//(int *)a是将指针转为int类型,而*(int *)a是取出指针a中的值,升序排列
}
qsort(num, 100, sizeof(num[0]), cmp);

如果cmp函数返回值为正数,则将第一个参数放在第二个参数后面 如果cmp函数返回值为 等于0,则a, b不动,若返回值为负数,则将第一个参数放在第二个参数前面
若想降序排列,则return *(int *)b - *(int *)a;

对char类型数组升序排序

char a[105];
int cmp(const void *a, const void *b)
{
    return *(char *a) - *(char *b);
}
qsort(a, 100, sizeof(a[0]), cmp);

对double类型数组升序排序(特别要注意)

double a[105];
int cmp(const void *a, const void *b)
{
    return *(double *)a > *(double *b) ? 1:-1;
}
qsort(a, 100, sizeof(a[0]), cmp);

*(double *)a - *(double *b)为大于0小于1的小数,则会返回0,于是不会移动位置

对结构体排序

struct node{
    int num;
    char name;
    double sum;
}a[105];

若想先对num升序排列,在对name降序排列,在对sum升序排列

struct node{
    int num;
    char name;
    double sum;
}a[105];
int cmp(struct node *a, struct node *b)
{
    if(a->num != b->num) return a->num - b->num;
    else if(a->name != b->name) return b->name - a->name;
    else if(a->sum != b->sum) return a->sum > b->sum ? 1 : -1;
}
qsort(a, 100, sizeof(a[0]), cmp)

结构体中->运算符代表将结构体指针中的某一变量值取出
而 . 运算符代表将结构体中的某一变量取出

sort函数与sort函数中cmp函数的区别

名称 返回值类型 语句
qsort( c ) int return a - b
sort(c++) bool return a > b

sort函数

sort为C++中的函数,sort 使用时得注明:using namespace std;还得加上#include< algorithm >
sort函数是qsort函数的升级版,能用sort函数尽量用sort函数,sort函数仅需要2-3个参数
第一个参数为起始元素的地址
第二个参数为末尾元素的地址
第三个参数为自定义的比较函数,若没有第三个参数,则默认升序排序

对int类型数组降序排序

int a[105];
bool cmp(int a, int b)
{
    return a > b //sort中使用 > 而不是 - 
}
sort(a, a+100, cmp);

cmp的返回值为1,则第一个参数放在第二个参数前面
若为0,则第一个参数放在第二个参数后面 所以不使用 -
简便记法写返回值时第一个参数在前,第二个参数在后 若为 > 则降序排列(前面的数大于后面的数同5>4>3>2>1), < 则为升序排列(后面的数大于前面的数同1<2<3<4<5)

对char类型数组降序排列

char a[105];
bool cmp(char a, char b)
{
    return a > b //sort中使用 > 而不是 - 
}
sort(a, a+100, cmp);

对double类型降序排列

double a[105];
bool cmp(double a, double b)
{
    return a > b //sort中使用 > 而不是 - 
}
sort(a, a+100, cmp);

对字符串降序排列

char str[105][105];
bool cmp(char a, char b)
{
    return a > b;
}
sort(str, str+100, cmp);

对结构体举例

先对num降序排序,在对name降序排序

struct node{
    int num;
    char name;
}a[105];
bool cmp(struct node a, struct node b)
{
    if(node a.num != node b.num) return a.num > b.num;
    if(node a.name != node b.name) return a.name < b.name;
}
sort(a, a+100, cmp)

两个参数举例

#include <iostream>
#include <algorithm>
using namespace std;

int main(void)
{
    int a[105] = {2,4,1,23,5,76,0,43,24,65};
    for(int i = 0; i < 10; i++)
        cout<<a[i]<<endl;
    sort(a, a+10);
    for(int i = 0; i < 10; i++)
        cout<<a[i]<<endl;
    return 0;
}

输出为:

三个参数举例

对a升序排列,对b降序排列,对c降序排列

struct node{
    int a;
    int b;
    double c;
}arr[105];
bool cmp(node x, node y)
{
     if(x.a != y.a)  return x.a < y.a;
     if(x.b != y.b)  return x.b > y.b;
     return x.c > y.c;
}
sort(arr, arr+100, cmp);

例题 hdu 2037

题目描述

“今年暑假不AC?”
“是的。”
“那你干什么呢?”
“看世界杯呀,笨蛋!”
“@#$%^&*%…”

确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。
作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)

Input

输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。

Output

对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。

Sample Input

12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0

Sample Output

5

解题思路

定义一个结构体变量,分别定义开始时间,结束时间
先将结束时间升序排列,若结束时间相同,则按开始时间,降序排列。
写一循环,如果前一节目结束时间小于等于后一节目开始时间,则观看该节目否则进行下一节目。
代码

#include <stdio.h>
#include <algorithm>
using namespace std;

struct node{
    int start;//开始时间
    int t;//结束时间
}a[105];
bool cmp(node a, node b)
{
    if(a.t == b.t) //结束时间相同时
        return a.start > b.start; //开始时间降序排列
    return a.t < b.t;//结束时间不同时,结束时间升序排列
}
int main(void)
{
    int n, ans, num;
    while(~scanf("%d", &n))
    {
        if(n == 0) break;
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d", &a[i].start, &a[i].t);
        }
        ans = 0;//统计节目个数
        sort(a, a+n, cmp);
        num = a[0].t;
        for(int i = 1; i < n; i++)
        {
            if(a[i].start >= num)
            {
                ans++;
                num = a[i].t;
            }
        }
        printf("%d\n", ans+1);
    }
}

(个人学习使用)

发布了20 篇原创文章 · 获赞 2 · 访问量 943

猜你喜欢

转载自blog.csdn.net/zhbbbbbb/article/details/103455817