排行榜冒泡排序c++
时间: 2024-01-16 14:18:54 浏览: 29
以下是使用冒泡排序算法对排行榜进行排序的C++代码示例:
```cpp
#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]) {
// 交换arr[j]和arr[j+1]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int scores[] = {90, 85, 95, 80, 70};
int n = sizeof(scores) / sizeof(scores[0]);
cout << "排行榜排序前的分数:";
for (int i = 0; i < n; i++) {
cout << scores[i] << " ";
}
cout << endl;
bubbleSort(scores, n);
cout << "排行榜排序后的分数:";
for (int i = 0; i < n; i++) {
cout << scores[i] << " ";
}
cout << endl;
return 0;
}
```
运行以上代码,将会输出排行榜排序前和排序后的分数。