93~94、2道题,利用数组实现冒泡排序&习题
1,请编程实现数组的冒泡排序(从小到大),然后逆序输出,实现从大到小的输出。
// 请编程实现数组的冒泡排序(从小到大),实现从大到小的输出。
#include<iostream>
using namespace std;
int main() {
int a[5];
int b;
int n = sizeof a / sizeof a[1];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n-1; i++) {
for (int j = 0; j<n-1-i;j++) {
if (a[j] > a[j+1]) {
b = a[j];
a[j] = a[j + 1];
a[j + 1] = b;
}
}
}
for (int i = n-1; i >= 0; i--) {
cout << a[i] ;
if (i >= 1) {
cout << '>';
}
}
cout << endl;
system("pause");//卡屏函数
return 0;
}
2,请编程直接实现数组的冒泡排序(从大到小)
//请编程直接实现数组的冒泡排序(从大到小)
#include<iostream>
using namespace std;
int main() {
int a[5];
int b;
int n = sizeof a / sizeof a[1];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n-1; i++) {
for (int j = 0; j<n-1-i;j++) {
if (a[j] < a[j+1]) {
b = a[j];
a[j] = a[j + 1];
a[j + 1] = b;
}
}
}
for (int i = 0; i < n; i++) {
cout << a[i];
if (i < n - 1) {
cout << '>';
}
}
cout << endl;
system("pause");//卡屏函数
return 0;
}
// 24 29 826
// 29 826 526
// 826 556 29
// 556 24 24