線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1316
推到 Plurk!
推到 Facebook!

請問stdlib之qsort的語法

答題得分者是:turboted
williamfox
一般會員


發表:14
回覆:15
積分:5
註冊:2002-08-05

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-05-26 02:35:31 IP:61.230.xxx.xxx 未訂閱
各位安: 小弟在書上發現quicksort在stdlib.h裡面有提供    可是小弟看不懂語法,可以請知道的朋友,教小弟一下嗎??     發表人 - williamfox 於 2003/05/26 04:43:15
turboted
版主


發表:95
回覆:754
積分:452
註冊:2002-07-23

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-05-26 06:26:22 IP:218.166.xxx.xxx 未訂閱
quicksort原理 http://www.cyut.edu.tw/~ckhung/olbook/algo/sort1.shtml quicksort 例子 http://alpha.luc.ac.be/~gjb/MIT-C/slides/quicksort.html    bcb help-online說明 Syntax    #include  void qsort(void *base, size_t nelem, size_t width, int (_USERENTRY *fcmp)(const void *, const void *)); Description Sorts using the quicksort algorithm. qsort is an implementation of the "median of three" variant of the quicksort algorithm. qsort sorts the entries in a table by repeatedly calling the user-defined comparison function pointed to by fcmp. base points to the base (0th element) of the table to be sorted. nelem is the number of entries in the table. width is the size of each entry in the table, in bytes. fcmp, the comparison function, must be used with the _USERENTRY calling convention. fcmp accepts two arguments, elem1 and elem2, each a pointer to an entry in the table. The comparison function compares each of the pointed-to items (*elem1 and *elem2 ), and returns an integer based on the result of the comparison. *elem1 < *elem2 fcmp returns an integer < 0 *elem1 == *elem2 fcmp returns 0 *elem1 > *elem2 fcmp returns an integer > 0 In the comparison, the less-than symbol (<) means the left element should appear before the right element in the final, sorted sequence. Similarly, the greater-than (>) symbol means the left element should appear after the right element in the final, sorted sequence. Return Value None. bcb5 help-online例子 #include #include #include int sort_function( const void *a, const void *b); char list[5][4] = { "cat", "car", "cab", "cap", "can" }; int main(void) { int x; qsort((void *)list, 5, sizeof(list[0]), sort_function); for (x = 0; x < 5; x ) printf("%s\n", list[x]); return 0; } int sort_function( const void *a, const void *b) { return( strcmp((char *)a,(char *)b) ); } 發表人 - turboted 於 2003/05/26 06:33:52 發表人 - turboted 於 2003/05/26 06:34:59
williamfox
一般會員


發表:14
回覆:15
積分:5
註冊:2002-08-05

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-05-26 10:14:49 IP:61.230.xxx.xxx 未訂閱
謝謝你嚕,小弟了啦~~
系統時間:2024-11-23 13:31:39
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!