sort排序函数详解

参考自:https://www.cnblogs.com/AlvinZH/p/6784862.html?utm_source=itdadao&utm_medium=referral

阅读目录

1.sort

使用:#include <algorithm>

     using namespace std;

作用:排序

时间复杂度:n*lg(n)

实现原理:sort并不是简单的快速排序,它对普通的快速排序进行了优化,此外,它还结合了插入排序推排序。系统会根据你的数据形式和数据量自动选择合适的排序方法,这并不是说它每次排序只选择一种方法,它是在一次完整排序中不同的情况选用不同方法,比如给一个数据量较大的数组排序,开始采用快速排序,分段递归,分段之后每一段的数据量达到一个较小值后它就不继续往下递归,而是选择插入排序,如果递归的太深,他会选择推排序。

具体函数实现,请参考:http://www.cnblogs.com/fengcc/p/5256337.html

2.sort简介

函数声明:

#include <algorithm>
 
template< class RandomIt >
void sort( RandomIt first, RandomIt last );
 
template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );

形式:sort(first_pointer,first_pointer+n,cmp)

参数解释: 第一个参数是数组的首地址,一般写上数组名就可以,因为数组名是一个指针常量。第二个参数相对较好理解,即首地址加上数组的长度n(代表尾地址的下一地址)。最后一个参数是比较函数的名称(自定义函数cmp),这个比较函数可以不写,即第三个参数可以缺省,这样sort会默认按数组升序排序

简单例子:对数组A的0~n-1元素进行升序排序,只要写sort(A,A+n)即可;对于向量V也一样,sort(v.begin(),v.end())即可。

3.sort扩展

sort不只是能像上面那样简单的使用,我们可以对sort进行扩展,关键就在于第三个参数<cmp比较函数>,我们想降序排列,或者说我不是一个简简单单的数组,而是结构体、类怎么办,下面给出一些方法和例子。

方法一:定义比较函数(最常用)

bool cmp_down(int a, int b)
{
	return a > b;   //降序排列
}

bool cmp_up(int a, int b)
{
	return a < b;  //升序排列
}

int main()
{
	vector<int> v{ 2,1,55,33,77,66,100 };
	for (auto i : v)
		cout << i << "	"; //2 1 55 33 77 66 100 
	cout << endl;
	sort(v.begin(), v.end(), cmp_down);
	for (auto i : v)
		cout << i << "	"; //100 77 66 55 33 2 1
	cout << endl;
	sort(v.begin(), v.end(), cmp_up);
	for (auto i : v)
		cout << i << "	"; //1 2 33 55 66 77 100
}

也可以比较其他类型的数据

Student Stu[100];
bool cmp2(Student a,Student b)
{
    return a.id>b.id;//按照学号降序排列
    //return a.id<b.id;//按照学号升序排列
}
sort(Stu,Stu+100,cmp2);

方法二:使用标准库函数

另外,其实我们还可以再懒一点,在标准库中已经有现成的。它在哪呢?答案是functional,我们include进来试试看。functional提供了一堆基于模板的比较函数对象,它们是:equal_to<Type>、not_equal_to<Type>、greater<Type>、greater_equal<Type>、less<Type>、less_equal<Type>。这些东西的用法看名字就知道了。在这里,我么sort要用到的也只是greater和less就足够了,用法如下:

  • 升序:sort(begin,end,less<data-type>())
  • 降序:sort(begin,end,greater<data-type>())

缺点:也只是实现简单的排序,结构体不适用

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <functional>

using namespace std;
//简单使用方法
sort(A,A+100,greater<int>());//降序排列
sort(A,A+100,less<int>());//升序排列

方法三:重载结构体或类的比较运算符

注意:一定要重载<运算符,因为系统默认降序用的是<运算符。重载>运算法没有任何作用

情况一:在结构体或者类的内部重载(作成员函数),在类内部只传递一个参数,*this默认为左操作数,更加重要的是这个重载函数不仅参数需要加const来修饰,函数本身也需要加const来修饰,没有加 const 的函数不能在 const 对象上使用,为了让函数能够在更多的情况下正常使用, 最好对所有不会修改对象的函数加 const,有些情况下,如果可以没有加const修饰函数编译则会出错还需要注意的是const修饰函数时,只能修饰类成员函数,也就是友元函数和普通函数都不能用const修饰

typedef struct Student
{
	Student() = default;
	Student(int i) :id(i) {}
	int id;
	bool operator <(const Student&s) const; //也可以在类内部直接实现
	//{
	//	return this->id > s.id;   //按照id降序排序
	//	//return this->id < s.id;   //按照id升序排序
	//}
};

bool Student::operator < (const Student&s) const
{
	return this->id > s.id;   //按照id降序排序
	//return this->id < s.id;   //按照id升序排序
}

int main()
{
	vector<Student> v{ Student(2),Student(1), Student(55), Student(33), Student(77), Student(66), Student(100) };
	for (auto i : v)
		cout << i.id << "	";  //2 1 55 33 77 66 100
	cout << endl;
	sort(v.begin(), v.end());
	for (auto i : v)
		cout << i.id << "	";  //100 77 66 55 33 2 1
}

情况二:运算符重载函数作友元函数,可以在类内部传递俩个参数

class A
{
public:
	A() = default;
	A(int i) :id(i) {}
	int id;
	friend bool operator < (const A& a1, const A& a2);
	//{
	//	return a1.id < a2.id;   //降序排序
	//	//return a1.id < a2.id;  //升序排序
	//}
};

bool operator <(const A& a1, const A& a2)
{
	return a1.id > a2.id;   //降序排序
	//return a1.id < a2.id;  //升序排序
}

int main()
{
	vector<A> v{ A(2),A(1),A(55),A(33),A(77),A(66),A(100) };
	for (auto i : v)
		cout << i.id << "	";  //2 1 55 33 77 66 100
	cout << endl;
	sort(v.begin(), v.end());
	for (auto i : v)
		cout << i.id << "	"; //100 77 66 55 33 2 1
}

情况三:运算符重载函数作普通函数,只能在类外部定义,不能出现在类内部,传递俩个参数(不推荐)

class A
{
public:
	A() = default;
	A(int i) :id(i) {}
	int id;
};

bool operator <(const A& a1, const A& a2)
{
	return a1.id > a2.id;   //降序排序
	//return a1.id < a2.id;  //升序排序
}

int main()
{
	vector<A> v{ A(2),A(1),A(55),A(33),A(77),A(66),A(100) };
	for (auto i : v)
		cout << i.id << "	";  //2 1 55 33 77 66 100
	cout << endl;
	sort(v.begin(), v.end());
	for (auto i : v)
		cout << i.id << "	"; //100 77 66 55 33 2 1
}

方法四:使用仿函数

class A
{
public:
	A() = default;
	A(int i) :id(i) {}
	int id;
};

struct cmp
{
	bool operator() (const A& s1, const A&s2)
	{
		return s1.id > s2.id;  //降序排列
		//return s1.id < s2.id;  //升序排列
	}
};

int main()
{
	vector<A> v{ A(2),A(1),A(55),A(33),A(77),A(66),A(100) };
	for (auto i : v)
		cout << i.id<< "	";  //2 1 55 33 77 66 100
	cout << endl;
	sort(v.begin(), v.end(),cmp());
	for (auto i : v)
		cout << i.id<< "	"; //100 77 66 55 33 2 1
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值