C++中qsort()的用法

 

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 ) );

RoutineRequired HeaderCompatibility
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 ValueDescription
< 0elem1 less than elem2
0elem1 equivalent to elem2
> 0elem1 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


ContractedBlock.gifExpandedBlockStart.gifCode
 1ExpandedBlockStart.gifContractedBlock.gif/**//* QSORT.C: This program reads the command-line
 2 * parameters and uses qsort to sort them. It
 3 * then displays the sorted arguments.
 4 */

 5
 6#include <stdlib.h>
 7#include <string.h>
 8#include <stdio.h>
 9
10int compare( const void *arg1, const void *arg2 );
11
12void main( int argc, char **argv )
13ExpandedBlockStart.gifContractedBlock.gif{
14   int i;
15ExpandedSubBlockStart.gifContractedSubBlock.gif   /**//* Eliminate argv[0] from sort: */
16   argv++;
17   argc--;
18
19ExpandedSubBlockStart.gifContractedSubBlock.gif   /**//* Sort remaining args using Quicksort algorithm: */
20   qsort( (void *)argv, (size_t)argc, sizeofchar * ), compare );
21
22ExpandedSubBlockStart.gifContractedSubBlock.gif   /**//* Output sorted list: */
23   for( i = 0; i < argc; ++i )
24      printf( "%s ", argv[i] );
25   printf( "\n" );
26}

27
28int compare( const void *arg1, const void *arg2 )
29ExpandedBlockStart.gifContractedBlock.gif{
30ExpandedSubBlockStart.gifContractedSubBlock.gif   /**//* Compare all of both strings: */
31   return _stricmp( * ( char** ) arg1, * ( char** ) arg2 );
32}

33

Output

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

 

 

 

 

 

转载于:https://www.cnblogs.com/skunk/archive/2009/05/07/1452205.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值