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;
}
總結
以上是生活随笔為你收集整理的c语言qsort函数简介,C语言排序函数—qsort函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WorkNC应用技巧分享—尖角清根时获得
- 下一篇: 卡尔曼滤波器 误差协方差矩阵