#include <iostream>
const int MAXSIZE = 100;
using namespace std;
void insertSort(int[], int);
int main(void)
{
int array[MAXSIZE];
int n;
while (cin >> n)
{
for (int i = 0; i < n; ++i)
{
cin >> array[i];
}
insertSort(array, n);
for (int i = 0; i < n; ++i)
{
cout << array[i] << ' ';
}
cout << endl;
}
return 0;
}
void insertSort(int array[], int n)
{
int key;
int i;
int j;
for (i = 1; i < n; ++i)
{
cout << "第" << i << "轮:" << endl;
key = array[i];
for (j = i - 1 ; j >= 0 && array[j] > key; --j)
{
cout << "第" << j << "个元素后移" << endl;
array[j + 1] = array[j];
}
cout << "将key放置在" << j + 1 << endl;
array[j + 1] = key;
}
}
插入排序
最新推荐文章于 2024-09-28 15:39:21 发布