#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::cin;
int main()
{
int l= 0,i = 0,j=0,key=0;
cout << "请输入数量: ";
cin >> l;
int *p1 = new int [l];
for (int i=0;i<l;i++)
{
cout << "请输入第"<<i+1<<"个值: ";
cin >>p1[i];
}
for (int j=1;j<l;j++)//执行次数为统计量-1
{
int key = p1[j];
int i = j - 1;
while (i>=0 && p1[i]>key)
{
p1[(i+1)] = p1[i];
i = i-1;
}
p1[(i+1)] = key;
/*for (int a = 0; a < l; a++)
{cout << p1[a];
if(a<(l-1))cout<<",";
}cout << endl;
*/
}
for (int a = 0; a < l; a++)
{
cout << "Value of var[" << a << "] = ";
cout << p1[a] << endl;
}
delete [] p1;
cin.get();
cin.get();
return 0;
}
插入排序
最新推荐文章于 2024-08-23 15:15:47 发布