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];
}
...全文
1214 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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

70,020

社区成员

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

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