1.编写顺序表的初始化、创建和输出函数。 2.编写实现选择排序的算法,并输出{14,1,68,27,55,19,29,20,84,79,23,10,11} 和 { 14, 17, 53, 35, 9, 32, 68, 41, 76, 23}的排序过程。 3.编写实现冒泡排序的算法,并输出{14,1,68,27,55,19,29,20,84,79,23,10,11} 和 { 14, 17, 53, 35, 9, 32, 68, 41, 76, 23}的排序过程。 4.输入主函数实现对上面排序函数的调用。
时间: 2024-03-14 17:42:51 浏览: 112
编写函数,建立一个顺序表并输出,计算该顺序表的长度。 实现在顺序表中查找第i个元素的值。实现在顺序表中第i个位置上插入值为x的
5星 · 资源好评率100%
好的,我已经记录下来了。下面是回答:
1. 顺序表的初始化、创建和输出函数
```c++
#include <iostream>
using namespace std;
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int length;
} SqList;
void InitList(SqList &L) {
L.length = 0;
}
void CreateList(SqList &L) {
cout << "请输入顺序表的长度:";
cin >> L.length;
cout << "请输入顺序表的元素:";
for (int i = 0; i < L.length; i++) {
cin >> L.data[i];
}
}
void PrintList(SqList L) {
if (L.length == 0) {
cout << "顺序表为空" << endl;
return;
}
for (int i = 0; i < L.length; i++) {
cout << L.data[i] << " ";
}
cout << endl;
}
```
2. 实现选择排序的算法,并输出{14,1,68,27,55,19,29,20,84,79,23,10,11} 和 { 14, 17, 53, 35, 9, 32, 68, 41, 76, 23}的排序过程
```c++
#include <iostream>
using namespace std;
void SelectSort(int A[], int n) {
for (int i = 0; i < n - 1; i++) {
int min_index = i;
for (int j = i + 1; j < n; j++) {
if (A[j] < A[min_index]) {
min_index = j;
}
}
if (min_index != i) {
swap(A[min_index], A[i]);
}
}
}
int main() {
int A[] = {14, 1, 68, 27, 55, 19, 29, 20, 84, 79, 23, 10, 11};
int n = sizeof(A) / sizeof(A[0]);
SelectSort(A, n);
for (int i = 0; i < n; i++) {
cout << A[i] << " ";
}
cout << endl;
int B[] = {14, 17, 53, 35, 9, 32, 68, 41, 76, 23};
n = sizeof(B) / sizeof(B[0]);
SelectSort(B, n);
for (int i = 0; i < n; i++) {
cout << B[i] << " ";
}
cout << endl;
return 0;
}
```
输出:
```
1 10 11 14 19 20 23 27 29 55 68 79 84
9 14 17 23 32 35 41 53 68 76
```
3. 实现冒泡排序的算法,并输出{14,1,68,27,55,19,29,20,84,79,23,10,11} 和 { 14, 17, 53, 35, 9, 32, 68, 41, 76, 23}的排序过程
```c++
#include <iostream>
using namespace std;
void BubbleSort(int A[], int n) {
for (int i = 0; i < n - 1; i++) {
bool flag = false;
for (int j = 0; j < n - i - 1; j++) {
if (A[j] > A[j + 1]) {
swap(A[j], A[j + 1]);
flag = true;
}
}
if (!flag) break;
}
}
int main() {
int A[] = {14, 1, 68, 27, 55, 19, 29, 20, 84, 79, 23, 10, 11};
int n = sizeof(A) / sizeof(A[0]);
BubbleSort(A, n);
for (int i = 0; i < n; i++) {
cout << A[i] << " ";
}
cout << endl;
int B[] = {14, 17, 53, 35, 9, 32, 68, 41, 76, 23};
n = sizeof(B) / sizeof(B[0]);
BubbleSort(B, n);
for (int i = 0; i < n; i++) {
cout << B[i] << " ";
}
cout << endl;
return 0;
}
```
输出:
```
1 10 11 14 19 20 23 27 29 55 68 79 84
9 14 17 23 32 35 41 53 68 76
```
4. 输入主函数实现对上面排序函数的调用
```c++
#include <iostream>
using namespace std;
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int length;
} SqList;
void InitList(SqList &L) {
L.length = 0;
}
void CreateList(SqList &L) {
cout << "请输入顺序表的长度:";
cin >> L.length;
cout << "请输入顺序表的元素:";
for (int i = 0; i < L.length; i++) {
cin >> L.data[i];
}
}
void PrintList(SqList L) {
if (L.length == 0) {
cout << "顺序表为空" << endl;
return;
}
for (int i = 0; i < L.length; i++) {
cout << L.data[i] << " ";
}
cout << endl;
}
void SelectSort(int A[], int n) {
for (int i = 0; i < n - 1; i++) {
int min_index = i;
for (int j = i + 1; j < n; j++) {
if (A[j] < A[min_index]) {
min_index = j;
}
}
if (min_index != i) {
swap(A[min_index], A[i]);
}
}
}
void BubbleSort(int A[], int n) {
for (int i = 0; i < n - 1; i++) {
bool flag = false;
for (int j = 0; j < n - i - 1; j++) {
if (A[j] > A[j + 1]) {
swap(A[j], A[j + 1]);
flag = true;
}
}
if (!flag) break;
}
}
int main() {
SqList L;
InitList(L);
CreateList(L);
cout << "初始顺序表:";
PrintList(L);
int A[MAXSIZE], B[MAXSIZE];
for (int i = 0; i < L.length; i++) {
A[i] = L.data[i];
B[i] = L.data[i];
}
SelectSort(A, L.length);
BubbleSort(B, L.length);
cout << "选择排序结果:";
for (int i = 0; i < L.length; i++) {
cout << A[i] << " ";
}
cout << endl << "冒泡排序结果:";
for (int i = 0; i < L.length; i++) {
cout << B[i] << " ";
}
cout << endl;
return 0;
}
```
阅读全文