About sort and qsort in c

1. for sort  here

struct POINT{
	__int64 a;
	__int64 b;
	bool operator < (const POINT &p) const {
		if(a==0&&b==0) return 0>1;
		if(p.a==0&&p.b==0) return 1>0;
		__int64 l1 = (a)*(p.b); __int64 l2 = (b)*(p.a);
		return l1 < l2;
	}
} points[100011];

We define a struct here, just complete the bool operator < (const POINT &p) const{...}, and then call:

sort(points, points+n); //sort the points


2. Second way: qsort

struct POINT{
	__int64 a;
	__int64 b;
} points[100011];

//define a method of compare here, 
//used to sort the points. 
//called by qsort method
int compare(const void * p1, const void * p2) {
		struct POINT *p3, *p4;
		p3 = (struct POINT *)p1;
		p4 = (struct POINT *)p2;

		__int64 t1, t2;
		t1 = (p3->a)*(p4->b);
		t2 = (p3->b)*(p4->a);
		if(t1<=t2) return -1;
		else return 1;
}

Finally

qsort(points,n,sizeof(points[0]),compare);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值