c语言qsort函数简介,C语言排序函数—qsort函数

前言:

在一些编程题中经常需要你按照某个指标按照从小到大或从大到小输出一些数据,这时你可以自己写一个排序函数进行排序,但是其实C语言函数库中就有一个排序函数——qsort函数,使用它可以节省你单独写排序函数所耗费的时间,因而在一些比赛中广泛应用。

qsort函数介绍:

定义于头文件

void qsort( void *ptr, size_t count, size_t size,int (*comp)(const void *, const void *) );

对 ptr 所指向的数组以升序排序。数组包含 count 个长度为 size 字节的元素。用 comp 所指向的函数比较对象。

同 (1) ,除了传递给 comp 附加环境参数 context ,还会在运行时检测下列错误,并调用当前安装的制约处理函数:

count 或 size 大于 RSIZE_MAX

key 、 ptr 或 comp 是空指针(除非 count 为零)

同所有边界检查函数, qsort_s 仅若实现定义了 STDC_LIB_EXT1 ,且用户在包含 stdlib.h 前定义 STDC_WANT_LIB_EXT1 为整数常量 1 才保证可用。

若 comp 指示两元素相等,则它们排序后的结果是未指定的。

参数

ptr - 指向待排序的数组的指针

count - 数组的元素数目

size - 数组每个元素的字节大小

comp - 比较函数。若首个参数小于第二个,则返回负整数值,若首个参数大于第二个,则返回正整数值,若两参数相等,则返回零。

比较函数的签名应等价于如下形式:

int cmp(const void *a, const void *b);

该函数必须不修改传递给它的对象,而且在调用比较相同对象时必须返回一致的结果,无关乎它们在数组中的位置。

context - 附加信息(例如,对照序列),作为第三个参数传递给 comp

返回值

(无)

成功时为零,若检测到运行时制约违规,则为非零

注意

与名称无关,C 和 POSIX 标准都未要求此函数用快速排序实现,也未保证任何复杂度或稳定性。

qsort 的用户通常用全局变量来将附加语境传递给比较函数。

应用举例:

例一·1019 数字黑洞

给定任一个各位数字不完全相同的 4 位正整数,如果我们先把 4 个数字按非递增排序,再按非递减排序,然后用第 1 个数字减第 2 个数字,将得到一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的 6174,这个神奇的数字也叫 Kaprekar 常数。

例如,我们从6767开始,将得到

7766 - 6677 = 1089

9810 - 0189 = 9621

9621 - 1269 = 8352

8532 - 2358 = 6174

7641 - 1467 = 6174

… …

现给定任意 4 位正整数,请编写程序演示到达黑洞的过程。

输入格式:

输入给出一个 (0,10^​4) 区间内的正整数 N。

输出格式:

如果 N 的 4 位数字全相等,则在一行内输出 N - N = 0000;否则将计算的每一步在一行内输出,直到 6174 作为差出现,输出格式见样例。注意每个数字按 4 位数格式输出。

输入样例 1:

6767

输出样例 1:

7766 - 6677 = 1089

9810 - 0189 = 9621

9621 - 1269 = 8352

8532 - 2358 = 6174

输入样例 2:

2222

输出样例 2:

2222 - 2222 = 0000

思路:

要用不同的数字组成一个最大和最小的数就可以用qsort函数对分离出来的各个数字进行排序组合

AC的代码:

// Date:2020/05/05

// Author:xiezhg5

#include

#include

int cmp(const void *a,const void *b) {

return *(int *)b-*(int *)a;

}

int main(void) {

int str[4];

int max,min;

int right=0;

int num;

scanf("%d",&num);

if(num%1111==0) {

printf("%04d - %04d = 0000",num,num);

return 0;

}

while(right!=6174) {

str[0]=num/1000;

str[1]=num/100%10;

str[2]=num/10%10;

str[3]=num%10;

qsort(str,4,sizeof(str[0]),cmp);

max=str[0]*1000+str[1]*100+str[2]*10+str[3];

min=str[3]*1000+str[2]*100+str[1]*10+str[0];

right=max-min;

printf("%04d - %04d = %04d\n",max,min,right);

num=right;

}

return 0;

}

例二·Sorting students on grades

Description

Rewrite Listing 6.12, GradeExam.cpp, to display the students in increasing order of

the number of correct answers.

Suppose the answers for all students are stored in a two-dimensional array.

Each row

records an student’s ten answers with ten columns.

For example, the following array

stores the answers for 3 students.

0 1 2 3 4 5 6 7 8 9

Student 0 A B A C C D E E A D

Student 1 D B D C C D A E A D

Student 2 E D D A C B E E A D

The key is stored in a one-dimensional array, as follows:

0 1 2 3 4 5 6 7 8 9

Key D B D C C D A E A D

Input

The first line is a positive integer t for the number of test cases.

Each test case contains m+2 lines.

The line 1 contains an integer m (0Then followed m lines, each line contains 10 integers seperated by blanks, for one student’s answers.

And the following line contains 10 keys of correct answers.

Output

For each test case,output each student’s number and the number of correct answers in increasing order of the number of correct answers. Use the format like the sample output.

Sample Input

2

3

A B A C C D E E A D

D B D C C D A E A D

E D D A C B E E A D

D B D C C D A E A D

2

B B E C C D E E A D

A B D C C D E E A D

A B D C C D E E B D

Sample Output

test case 1:

Student 2: 5

Student 0: 7

Student 1: 10

test case 2:

Student 0: 7

Student 1: 9

Problem Source: 程序设计I Chapter6 Arrays

// Date:2020/4/24

// Author:xiezhg5

#include

#include

typedef struct NODE {

int num;

int count;

} NODE; //定义结构体变量NODE

//等下调用排序函数qsort

int cmp(const void *p1, const void *p2) {

NODE *c = (NODE*)p1;

NODE *d = (NODE*)p2;

if(c->count != d->count)

return c->count - d->count; //按成绩降序排列

else

return c->num - d->num; //按序号降序排列

}

int main(void) {

int m;

scanf("%d\n",&m);

for(int k=0; k

int n;

scanf("%d\n",&n);

struct NODE student[101]; //创建studen结构体

char score[n+1][10]; //这是记录成绩的数组

for(int i=0; i

for(int j=0; j<10; j++) {

scanf("%c ",&score[i][j]);

}

}

for(int i=0; i

int grade=0;

for(int j=0; j<10; j++) {

if(score[i][j]==score[n][j]) grade++;

}

student[i].num=i;

student[i].count=grade;

}

//调用qsort函数排序

qsort(student,n,sizeof(NODE),cmp);

printf("test case %d:\n",k+1);

for(int i=0; i < n; i++) {

printf("Student %d: %d\n",student[i].num,student[i].count);

}

}

return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值