# c++排序算法

78人阅读 评论(0)

sort(first,last)

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void main()
{
vector<int>a;
a.push_back (4);
a.push_back (7);
a.push_back (2);
a.push_back (9);
a.push_back (5);
a.push_back (1);
for(int i=0;i<a.size();i++)
{
cout<<a[i]<<"  ";
}
cout<<endl;
sort(a.begin()+2,a.end() );			//sort
for(int j=0;j<a.size();j++)
{
cout<<a[j]<<"  ";
}
system("pause");
}
partial_sort(first,middle,last)

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void main()
{
vector<int>a;
a.push_back (5);
a.push_back (2);
a.push_back (52);
a.push_back (9);
a.push_back (4);
a.push_back (3);
a.push_back (43);
a.push_back (7);
for(int i=0;i<a.size ();i++)
{
cout<<a[i]<<"  ";
}
partial_sort(a.begin(),a.begin()+3,a.end() );			//partial sort
cout<<endl;
for(int j=0;j<a.size ();j++)
{
cout<<a[j]<<"  ";
}
cout<<endl;
system("pause");
}
merge(first1,last1,first2,last2,result)

#include<iostream>
#include<vector>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void main()
{
vector<int>a;
vector<int>b;
a.push_back (4);
a.push_back (7);
a.push_back (43);
a.push_back (72);

b.push_back (9);
b.push_back (5);
b.push_back (83);
b.push_back (24);
for(int i=0;i<a.size();i++)
{
cout<<a[i]<<"  ";
}
cout<<endl;
for(int j=0;j<b.size();j++)
{
cout<<b[j]<<"  ";
}
cout<<endl;
sort(a.begin (),a.end () );
sort(b.begin (),b.end () );

int size=a.size()+b.size();
vector<int>c(size);
vector<int>::iterator p;
merge(a.begin(),a.end(),b.begin(),b.end(),c.begin() );			//merge container
for(p=c.begin();p!=c.end();p++)
{
cout<<*p<<"  ";
}
cout<<endl;
system("pause");
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：8273次
• 积分：283
• 等级：
• 排名：千里之外
• 原创：21篇
• 转载：2篇
• 译文：0篇
• 评论：0条
文章分类