插入排序
有一个已经有序的数据序列,要求在这个已经排好的数据序列中插入一个数,但要求插入后此数据序列仍然有序
插入排序的基本思想是:每步将一个待排序的纪录,按其关键码值的大小插入前面已经排序的文件中适当位置上,直到全部插入完为止。
#incluee <iostream>
void InsertSort(int *a,int n)
{
int in; //已经排好队的序列
int out; //待排序元素 开始时out=0
for(out=0;out<n;out++)
{
int temp = a[out];
in = out;
while(in >0 && a[in -1] >= temp)
{
a[in] = a[in -1];
--in;
}
a[in] = temp;
}
}
int main()
{
int x[]={3,4,1,9,0,8,2,5,7,6};
InsertSort(x,sizeof(x)/sizeof(int));
for(auto num : x)
std::cout << num << " ";
std::cout<< std::endl;
return 0;
}