MSDN中的介绍

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

 

 

Return Value

None

Parameters

base

Start of target array//数组的起点

num

Array size in elements//数组中的个数

width

Element size in bytes//int,char等等的长度,通常直接sizeof(a[0]);

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


Code

Output

[C:\code]qsort every good boy deserves favor
boy deserves every favor good