C語言中使用qsort函數(shù)對自定義結(jié)構(gòu)體數(shù)組進行排序
使用qsort函數(shù)對自定義結(jié)構(gòu)體數(shù)組進行排序
qsort進行排序的數(shù)組存儲的不能是結(jié)構(gòu)體的指針,需要是結(jié)構(gòu)體本身。
結(jié)構(gòu)體
struct student{ ? ? char* id; ? ? int mark; }arr[4], test0={"0001",80}, test1={"0002",90}, test2={"0003",60}, test3={"0004",61} ;
排序函數(shù)
int cmp(const void *a, const void *b){ ? ? int mark1=((struct student *)a)->mark; ? ? int mark2=((struct student *)b)->mark; ? ? return mark1>mark2 ? 1:-1; }
總體代碼
#include <stdio.h> struct student{ ? ? char* id; ? ? int mark; }arr[4], test0={"0001",80}, test1={"0002",90}, test2={"0003",60}, test3={"0004",61} ; int cmp(const void *a, const void *b){ ? ? int mark1=((struct student *)a)->mark; ? ? int mark2=((struct student *)b)->mark; ? ? return mark1>mark2 ? 1:-1; } int main(){ ? ? arr[0]=test0;arr[1]=test1;arr[2]=test2;arr[3]=test3; ? ? printf("—————排序前—————\n"); ? ? for(int i=0; i<4; i++) ? ? ? ? printf("%s %d\n",arr[i].id,arr[i].mark); ? ? qsort(arr,4,sizeof(struct student),cmp); ? ? printf("—————排序后—————\n"); ? ? for(int i=0; i<4; i++) ? ? ? ? printf("%s %d\n",arr[i].id,arr[i].mark); ? ? return 0; }
結(jié)果
—————排序前—————
0001 80
0002 90
0003 60
0004 61
—————排序后—————
0003 60
0004 61
0001 80
0002 90
C語言 qsort()函數(shù)詳解
1、qsort概念介紹
qsort()函數(shù)(quick sort)是八大排序算法中的快速排序,能夠排序任意數(shù)據(jù)類型的數(shù)組其中包括整形,浮點型,字符串甚至還有自定義的結(jié)構(gòu)體類型。
2、qsort()函數(shù)實現(xiàn)(循序漸進式講解)
2.1 qsort()函數(shù)
qsort()函數(shù)函數(shù)函數(shù)參數(shù):
void qsort? (void* base //待排序數(shù)據(jù)的起始地址 ?size_t num, //待排序數(shù)據(jù)的元素個數(shù) ?size_t size,//待排序數(shù)據(jù)中一個元素的大?。▎挝唬鹤止?jié)) ?int (*compar)(const void*,const void*)//比較兩個元素大小的函數(shù)指針 );
函數(shù)中第一個函數(shù)參數(shù)(void)的類型非常奇妙,因為待排序數(shù)據(jù)的元素類型可能是整形、浮點型、字符型、結(jié)構(gòu)體……而void類型就像一個宰相(宰相肚子能撐船)不管你傳過來的地址是何類型,我都可以積極的收納。
函數(shù)中第四個函數(shù)參數(shù)是最難理解的。對于整形數(shù)據(jù)我們可以通過大小號來比較,對于字符型數(shù)據(jù)可以用strcmp來比較,但是,我們遇到結(jié)構(gòu)體就出現(xiàn)了問題,結(jié)構(gòu)體中包含的類型多,我們就需要因材施教。
int (*compar)(const void*p1,const void*p2)//比較兩個元素大小的函數(shù)指針 );
返回值 | 含義 |
return -1 | p1<p2 |
return 0 | p1=p2 |
return 1 | p1>p2 |
2.2 qsort()函數(shù)實現(xiàn)過程
觀察冒牌排序:
可得出如下過程:
整形:
//測試qsort()函數(shù)功能 int cmp_int(const void* e1, const void* e2) { ?? ?return ?*(int*)e1 - *(int*)e2; } void test() { ?? ?int arr[] = { 9,8,7,6,5,4,3,2,1,0 }; ?? ?int sz = sizeof(arr) / sizeof(arr[0]); ?? ?qsort(arr, sz, sizeof(arr[0]), cmp_int); ?? ?for (int i = 0; i < sz; i++) ?? ?{ ?? ??? ?printf("%d ", arr[i]);//打印排列好的數(shù)組 ?? ?} } ? int main() { ?? ? ?? ?test(); ?? ? ?? ?return 0; }
結(jié)構(gòu)體:
struct Stu { ?? ?char name[20] = {0}; ?? ?int age = 0; }; int cmp_by_name(const void* e1, const void* e2) { ?? ?return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name); } void test() { ?? ?struct Stu s[] = { {"zhangsan",10},{"lishi",20}}; ?? ?qsort(s, sizeof(s), sizeof(s->name),cmp_by_name); ?? ?printf("%s ", s->name); } ? int main() { ?? ? ?? ?test(); ?? ? ?? ?return 0; }
3、小結(jié)
有了qsort()函數(shù)能有節(jié)省不少時間,后期我會出一起通訊錄再詳細的介紹qsort()函數(shù)的使用。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解C++中的內(nèi)存同步模式(memory order)
這篇文章主要介紹了C++中的內(nèi)存同步模式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04C/C++ 中怎樣使用SetConsoleTextAttribute()函數(shù)來控制輸出字符的顏色
這篇文章主要介紹了C/C++ 中如何使用SetConsoleTextAttribute()函數(shù)來控制輸出字符的顏色,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03C++中的局部變量、全局變量、局部靜態(tài)變量、全局靜態(tài)變量的區(qū)別
本文主要介紹了C++中的局部變量、全局變量、局部靜態(tài)變量、全局靜態(tài)變量的區(qū)別。具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02C++報錯`Null Pointer Dereference`的解決方法
在軟件開發(fā)中,Null Pointer Dereference 是一種常見的錯誤,它發(fā)生在程序試圖訪問或操作一個空指針指向的內(nèi)存位置時,這種情況通常會導致程序崩潰,給 debug 工作帶來很大困擾,今天,我們將探討如何解決 Null Pointer Dereference 報錯,需要的朋友可以參考下2024-07-07