#include<iostream>
using namespace std;
void InsertSort(int a[],int n);
int main()
{
int a[20];
for(int i=0;i<10;i++)
a[i]=i*2;
for(int i=10;i<20;i++)
a[i]=i/2+i%2;
InsertSort(a,20);
for(int i=0;i<20;i++)
cout<<a[i]<<" ";
return 0;
}
void InsertSort(int a[],int n)
{
int swap;
for(int i=1;i<n;i++)
{
swap=a[i];
int j=i-1;
while(j>=0&&a[j]>swap)
{
a[j+1]=a[j];
j--;
}
a[j+1]=swap;//j后一位为插入元素
}
}
(算法设计技巧与分析)InsertionSort
最新推荐文章于 2022-07-01 21:57:48 发布