Chinaunix首页 | 论坛 | 博客
  • 博客访问: 413709
  • 博文数量: 68
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 728
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-14 00:19
文章分类

全部博文(68)

文章存档

2011年(1)

2009年(1)

2008年(17)

2007年(30)

2006年(19)

我的朋友

分类: C/C++

2006-12-12 18:17:21

void  qsort ( void * base, size_t num, size_t width, int (*fncompare)(const void *, const void *) );

Sort using quicksort algorith.
  This function uses an implementation of the quicksort algorithm to sort the num elements of an array pointed by base, each element has the specified width in bytes.
  The method used to compare each pair of elements is provided by the caller to this function with fncompare parameter, that is a function called one or more times during the sort process.

Parameters.

base
Pointer to the first element of the array where the sorting process has to be performed.
num
Number of elements in the array pointed by base.
width
Width of each element in the array.
fncompare
Function that compares two elements. This should be provided by the caller to this function and must follow or be casted to a declaration like:
int fncompare (const void * elem1, const void * elem2 );
The function should receive two parameters (elem1 and elem2) that are pointers to elements, and should return an int value with the result of comparing them:
return value description
<0 *elem1 goes before *elem2
0 *elem1 == *elem2
>0 *elem1 goes after *elem2

Return Value.
  (none)

Portability.
  Defined in ANSI-C.

Example.

/* qsort example */
#include
#include

int values[] = { 40, 10, 100, 90, 20, 25 };

int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}

int main ()
{
int * pItem;
int n;
qsort (values, 6, sizeof(int), compare);
for (n=0; n<6; n++)
{
printf ("%d ",values[n]);
}
return 0;
}
Output:
10 20 25 40 90 100
阅读(2099) | 评论(0) | 转发(0) |
0

上一篇:不眠

下一篇:我的Linux笔记!!!

给主人留下些什么吧!~~