#include<iostream>
using namespace std;
const int maxn = 100;
int n;
int a[maxn];
int Insertion_sort(int a[])
{
int i, j, key;
for (i = 2; i <= n; i++){
key = a[i];
j = i - 1;
while(j > 0 && a[j] > key){
a[j + 1] = a[j];
j--;
}
a[j + 1] = key;
}
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++){
cin >> a[i];
}
Insertion_sort(a);
for (int i = 1; i <= n; i++){
cout << a[i] << ' ';
}
}
插入排序【模板】
最新推荐文章于 2024-06-19 16:08:31 发布