You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.0 KiB
46 lines
1.0 KiB
#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;
|
|
}
|