某班第1组有10个学生,进行了C++考试,编写程序将考试成绩输入一维数组,并将C++成绩用冒泡法、选择法两种排序算法,由低到高的顺序排序后输出。实验数据:90、85、80、75、70、65、60、55、50、45
时间: 2023-07-04 20:06:20 浏览: 218
好的,这道题目可以通过以下代码实现:
```c++
#include <iostream>
using namespace std;
// 冒泡排序
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// 选择排序
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
int main() {
int scores[10] = {90, 85, 80, 75, 70, 65, 60, 55, 50, 45};
// 冒泡排序
bubbleSort(scores, 10);
cout << "冒泡排序结果:";
for (int i = 0; i < 10; i++) {
cout << scores[i] << " ";
}
cout << endl;
// 选择排序
selectionSort(scores, 10);
cout << "选择排序结果:";
for (int i = 0; i < 10; i++) {
cout << scores[i] << " ";
}
cout << endl;
return 0;
}
```
输出结果为:
```
冒泡排序结果:45 50 55 60 65 70 75 80 85 90
选择排序结果:45 50 55 60 65 70 75 80 85 90
```