实用算法实践(辅助)--C&C++中的qsort和sort库函数

文章转自: http://blog.csdn.net/lewsn2008/article/details/8626000

 

今天在看高手的代码的时候才发现,C语言中有一个qsort的库函数(在<stdlib.h>头文件中),该函数可以对用户指定的数据(或者说内存区域)进行快速排序,为什么说是对内存区域排序呢?因为排序的过程中是对用户指定的内存区域按照用户指定的偏移量进行排序的,所以不仅可以对简单类型的数组排序,也可以对结构体类型的数组按照其某个字段来排序,使用起来非常的方便,下面将详细说明该函数的用法。

 

另外,C++的STL中也实现并优化了排序函数,并提供了sort、stable_sort等不同的版本,且这些函数都是泛型函数,可以对不同的容器进行排序,功能比C语言版本的qsort要强大,效率要高。(可以参考文章《详细解说 STL 排序(Sort)》:http://www.cppblog.com/mzty/archive/2005/12/15/1770.html

 


=================================================================================================================================

以下内容转自:http://www.slyar.com/blog/stdlib-qsort.html

C语言标准库函数 qsort 详解

文章作者:姜南(Slyar) 文章来源:Slyar Home (www.slyar.com)

 

qsort包含在<stdlib.h>头文件中,此函数根据你给的比较条件进行快速排序,通过指针移动实现排序。排序之后的结果仍然放在原数组中。使用qsort函数必须自己写一个比较函数。

函数原型:

void qsort (void * base, size_t num, size_t size, int ( * comparator ) ( const void *,const void * ) );

用法以及参数说明:

Sorts the num elements of the array pointedby base, each element size bytes long, using the comparator function todetermine the order.

The sorting algorithm used by this functioncompares pairs of values by calling the specified comparator function with twopointers to elements of the array.

The function does not return any value, butmodifies the content of the array pointed by base reordering its elements tothe newly sorted order.

base Pointer tothe first element of the array to be sorted.(数组起始地址)
num 
Number of elements in the arraypointed by base.(数组元素个数)
size 
Size in bytes of each element in thearray.(每一个元素的大小)
comparator 
Function that compares two elements.(函数指针,指向比较函数)
1
The function must accept two parameters thatare pointers to elements, type-casted as void*. These parameters should be castback to some data type and be compared.
2
The return value of this function shouldrepresent whether elem1 is considered less than, equal to, or greater thanelem2 by returning, respectively, a negative value, zero or a positive value.
Return Value 
none (无返回值)

一、对int类型数组排序

int num[100];

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

qsort(num,100,sizeof(num[0]),cmp);

二、对char类型数组排序(同int类型)

charword[100];

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

qsort(word,100,sizeof(word[0]),cmp);

三、对double类型数组排序

double in[100];

int cmp(const void *a , const void *b )
{
return *(double *)a > *(double *)b ? 1 : -1;
}

qsort(in,100,sizeof(in[0]),cmp);

四、对结构体一级排序

struct Sample
{
double data;
int other;
}s[100]

//按照data的值从小到大将结构体排序

int cmp(const void *a ,const void *b)
{
return (*(Sample *)a).data > (*(Sample *)b).data ? 1 : -1;
}

qsort(s,100,sizeof(s[0]),cmp);

五、对结构体二级排序

struct Sample
{
int x;
int y;
}s[100];

//
按照x从小到大排序,当x相等时按照y从大到小排序

int cmp(const void *a , const void *b )
{
struct Sample *c = (Sample *)a;
struct Sample *d = (Sample *)b;
if(c->x != d->x) return c->x - d->x;
else return d->y - c->y;
}

qsort(s,100,sizeof(s[0]),cmp);

六、对字符串进行排序

struct Sample
{
int data;
char str[100];
}s[100];

//
按照结构体中字符串str的字典顺序排序

int cmp (const void *a , const void *b )
{
return strcmp( (*(Sample *)a)
.str , (*(Sample *)b).str );
}

qsort(s,100,sizeof(s[0]),cmp);

附加一个完整点的代码,对字符串二维数组排序:

#include<stdio.h>
#include <stdlib.h>
#include <string.h>

chars[2001][1001];

int cmp(constvoid *a, const void *b){
return strcmp((char *)a,(char *)b);
}

int main(){
int i,n;
scanf("%d",&n);
getchar();
for(i=0;i<n;i++) gets(s[i]);
qsort(s,n,1001*sizeof(char),cmp);
for(i=0;i<n;i++) puts(s[i]);
return 0;
}

=================================================================================================================================

 

 

http://blog.163.com/sentimental_man/blog/static/73001618200941584052485/该文章中使用qsort函数对线段数组进行排序,数组中存的是线段的起点终点的值,每条线段占两个int,自己编写的cmp函数以起点值进行比较,所以最终是以线段的起点进行排序的。该文章中,作者标注了“qsort(map, n, sizeof(int)*2, cmp); /*这儿行吗?map?  不是一维数组啊?*/”,实际上这里是没有问题的,而恰恰是对qsort的巧妙使用!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值