/**直接插入**/
#include <iostream>
#define N 10
using namespace std;
void InsertSort(int a[],size_t len)
{
for (size_t i = 1; i < len; i++)
{
int temp = a[i];
for (size_t j = i - 1; j>=0; j--)
{
if (temp < a[j])
{
a[j + 1] = a[j];
a[j] = temp;
}
else break;
}
cout << "第" << i << "趟:";
for (size_t i = 0; i != N; i++)
cout << a[i] << " ";
cout << endl;
}
}
int main()
{
int a[N];
for (size_t i = 0; i != N; i++)
cin >> a[i];
InsertSort(a,N);
for (size_t i = 0; i != N; i++)
cout << a[i] << " ";
cout<<endl;
return 0;
}
直接插入排序
最新推荐文章于 2025-03-09 23:46:07 发布