qsort在中排序函數(shù)的使用應用_第1頁
qsort在中排序函數(shù)的使用應用_第2頁
qsort在中排序函數(shù)的使用應用_第3頁
qsort在中排序函數(shù)的使用應用_第4頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、在c+中 qsort()排序函數(shù)的使用 qsort 函數(shù)應用大qsort本文中排序都是采用的在c+中 qsort()排序函數(shù)的使用 qsort 函數(shù)應用大qsort *(double *)b ? 1 : -四、對結構體一級排struct double cmp( const void *a ,const void return (*(In *)a)-data (*(In *)B)-data ? 1 : -五、對結構體二級排struct xxycmp( const void , const void )structIn*c=(In struct In *d = if(c-x 五、對結構體二級排st

2、ruct xxycmp( const void , const void )structIn*c=(In struct In *d = if(c-x d-c-x - d-d-y - c-六、對字符串進行排struct char strcmp ( const void *a , const )return strcmp( (*(In *)a)-str *)B)-七、計算幾何中求凸包的 structpo struct void *a,const void cmp1x,c-y,p1.x,p1.y) dis(d-x,d-y,p1.x,p1.y) / 如果在一條直線上,則把遠的放return else

3、return -面c+中加載頭文件 cqsort的頭文件里,strcmp/*qsortP.S.:qsortn*log(n),voidqsort(void*base,size_tnelem,size_t(*Comp)(constvoidc+中加載頭文件 cqsort的頭文件里,strcmp/*qsortP.S.:qsortn*log(n),voidqsort(void*base,size_tnelem,size_t(*Comp)(constvoid*,const為數(shù)組元素的大?。ǎ?Comp)(constvoid*p1,constvoid*p2)p1p2,函數(shù)返回-1;ab,1;a=b0。例子

4、 1:對一維數(shù)組進行#include #include Comp(const void *p1,const void *p2 return *)p1) - qsort(list, 10,sizeof(f(%d return 0;例子 2:對字符串進行排#include #include #include Comp(const void *p1,const void return strcmp(char *)p1,(char char return 0;例子 3:按結構體中某個關鍵字排序(#include Comp(const void *p1,const void return strcmp(

5、char *)p1,(char char return 0;例子 3:按結構體中某個關鍵字排序(對結構體一級排序#include #include struct cmp(const void *a, const void return (struct ab *)a)-vote1-(struct ab *)b)-i = 0; i 5; qsort(void f(%d return 0;例子 4:按結構體中多個關鍵字排序(對結構體多級排序)以二級為#include #include struct abcmp(const void *a, const void if(struct ab *)a)-x

6、!=(struct ab *)b)-x) return (struct ab *)a)-x-(struct return (struct ab *)a)-y-(struct *)b)-*)b)-i = 0; i 5; qsort(void #include #include struct abcmp(const void *a, const void if(struct ab *)a)-x!=(struct ab *)b)-x) return (struct ab *)a)-x-(struct return (struct ab *)a)-y-(struct *)b)-*)b)-i = 0;

7、i 5; qsort(void f(%d %d return 0;n 對數(shù)(a,b)a a b 1 1 3 6 le#include #include usingstructleOutput 1 31 3 6 cmp(constvoid*,constvoidargc,charfor(i=0;icmp(constvoid*,constvoidargc,charfor(i=0;iqsort(voidfor(i=0;icoutnumi.aa!=(structnumber*)y)-return(structnumber*)x)-a-(structnumber*)y)-a; return(structn

8、umber*)y)-b-(structnumber*)x)-例子 5:對結構體中字符串進行排struct char cmp(const void *a, const void return strcmp(struct ab *)a)-ac,(struct ab *)b)-i = 0; i 5; qsort(void return 0;6、計算幾何中求凸包的 Comp(const void *p1,const void *p2)p1struct po struct po if(cacl(*c,i = 0; i 5; qsort(void return 0;6、計算幾何中求凸包的 Comp(con

9、st void *p1,const void *p2)p1struct po struct po if(cacl(*c,x,c-x,d-y,p1.x,p1.y ) return 1;return -面P.S.:qsortCstdlib.hn*log(n),voidqsort(void*base,size_tnelem,size_t(*Comp)(constvoid*,const為數(shù)組元素的大?。ǎ?Comp)(constvoid*p1,constvoid*p2)p1p2,函數(shù)返回-1;ab,1;a=b0。又見#include #include #include struct Nodechar

10、Name100; CompY(const struct *p1,const void *p2)/year*c = (Node structNode*d=(Node if(c-Year!=d-Year) else return c-Year-d-*)p1).Name,(*(Node return strcmp(*(Node *)p1).Name,(*(Node else c-Price-d-*p1,const void *p2)/#include #include struct NodecharName100; CompY(const struct *p1,const void *p2)/yea

11、r*c = (Node structNode*d=(Node if(c-Year!=d-Year) else return c-Year-d-*)p1).Name,(*(Node return strcmp(*(Node *)p1).Name,(*(Node else c-Price-d-*p1,const void *p2)/price*c = (Node *d = (Node != d-Price) return c-Price-d-if(c-else if(strcmp(*(Node *)p1).Name,(*(Node *)p2).Name) returnstrcmp(*(Node*)

12、p1).Name,(*(Node*)p2).Name);else c-Year-d-*p1,const void *p2)/name*c = (Node *d = (Node if(strcmp(*(Node *)p1).Name,(*(Node return strcmp(*(Node *)p1).Name,(*(Node if(c-Year!=d-Year)returnc-Year-d-Year; return c-Price-d-Price;/*void f(%s %d char qsort(Book, n, else qsort(Book, qsort(Book, char qsort(Book, n, else qsort(Book, qsort(Book, return #include #include struct abcmp1(const return cmp2(const return *a, void ab *)a)-vote1-(struct ab *)b)-*a,const void ab *)a)-vote2-(struct ab *)b)-for(i=0;ij;i+)scanf(%d%d,&abci.vo

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論