#include<iostream>
#include<vector>
#include<algorithm>
#include <iterator>
using namespace std;
int main()
{
int n;
cin>>n;
vector<int> v(n,8);
for (int i = 0; i < 8; ++i)
{
/* code */
v.push_back(i);
}
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout<<endl;
system("pause");
return 0;
}
insertion sort:
#include<iostream>
using namespace std;
int main()
{
int re[7]={0};
for(int i=0;i<7;i++)
cin>>re[i];
for(int j=1;j<7;j++)
{
int key=re[j];
cout<<key<<endl;
int i=j-1;
while(i>=0&&re[i]>key)
{
re[i+1]=re[i];
i-=1;
}
re[i+1]=key;
}
for(int j=0;j<7;j++)
cout<<re[j]<<" ";
cout<<endl;
system("pause");
}
递归版本的sort
#include<iostream>
#include<cstring>
using namespace std;
void sort(char *list,int high)
{
if(high>0)
{
int indexofhigh=0;
char max = list[0];
for(int i=1;i<=high;i++)
{
if(list[i]>max)
{
max=list[i];
indexofhigh=i;
}
}
list[indexofhigh]=list[high];
list[high]=max;
sort(list,high-1);
}
}
void sort(char *list)
{
sort(list,strlen(list)-1);
}
int main()
{
//string a;
char s[80];
cin.getline(s,80);
sort(s);
for(int i=0;i<strlen(s);i++)
cout<<s[i];
cout<<endl;
system("pause");
}