插入排序
来源:互联网 发布:mac如何保存gif 编辑:程序博客网 时间:2024/06/08 01:26
插入排序
#include <iostream>
using namespace std;
void coutstream(int a[],int n){
for(int i=0;i!=n;i++)
cout<<a[i]<<" ";
}
void insertsort(int a[],int n){
int temp;
for(int i=1;i<n;i++)
{
int j=i;
temp=a[i]; //先把a[i]位置的数据存起来
while(j>0&&temp<a[j-1])
{
a[j]=a[j-1];
j--;
}
a[j]=temp;
}
}
int main()
{
int a[5]={1,6,4,8,4};
insertsort(a,5);//插入排序
coutstream(a,5);//
return 0;
}
#include <iostream>
using namespace std;
void coutstream(int a[],int n){
for(int i=0;i!=n;i++)
cout<<a[i]<<" ";
}
void insertsort(int a[],int n){
int temp;
for(int i=1;i<n;i++)
{
int j=i;
temp=a[i]; //先把a[i]位置的数据存起来
while(j>0&&temp<a[j-1])
{
a[j]=a[j-1];
j--;
}
a[j]=temp;
}
}
int main()
{
int a[5]={1,6,4,8,4};
insertsort(a,5);//插入排序
coutstream(a,5);//
return 0;
}