Files
python/TangDou/LuoGuBook/L9_2_InsertionSort.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

23 lines
638 B
C++

#include <bits/stdc++.h>
using namespace std;
const int len = 10;
int a[] = {3, 5, 1, 7, 2, 4, 6, 9, 8, 0};
int main() {
//数组长度
int len = sizeof(a) / sizeof(int);
for (int i = 1; i < len; i++) {
int now = a[i]; //记录一下待插牌
int j = i - 1;
while (j >= 0 && a[j] > now) { //发现比待插入牌大
a[j + 1] = a[j]; //挪出位置
j--; //继续向前找
}
a[j + 1] = now; //放到合适的位置上
}
//输出
for (int i = 0; i < len; i++) printf("%d ", a[i]);
return 0;
}