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;
}
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;
}
相关文章推荐
- 快速排序:Sort:QuickSort using C++
- 快速排序 C语言的qsort 以及 C++的 sort
- C++快速排序之sort()
- 快速排序 C语言的qsort及C++的sort
- C++ 排序函数 sort(),qsort()的用法
- C++实现谢尔排序(希尔排序)(shell sort)
- 快速排序--c++实现
- c++: 直接插入排序,冒泡排序,快速排序,堆排序和归并排序
- C++ 详细解说 STL 排序(Sort)
- C++学习 - 快速排序,更加优化的实现
- C/C++面试题(三) 推断二叉树、快速排序递归实现、递归判断数组递增
- C++快速排序
- 关于C++中vector和set使用sort方法进行排序
- 快速排序C++
- 经典排序算法 - 快速排序Quick_sort
- C++的STL库,vector sort排序时间复杂度 及常见容器比较
- [Sort]快速排序-递归实现
- 快速排序 c++ 实现
- STL sort 函数实现详解 作者:fengcc 原创作品 转载请注明出处 前几天阿里电话一面,被问到STL中sort函数的实现。以前没有仔细探究过,听人说是快速排序,于是回答说用快速排序实现的
- c++中快速排序