插入排序

来源:互联网 发布:611资源网新域名 编辑:程序博客网 时间:2024/06/08 04:46

#include<iostream>
using namespace std;
void InsertSort(int a[],int n)
{
 int x;
 int i,j;
 for(i=1;i<n;i++)
 {
  x=a[i];
  for(j=i-1;j>=0;j--)
  {
   if(a[j]>x)
   {
    a[j+1]=a[j];
   } 
   else
   {
    break;
   }
  }
  a[j+1]=x;
 }
}
void main()
{
 int a[10]={1,5,2,7,8,6,10,3,4,9};
 InsertSort(a,10);
 for(int i=0;i<10;i++)
  cout<<a[i]<<"   ";
}