# c++排序算法

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");
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：c++排序算法 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)