insert sort with C++

来源:互联网 发布:淘宝企业店铺如何申请 编辑:程序博客网 时间:2024/06/05 22:06

  1 #include <iostream>
  2 using namespace std;
  3
  4 void insert_sort(int a[],int len)
  5 {
  6     int i,j,tmp;
  7     for(i=1;i<len;i++)
  8     {
  9        tmp=a[i];
 10        for(j=i-1;j>=0&&a[j]>tmp;j--)
 11            a[j+1]=a[j];
 12        a[j+1]=tmp;
 13     }
 14 }
 15
 16 int main()
 17 {
 18    const int LEN=10;
 19    int a[LEN] = {10,2,4,3,6,7,9,8,5,1};
 20
 21    cout << "insert sort before :/n ";
 22    for(int i=0; i<LEN; i++)
 23        cout << a[i] << " ";
 24    cout << endl;
 25
 26    insert_sort(a,LEN);
 27    cout << "insert sort after :/n ";
 28    for(int i=0; i<LEN; i++)
 29        cout << a[i] << " ";
 30    cout << endl;
 31
 32    return 0;
 33 }