C语言函数qsort中compare详细用法

bayhax 2016-11-09 10:56:22
如题,在compare中。用
/*int x = *(const int *)p;
int y = *(const int *)q;
if (x < y) {
return -1;
} else if (x > y) {
return 1;
}
return 0;*/
和return (*(int *)p-*(int *)q);
的效果不是一样的吗?我想让他升序排序,这不都是升序排序的吗?
能不能给窝详细讲讲呢?我都不懂什么意思,这个qsort用法的具体意思
还有排序这个算法给我普及普及吧,谢谢谢








#include <stdio.h>
#include<stdlib.h>
#define MAXN 10
typedef float ElementType;
int compare (const void *p, const void *q) {
/*int x = *(const int *)p;
int y = *(const int *)q;
if (x < y) {
return -1;
} else if (x > y) {
return 1;
}
return 0;*/
return (*(int *)p-*(int *)q);
}
ElementType Median( ElementType A[], int N );

int main ()
{
ElementType A[MAXN];
int N, i;

scanf("%d", &N);
for ( i=0; i<N; i++ )
scanf("%f", &A[i]);
printf("%.2f\n", Median(A, N));

return 0;
}
/*ElementType Median(ElementType A[],int N)
{
int i,j,m;
ElementType temp;
for(i=0;i<N-1;i++)
for(j=i+1;j<N;j++)
if(A[i]>A[j])
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
return A[N/2];
}*/
ElementType Median(ElementType A[],int N)
{
qsort(A, N, sizeof(ElementType), compare);
int i;
for(i=0;i<N;i++)
{
printf("%f ",A[i]);
}
return A[N/2];
}
...全文
1117 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jingxingxiaozhang 2016-11-11
  • 打赏
  • 举报
回复
int compare (const void *p, const void *q) { /*int x = *(const int *)p; int y = *(const int *)q; if (x < y) { return -1; 如果x<y那么返回-1 } else if (x > y) { return 1; 如果x>y那么返回1 } return 0;*/ 相等等于0 return (*(int *)p-*(int *)q); } 这个函数就是利用返回值来查看x和y的大小关系 调用compare函数后,如果返回值是 -1那么x<y 1那么x>y 0那么相等。 函数返回值做验证参数
AlbertS 2016-11-10
  • 打赏
  • 举报
回复
应该说是一样的,这个compare函数就是告诉它两个数的大小关系就行
赵4老师 2016-11-10
  • 打赏
  • 举报
回复
参考C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\qsort.c
赵4老师 2016-11-10
  • 打赏
  • 举报
回复
qsort Performs a quick sort. void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *elem1, const void *elem2 ) ); Routine Required Header Compatibility qsort <stdlib.h> and <search.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value None Parameters base Start of target array num Array size in elements width Element size in bytes compare Comparison function elem1 Pointer to the key for the search elem2 Pointer to the array element to be compared with the key Remarks The qsort function implements a quick-sort algorithm to sort an array of num elements, each of width bytes. The argument base is a pointer to the base of the array to be sorted. qsort overwrites this array with the sorted elements. The argument compare is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. qsort calls the compare routine one or more times during the sort, passing pointers to two array elements on each call: compare( (void *) elem1, (void *) elem2 ); The routine must compare the elements, then return one of the following values: Return Value Description < 0 elem1 less than elem2 0 elem1 equivalent to elem2 > 0 elem1 greater than elem2 The array is sorted in increasing order, as defined by the comparison function. To sort an array in decreasing order, reverse the sense of “greater than” and “less than” in the comparison function. Example /* QSORT.C: This program reads the command-line * parameters and uses qsort to sort them. It * then displays the sorted arguments. */ #include <stdlib.h> #include <string.h> #include <stdio.h> int compare( const void *arg1, const void *arg2 ); void main( int argc, char **argv ) { int i; /* Eliminate argv[0] from sort: */ argv++; argc--; /* Sort remaining args using Quicksort algorithm: */ qsort( (void *)argv, (size_t)argc, sizeof( char * ), compare ); /* Output sorted list: */ for( i = 0; i < argc; ++i ) printf( "%s ", argv[i] ); printf( "\n" ); } int compare( const void *arg1, const void *arg2 ) { /* Compare all of both strings: */ return _stricmp( * ( char** ) arg1, * ( char** ) arg2 ); } Output [C:\code]qsort every good boy deserves favor boy deserves every favor good Searching and Sorting Routines See Also bsearch, _lsearch
C.参考大全第四版 本书是根据著名C语言专家HerbertSchildt的著作翻译的。这是一本关于C++语言的百科全书,包括C和C++的命令、功能、编程和应用等方面的内容。全书分为五个部分:C++基础:C子集;C++的专有特征;标准函数库;标准C++类库;C++应用程序范例。详细描述和演示了定义C++语言的关键字、语法、函数、类和特征。其第一部分全面讨论了C++的C子集;第二部分详细介绍了C++本身的特性,如类和对象、构造函数、析构函数和模板等;第三部分描述了标准函数库;第四部分讨论了标准类库,包括STL(标准模板库);第五部分显示了两个应用C++和面向对象编程的实际例子。 本书内容全面、翔实,是学习C++编程语言的广大学生的一部有用的工具书,也是对C++感兴趣的读者的必备参考书。 第一部分 C++基础:C子集 第1章 C语言概述 1.1 C语言的起源和历史 1.2 C语言级语言 1.3 C语言是结构化语言 1.4 C语言是程序员的语言 1.5 C程序的结构 1.6 库和链接 1.7 分别编译 1.8 理解.C和.CPP文件扩展 第2章 表达式 2.1 五种基本数据类型 2.2 修饰基本类型 2.3 标识符名称 2.4 变量 2.5 const和volatile限定符 2.6 存储类限定符 2.7 变量初始化 2.8 常量 2.9 运算符 2.10 表达式 第3章 语句 3.1 C和C++的真值和假值 3.2 选择语句 3.3 迭代语句 3.4 在选择和迭代语句内声明变量 3.5 跳转语句 3.6 表达式语句 3.7 块语句 第4章 数组和以null结束的字符串 4.1 一维数组 4.2 生成指向数组的指针 4.3 向函数传递一维数组 4.4 以null结束的字符串 4.5 二维数组 4.6 多维数组 4.7 带下标的指针 4.8 数组初始化 4.9 棋盘游戏实例 第5章 指针 5.1 什么是指针 5.2 指针变量 5.3 指针运算符 5.4 指针表达式 5.5 指针和数组 5.6 多级间址 5.7 初始化指针 5.8 指向函数的指针 5.9 C语言的动态分配函数 5.10 指针应用的问题 第6章 函数 6.1 函数的一般形式 6.2 数作用域的规则 6.3 函数变元 6.4 传给main()的变元argc和argv 6.5 return语句 6.6 递归 6.7 函数原型 6.8 声明变长参数列表 6.9 传统的与现代的函数参数声明 第7章 结构、联合、枚举和用户定义的类型 7.1 结构 7.2 结构数组 7.3 向函数传递结构 7.4 结构指针 7.5 结构的数组和结构 7.6 位域 7.7 联合 7.8 枚举 7.9 用sizeof来保证可移植性 7.10 typedef 第8章 C风格的控制台I/O 8.1 一个重要的应用说明 8.2 读写字符 8.3 读写字符串 8.4 格式化的控制台I/O 8.5 printf() 8.6 scanf() 第9章 文件I/O 9.1 C与C++的文件I/O 9.2 流和文件 9.3 流 9.4 文件 9.5 文件系统基础 9.6 fread()和fwrite() 9.7 fseek()和随机访问I/O 9.8 fprintf()和fscanf() 9.9 标准流 第10章 预处理器和注释 10.1 预处理器 10.2 #define 10.3 #error 10.4 #include 10.5 条件编译指令 10.6 #undef 10.7 使用defined 10.8 #line 10.9 #pragma 10.10 #和##预处理器运算符 10.11 预定义的宏名 10.12 注释 第二部分 C++的专有特征 第11章 C++语言概述 11.1 C++的起源 11.2 什么是面向对象的程序设计 11.3 C++基础 11.4 老的C++与现代C++ 11.5 C++的类 11.6 函数重载 11.7 运算符重载 11.8 继承 11.9 构造函数和析构函数 11.10 C++的关键字 11.11 C++程序的一般形式 第12章 类和对象 12.1 类 12.2 结构和类是相互关联的 12.3 联合和类是相互关联的 12.4 友元函数 12.5 友元类 12.6 内联函数 12.7 在类定义内联函数 12.8 带参数的构造函数 12.9 带一个参数的构造函数:特例 12.10 静态类成员 12.11 何时执行构造函数和析构函数 12.12 作用域分辨符 12.13 嵌套类 12.14 局部类 12.15 向函数传递对象 12.16 返回对象 12.17 对象赋值 第13章 数组、指针、引用和动态分配运算符 13.1 对象数组 13.2 指向对象的指针 13.3 C++指针的类型检查 13.4 this指针 13.5 指向派生类型的指针 13.6 指向类成员的指针 13.7 引用 13.8 格式问题 13.9 C++的动态分配运算符 第14章 函数重载、拷贝构造函数和默认变元 14.1 函数重载 14.2 重载构造函数 14.3 拷贝构造函数 14.4 查找重载函数的地址 14.5 重载的过去与现在 14.6 默认的函数变元 14.7 函数重载和二义性 第15章 运算符重载 15.1 创建成员运算符函数 15.2 使用友元函数的运算符重载 15.3 重载new和delete 15.4 重载某些特殊运算符 15.5 重载逗号运算符 第16章 继承 16.1 基类访问控制 16.2 继承和保护成员 16.3 继承多个基类 16.4 构造函数、析构函数和继承 16.5 准许访问 16.6 虚基类 第17章 虚函数与多态性 17.1 虚函数 17.2 继承虚属性 17.3 虚函数是分层的 17.4 纯虚函数 17.5 使用虚函数 17.6 早期绑定与后期绑定 第18章 模板 18.1 通用函数 18.2 应用通用函数 18.3 通用类 18.4 关键字typename和export 18.5 模板的功用 第19章 异常处理 19.1 异常处理基础 19.2 处理派生类异常 19.3 异常处理选项 19.4 理解terminate()和unexpected() 19.5 uncaught_exception()函数 19.6 exception和bad_exception类 19.7 异常处理的应用 第20章 C++输入/输出系统基础 20.1 老的C++I/O与现代的C++I/O 20.2 C++的流 20.3 C++的流类 20.4格式化的I/O 20.5 重载 第21章 C++文件的输入/输出 21.1 和文件类 21.2 打开和关闭文件 21.3 读写文本文件 21.4 无格式和二进制I/O 21.5 其他get()函数 21.6 getline()函数 21.7 检测EOF 21.8 ignore()函数 21.9 peek()和putback()函数 21.10 flush()函数 21.11 随机访问 21.12 I/O状态 21.13 定制的I/O和文件 第22章 运行时类型标识与强制转换运算符 22.1 运行时类型标识 22.2 强制转换运算符 22.3 dynamic_cast 第23章 名字空间、转换函数和其他高级主题 23.1 名字空间 23.2 std名字空间 23.3 创建转换函数 23.4 const成员函数与mutable 23.5 volatile成员函数 23.6 explicit构造函数 23.7 成员初始化语法 23.8 利用关键字asm 23.9 连接说明 23.10 基于数组的I/O 23.11 C与C++的区别 第24章 标准模板库 24.1 STL概述 24.2 容器类 24.3 一般的操作原理 24.4 vector容器 24.5 list容器 24.6 map容器 24.7 算法 24.8 使用函数对象 24.9 string类 24.10 关于STL的最后一点说明 第三部分 标准函数库 第25章 基子C的输入/输出函数 25.1 clearerr函数 25.2 fclose函数 25.3 feof函数 25.4 ferror函数 25.5 fflush函数 25.6 fSetc函数 25.7 fgetpos函数 25.8 fSets函数 25.9 fopen函数 25.10 fprintf函数 25.11 fputc函数 25.12 fputs函数 25.13 fread函数 25.14 freopen函数 25.15 fscmff函数 25.16 fseek函数 25.17 fsetpos函数 25.18 ftell函数 25.19 fwrite函数 25.20 gete函数 25.21 getchar函数 25.22 gets函数 25.23 perror函数 25.24 prinff函数 25.25 putc函数 25.26 putchar函数 25.27 puts函数 25.28 remove函数 25.29 rename函数 25.30 rewind函数 25.31 scanf函数 25.32 setbuf函数 25.33 setvbuf函数 25.34 sprinff函数 25.35 sscanf函数 25.36 tmpfile函数 25.37 tmpnam函数 25.38 ungetc函数 25.39 vpfintf,vfpfintf和vsprintf函数 第26章 字符串与字符函数 26.1 isalnum函数 26.2 isalpha函数 26.3 iscntrl函数 26.4 isdiSit函数 26.5 isgraph函数 26.6 islower函数 26.7 isprint函数 26.8 ispunct函数 26.9 isspace函数 26.10 isupper函数 26.11 isxdiSit函数 26.12 memchr函数 26.13 memcmp函数 26.14 memcpy函数 26.15 memmove函数 26.16 memset函数 26.17 strcat函数 26.18 strchr函数 26.19 strcmp函数 26.20 strcoll函数 26.21 strcpy函数 26.22 strcspn函数 26.23 strerror函数 26.24 strlen函数 26.25 strncat函数 26.26 stmcmp函数 26.27 strncpy函数 26.28 strpbrk函数 26.29 strrchr函数 26.30 strspn函数 26.31 strstr函数 26.32 strtok函数 26.33 strxfrm函数 26.34 tolower函数 26.35 toupper函数 第27章 数学函数 27.1 acos函数 27.2 asin函数 27.3 atan函数 27.4 atan2函数 27.5 ceil函数 27.6 COS函数 27.7 cosh函数 27.8 exp函数 27.9 fabs函数 27.10 floor函数 27.11 fmod函数 27.12 kexp函数 27.13 ldexp函数 27.14 log函数 27.15 loglO函数 27.16 modf函数 27.17 pow函数 27.18 sin函数 27.19 sinh函数 27.20 sqrt函数 27.21 tan函数 27.22 tanh函数 第28章 时间、日期和定位函数 28.1 asctime函数 28.2 clock函数 28.3 ctime函数 28.4 difftime函数 28.5 gmtime函数 28.6 localeeonv函数 28.7 localtime函数 28.8 mktime函数 28.9 setlocale函数 28.10 strftime函数 28.11 time函数 第29章 动态分配函数 29.1 calloc函数 29.2 free函数 29.3 malloc函数 29.4 realloe函数 第30章 实用函数 30.1 abort函数 30.2 abs函数 30.3 assert函数 30.4 atexit函数 30.5 atof函数 30.6 atoi函数 30.7 atol函数 30.8 bsearch函数 30.9 div函数 30.10 exit函数 30.11 getenv函数 30.12 labs函数 30.13 ldiv函数 30.14 longjmp函数 30.15 mblen函数 30.16 mbstowes函数 30.17 mbtowc函数 30.18 qsort函数 30.19 raise函数 30.20 rand函数 30.21 setjmp函数 30.22 signal函数 30.23 srand函数 30.24 strtod函数 30.25 strtol函数 30.26 strtoul函数 30.27 system函数 30.28 va_arg,va_start和va end函数. 30.29 wcstombs函数 30.30 wctomb函数 第31章 宽字符函数 31.1 宽字符分类函数 31.2 宽字符I/O函数 31.3 宽字符串函数 31.4 宽字符串转换函数 31.5 宽字符数组函数 31.6 多字节/宽字符转换函数 第四部分 标准C++类库 第32章 标准C++I/O类 32.1 I/O类 32.2 I/O头文件 32.3 格式化标记和I/O操作算子 32.4 几个数据类型 32.5 重载运算符 32.6 通用的I/O函数 第33章 STL容器类 33.1 容器类 第34章 STL算法 34.1 adjacent_find 34.2 binary_search 34.3 copy 34.4 copy_backward 34.5 count 34.6 count_if 34.7 equal 34.8 equal_range 34.9 flll和fill_n 34.10 find 34.11 find_end 34.12 find_first_of 34.13 find_if 34.14 for_each 34.15 generate和generate_n 34.16 includes 34.17 inplace_merge 34.18 iter_swap 34.19 lexicographical_compare 34.20 lower_bound 34.21 make_heap 34.22 max 34.23 max_element 34.24 merge 34.25 min 34.26 min_element 34.27 mismatch 34.28 next_permutation 34.29 nth_element 34.30 partial sort 34.31 partial sort_copy 34.32 partition 34.33 pop_heap 34.34 prev_permutation 34.35 push_heap 34.36 random_shuffle 34.37 remove,remove_if,remove copy和remove_copy_if 34.38 replace,replace_copy,replace_if和replace_copy_if 34.39 reverse和reverse_copy 34.40 rotate和rotate_copy 34.41 search 34.42 search_n 34.43 set_difference 34.44 set_intersection 34.45 set_symmetric_difference 34.46 set_union 34.47 sort 34.48 sort_heap 34.49 stable_partition 34.50 stable_sort 34.51 swap 34.52 swap_ranges 34.53 transform 34.54 unique和unique_copy 34.55 upper_bound 第35章 STL迭代器、分配器和函数对象 35.1 迭代器 35.2 函数对象 35.3 分配器 第36章 字符串类 36.1 basic_string类 36.2 char_traits类 第37章 数字类 37.1 complex类 37.2 valarray类 37.3 数字算法 第38章 异常处理和杂项类 38.1 异常 38.2 auto_ptr 38.3 pair类 38.4 本地化 38.5 其他有趣的类 第五部分 C++应用程序范例 第39章 集成新的类:自定义字符串类 39.1 StrType类 39.2 构造函数和析构函数 39.3 字符串I/O 39.4 赋值函数 39.5 连接 39.6 子字符串减法 39.7 关系运算符 39.8 各种字符串函数 39.9 完整的StrType类 39.10 使用StrType类 39.11 创建和集成新类型 39.12 挑战 第40章 分析表达式 40.1 表达式 40.2 分析表达式:问题 40.3 分析一个表达式 40.4 parser类 40.5 剖析一个表达式 40.6 一个简单的表达式分析器 40.7 向分析器添加变量 40.8 递归下降分析器的语法检查 40.9 构建一个通用的分析器 40.10 需要试验的一些东西 附录A C++的.NET可管理扩展 附录B C++和机器人时代
所有的 C / C++ 函数 Constructors (cppstring) Constructors (cppvector) Operators (cppbitset) Operators (cppdeque) Operators (cppstack) Operators (cppstring) Operators (cppvector) abort (stdother) abs (stdmath) acos (stdmath) any (cppbitset) append (cppstring) asctime (stddate) asin (stdmath) assert (stdother) assign (cppdeque) assign (cpplist) assign (cppstring) assign (cppvector) at (cppdeque) at (cppstring) at (cppvector) atan (stdmath) atan2 (stdmath) atexit (stdother) atof (stdstring) atoi (stdstring) atol (stdstring) back (cppdeque) back (cpplist) back (cppqueue) back (cppvector) bad (cppio) begin (cppdeque) begin (cpplist) begin (cppmap) begin (cppmultimap) begin (cppmultiset) begin (cppset) begin (cppstring) begin (cppvector) bsearch (stdother) c_str (cppstring) calloc (stdmem) capacity (cppstring) capacity (cppvector) ceil (stdmath) clear (cppdeque) clear (cppio) clear (cpplist) clear (cppmap) clear (cppmultimap) clear (cppmultiset) clear (cppset) clear (cppvector) clearerr (stdio) clock (stddate) compare (cppstring) copy (cppstring) cos (stdmath) cosh (stdmath) count (cppbitset) count (cppmap) count (cppmultimap) count (cppmultiset) count (cppset) ctime (stddate) data (cppstring) #define (preproc) difftime (stddate) div (stdmath) empty (cppdeque) empty (cpplist) empty (cppmap) empty (cppmultimap) empty (cppmultiset) empty (cpppriorityqueue) empty (cppqueue) empty (cppset) empty (cppstack) empty (cppstring) empty (cppvector) end (cppdeque) end (cpplist) end (cppmap) end (cppmultimap) end (cppmultiset) end (cppset) end (cppstring) end (cppvector) eof (cppio) equal_range (cppmap) equal_range (cppmultimap) equal_range (cppmultiset) equal_range (cppset) erase (cppdeque) erase (cpplist) erase (cppmap) erase (cppmultimap) erase (cppmultiset) erase (cppset) erase (cppstring) erase (cppvector) #error (preproc) exit (stdother) exp (stdmath) fabs (stdmath) fail (cppio)

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧