写一段学生成绩排序的代码,含折半插入排序,快速排序,归并排序,希尔排序四中算法。输入姓名成绩,输出名词,姓名,成绩,运行中Visual Studio上
时间: 2024-06-08 15:12:27 浏览: 130
以下是使用C++编写的学生成绩排序代码,包括折半插入排序、快速排序、归并排序和希尔排序四种算法:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 折半插入排序
void binaryInsertionSort(vector<pair<string, int>>& v, int n) {
int i, j, low, high, mid;
pair<string, int> tmp;
for (i = 1; i < n; i++) {
tmp = v[i];
low = 0;
high = i - 1;
while (low <= high) {
mid = (low + high) / 2;
if (tmp.second < v[mid].second) {
high = mid - 1;
} else {
low = mid + 1;
}
}
for (j = i - 1; j >= high + 1; j--) {
v[j + 1] = v[j];
}
v[high + 1] = tmp;
}
}
// 快速排序
void quickSort(vector<pair<string, int>>& v, int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right;
pair<string, int> tmp = v[left];
while (i < j) {
while (i < j && v[j].second >= tmp.second) {
j--;
}
v[i] = v[j];
while (i < j && v[i].second <= tmp.second) {
i++;
}
v[j] = v[i];
}
v[i] = tmp;
quickSort(v, left, i - 1);
quickSort(v, i + 1, right);
}
// 归并排序
void merge(vector<pair<string, int>>& v, int left, int mid, int right) {
vector<pair<string, int>> tmp(right - left + 1);
int i = left, j = mid + 1, k = 0;
while (i <= mid && j <= right) {
if (v[i].second < v[j].second) {
tmp[k++] = v[i++];
} else {
tmp[k++] = v[j++];
}
}
while (i <= mid) {
tmp[k++] = v[i++];
}
while (j <= right) {
tmp[k++] = v[j++];
}
for (i = 0; i < k; i++) {
v[left + i] = tmp[i];
}
}
void mergeSort(vector<pair<string, int>>& v, int left, int right) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
mergeSort(v, left, mid);
mergeSort(v, mid + 1, right);
merge(v, left, mid, right);
}
// 希尔排序
void shellSort(vector<pair<string, int>>& v, int n) {
int gap, i, j;
pair<string, int> tmp;
for (gap = n / 2; gap > 0; gap /= 2) {
for (i = gap; i < n; i++) {
tmp = v[i];
for (j = i - gap; j >= 0 && tmp.second < v[j].second; j -= gap) {
v[j + gap] = v[j];
}
v[j + gap] = tmp;
}
}
}
int main() {
vector<pair<string, int>> v;
int n;
cout << "请输入学生人数: ";
cin >> n;
string name;
int score;
cout << "请依次输入学生姓名和成绩: " << endl;
for (int i = 0; i < n; i++) {
cin >> name >> score;
v.push_back(make_pair(name, score));
}
// 折半插入排序
binaryInsertionSort(v, n);
cout << "折半插入排序结果: " << endl;
for (int i = 0; i < n; i++) {
cout << v[i].first << " " << v[i].second << endl;
}
cout << endl;
// 快速排序
quickSort(v, 0, n - 1);
cout << "快速排序结果: " << endl;
for (int i = 0; i < n; i++) {
cout << v[i].first << " " << v[i].second << endl;
}
cout << endl;
// 归并排序
mergeSort(v, 0, n - 1);
cout << "归并排序结果: " << endl;
for (int i = 0; i < n; i++) {
cout << v[i].first << " " << v[i].second << endl;
}
cout << endl;
// 希尔排序
shellSort(v, n);
cout << "希尔排序结果: " << endl;
for (int i = 0; i < n; i++) {
cout << v[i].first << " " << v[i].second << endl;
}
cout << endl;
return 0;
}
```
需要注意的是,以上代码中的 `vector<pair<string, int>>` 是一个存储姓名和成绩的二元组的容器,使用 `make_pair()` 函数生成二元组后存储在容器中。在输入数据时需要按照姓名和成绩的顺序输入,例如:
```
请输入学生人数: 5
请依次输入学生姓名和成绩:
Tom 78
Jack 89
Lucy 92
Mary 85
Bob 67
```
输出结果如下:
```
折半插入排序结果:
Bob 67
Tom 78
Mary 85
Jack 89
Lucy 92
快速排序结果:
Bob 67
Tom 78
Mary 85
Jack 89
Lucy 92
归并排序结果:
Bob 67
Tom 78
Mary 85
Jack 89
Lucy 92
希尔排序结果:
Bob 67
Tom 78
Mary 85
Jack 89
Lucy 92
```
阅读全文