您的位置:首页 > 编程语言 > C语言/C++

C++快速排序之sort()

2012-12-21 09:15 375 查看
原文地址:http://blog.csdn.net/wanjun8659/article/details/7525009

ort()函数是C++中的排序函数其头文件为:#include<algorithm>头文件;

sort()相对于qsort()更加灵活,对基本的类型排序不需要定义排序函数

1、sort()

sort 对给定区间所有元素进行排序

stable_sort 对给定区间所有元素进行稳定排序

partial_sort 对给定区间所有元素部分排序

partial_sort_copy 对给定区间复制并排序

nth_element 找出给定区间的某个位置对应的元素

is_sorted 判断一个区间是否已经排好序

partition 使得符合某个条件的元素放在前面

stable_partition 相对稳定的使得符合某个条件的元素放在前面

语法描述为:

(1)sort(begin,end),表示一个范围,例如:

#include "stdafx.h"

#include<iostream>

#include <algorithm>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

int a[10]={2,4,1,23,5,76,0,43,24,65},i;

for(i=0;i<10;i++)

cout<<a[i]<<" ";

cout<<endl;

sort(a,a+5);

for(i=0;i<10;i++)

cout<<a[i]<<" ";

system("pause");

return 0;

}

输出结果将是把数组a按升序排序,说到这里可能就有人会问怎么样用它降序排列呢?这就是下一个讨论的内容。

(2)sort(begin,end,compare)

一种是自己编写一个比较函数来实现,接着调用三个参数的sort:sort(begin,end,compare)就成了。

对于list容器,这个方法也适用,把compare作为sort的参数就可以了,即:sort(compare)。

1)自己编写compare函数:

bool compare(int a,int b) //注意这里的返回值类型,是bool类型,而不是qsort()函数那样的int类型,而且参数更加简化了

{

return a<b; //按升序排序,如果按降序排序改为“a>b”

}

int _tmain(int argc, _TCHAR* argv[])

{

int a[10]={2,4,1,23,5,76,0,43,24,65},i;

for(i=0;i<10;i++)

cout<<a[i]<<" ";

cout<<endl;

sort(a,a+10,compare);

for(i=0;i<10;i++)

cout<<a[i]<<" ";

system("pause");

return 0;

}

2)更进一步,让这种操作更加能适应变化。也就是说,能给比较函数一个参数,用来指示是按升序还是按降序排,这回轮到函数对象出场了。

为了描述方便,我先定义一个枚举类型EnumComp用来表示升序和降序。很简单:

enum Enumcomp{ASC,DESC};

class compare

{

private:

Enumcomp comp;

public:

compare(Enumcomp c):comp(c) {};

bool operator () (int num1,int num2)

{

switch(comp)

{

case ASC:

return num1<num2;

case DESC:

return num1>num2;

}

}

};

int main()

{

int a[20]={2,4,1,23,5,76,0,43,24,65},i;

for(i=0;i<10;i++)

cout<<a[i]<<" ";

cout<<endl;

sort(a,a+20,compare(DESC));

for(i=0;i<10;i++)

cout<<a[i]<<" ";

return 0;

}

3)其实对于这么简单的任务(类型支持“<”、“>”等比较运算符),完全没必要自己写一个类出来。标准库里已经有现成的了,就在functional里,include进来就行了。functional提供了一堆基于模板的比较函数对象。它们是(看名字就知道意思了):equal_to<Type>、not_equal_to<Type>、greater<Type>、greater_equal<Type>、less<Type>、less_equal<Type>。对于这个问题来说,greater和less就足够了,直接拿过来用:

升序:sort(begin,end,less<data-type>());

降序:sort(begin,end,greater<data-type>()).

#include "stdafx.h"

#include<iostream>

#include <algorithm>

#include <xfunctional>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

int a[10]={2,4,1,23,5,76,0,43,24,65},i;

for(i=0;i<10;i++)

cout<<a[i]<<" ";

cout<<endl;

sort(a,a+10,greater<int>());

for(i=0;i<10;i++)

cout<<a[i]<<" ";

system("pause");

return 0;

}

int main()

{

vector <int> vec;

int a,i=0;

cin>>a;

while(cin.peek()!='\n')

{

vec.push_back(a);

cin>>a;

}

vec.push_back(a);

sort(vec.begin(),vec.end(),greater<int>());

while(i<vec.size())

cout<<vec[i++]<<" ";

system("pause");

return 0;

}

4)既然有迭代器,如果是string 就可以使用反向迭代器来完成逆序排列,程序如下:

#include "stdafx.h"

#include<iostream>

#include<string>

#include <algorithm>

using namespace std;

int main()

{

string str("wanjun");

string s(str.rbegin(),str.rend());

cout<<s<<endl;

system("pause");

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: