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

46 lines
1.0 KiB
C++

#include <bits/stdc++.h>
using namespace std;
int main() {
int a[5] = {0};
for (int i = 0; i < 5; ++i) {
cin >> a[i];
}
const int len = sizeof a / sizeof a[0];
//--插入排序(升序)----------------
for (int i = 1; i < len; i++) {
int key = a[i];
int j = i - 1;
while (j >= 0 && key < a[j]) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = key;
}
//---------------------------------
cout << "升序:";
for (int i = 0; i < len; i++) {
cout << a[i] << " ";
}
cout << endl;
//--插入排序(降序)----------------
for (int i = 1; i < len; i++) {
int key = a[i];
int j = i - 1;
while (j >= 0 && key > a[j]) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = key;
}
//---------------------------------
cout << "降序:";
for (int i = 0; i < len; i++) {
cout << a[i] << " ";
}
cout << endl;
return 0;
}