c语言qsort函数简介,C语言排序函数—qsort函数
前言:
在一些編程題中經(jīng)常需要你按照某個(gè)指標(biāo)按照從小到大或從大到小輸出一些數(shù)據(jù),這時(shí)你可以自己寫一個(gè)排序函數(shù)進(jìn)行排序,但是其實(shí)C語言函數(shù)庫中就有一個(gè)排序函數(shù)——qsort函數(shù),使用它可以節(jié)省你單獨(dú)寫排序函數(shù)所耗費(fèi)的時(shí)間,因而在一些比賽中廣泛應(yīng)用。
qsort函數(shù)介紹:
定義于頭文件
void qsort( void *ptr, size_t count, size_t size,int (*comp)(const void *, const void *) );
對(duì) ptr 所指向的數(shù)組以升序排序。數(shù)組包含 count 個(gè)長(zhǎng)度為 size 字節(jié)的元素。用 comp 所指向的函數(shù)比較對(duì)象。
同 (1) ,除了傳遞給 comp 附加環(huán)境參數(shù) context ,還會(huì)在運(yùn)行時(shí)檢測(cè)下列錯(cuò)誤,并調(diào)用當(dāng)前安裝的制約處理函數(shù):
count 或 size 大于 RSIZE_MAX
key 、 ptr 或 comp 是空指針(除非 count 為零)
同所有邊界檢查函數(shù), qsort_s 僅若實(shí)現(xiàn)定義了 STDC_LIB_EXT1 ,且用戶在包含 stdlib.h 前定義 STDC_WANT_LIB_EXT1 為整數(shù)常量 1 才保證可用。
若 comp 指示兩元素相等,則它們排序后的結(jié)果是未指定的。
參數(shù)
ptr - 指向待排序的數(shù)組的指針
count - 數(shù)組的元素?cái)?shù)目
size - 數(shù)組每個(gè)元素的字節(jié)大小
comp - 比較函數(shù)。若首個(gè)參數(shù)小于第二個(gè),則返回負(fù)整數(shù)值,若首個(gè)參數(shù)大于第二個(gè),則返回正整數(shù)值,若兩參數(shù)相等,則返回零。
比較函數(shù)的簽名應(yīng)等價(jià)于如下形式:
int cmp(const void *a, const void *b);
該函數(shù)必須不修改傳遞給它的對(duì)象,而且在調(diào)用比較相同對(duì)象時(shí)必須返回一致的結(jié)果,無關(guān)乎它們?cè)跀?shù)組中的位置。
context - 附加信息(例如,對(duì)照序列),作為第三個(gè)參數(shù)傳遞給 comp
返回值
(無)
成功時(shí)為零,若檢測(cè)到運(yùn)行時(shí)制約違規(guī),則為非零
注意
與名稱無關(guān),C 和 POSIX 標(biāo)準(zhǔn)都未要求此函數(shù)用快速排序?qū)崿F(xiàn),也未保證任何復(fù)雜度或穩(wěn)定性。
qsort 的用戶通常用全局變量來將附加語境傳遞給比較函數(shù)。
應(yīng)用舉例:
例一·1019 數(shù)字黑洞
給定任一個(gè)各位數(shù)字不完全相同的 4 位正整數(shù),如果我們先把 4 個(gè)數(shù)字按非遞增排序,再按非遞減排序,然后用第 1 個(gè)數(shù)字減第 2 個(gè)數(shù)字,將得到一個(gè)新的數(shù)字。一直重復(fù)這樣做,我們很快會(huì)停在有“數(shù)字黑洞”之稱的 6174,這個(gè)神奇的數(shù)字也叫 Kaprekar 常數(shù)。
例如,我們從6767開始,將得到
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
… …
現(xiàn)給定任意 4 位正整數(shù),請(qǐng)編寫程序演示到達(dá)黑洞的過程。
輸入格式:
輸入給出一個(gè) (0,10^?4) 區(qū)間內(nèi)的正整數(shù) N。
輸出格式:
如果 N 的 4 位數(shù)字全相等,則在一行內(nèi)輸出 N - N = 0000;否則將計(jì)算的每一步在一行內(nèi)輸出,直到 6174 作為差出現(xiàn),輸出格式見樣例。注意每個(gè)數(shù)字按 4 位數(shù)格式輸出。
輸入樣例 1:
6767
輸出樣例 1:
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
輸入樣例 2:
2222
輸出樣例 2:
2222 - 2222 = 0000
思路:
要用不同的數(shù)字組成一個(gè)最大和最小的數(shù)就可以用qsort函數(shù)對(duì)分離出來的各個(gè)數(shù)字進(jìn)行排序組合
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: 程序設(shè)計(jì)I Chapter6 Arrays
// Date:2020/4/24
// Author:xiezhg5
#include
#include
typedef struct NODE {
int num;
int count;
} NODE; //定義結(jié)構(gòu)體變量NODE
//等下調(diào)用排序函數(shù)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; //按成績(jī)降序排列
else
return c->num - d->num; //按序號(hào)降序排列
}
int main(void) {
int m;
scanf("%d\n",&m);
for(int k=0; k
int n;
scanf("%d\n",&n);
struct NODE student[101]; //創(chuàng)建studen結(jié)構(gòu)體
char score[n+1][10]; //這是記錄成績(jī)的數(shù)組
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;
}
//調(diào)用qsort函數(shù)排序
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;
}
總結(jié)
以上是生活随笔為你收集整理的c语言qsort函数简介,C语言排序函数—qsort函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WorkNC应用技巧分享—尖角清根时获得
- 下一篇: 卡尔曼滤波器 误差协方差矩阵