第1关:冒泡排序
编程要求
根据提示,在右侧编辑器补充代码,编写对顺序表L做直接插入排序算法的函数,要求调用show函数打印每一趟排序的结果。
- void BubbleSort(SqList &L); //对顺序表L做冒泡排序
测试说明
平台会对你编写的代码进行测试:
测试输入: 10
输入说明: 第一行为整数n,代表待排序的整数的个数,同时也代表随机数的种子,系统自动产生n个整数。
预期输出:
#include<iostream>
#include<fstream>
using namespace std;
#include <time.h>
#define MAXSIZE 100 //顺序表的最大长度
#define OK 0
#define ERROR -1
typedef char* InfoType;
typedef struct {
int key;//关键字项
InfoType otherinfo;//其他数据项
}RedType;//记录类型
typedef struct {
RedType r[MAXSIZE+1];//r[0]闲置或用做哨兵单元
int length;//顺序表长度
}SqList;//顺序表类型
//初始化一个空的顺序表L
void InitSqList(SqList &L) {
L.length = 0;
}
//将待排序记录依次插入顺序表L
void InsertSqList(SqList &L) {
int n;//待排序记录的个数
cin>>n;
srand(n);
if(n > MAXSIZE) exit(ERROR);
for(int i=1; i<=n; ++i) {
L.r[i].key = rand() % 100;
++L.length;
}
}
//打印顺序表L
void show(SqList L) {
for(int i=1; i<=L.length; ++i)
cout<<L.r[i].key<<" ";
cout<<endl;
}
//对顺序表L做冒泡排序
//要求调用show函数打印每一趟排序的结果
void BubbleSort(SqList &L)
{ show(L);//打印初始待排序序列
/*-------------代码开始------------------*/
for (int i = 1; i <= L.length - 1; ++i) {
for (int j = 1; j <= L.length - i; ++j) {
if (L.r[j].key > L.r[j + 1].key) {
RedType temp = L.r[j];
L.r[j] = L.r[j + 1];
L.r[j + 1] = temp;
}
}
show(L); // 打印每一趟排序的结果
}
/*-------------代码结束------------------*/
}
int main() {
SqList L;
InitSqList(L);
InsertSqList(L); //测试数据
BubbleSort(L);
return OK;
}
第2关:快速排序
编程要求
根据提示,在右侧编辑器补充代码,编写对顺序表L做快速排序算法的函数,要求调用show函数打印每一趟排序的结果。
- int Partition(SqList &L,int low,int high) ; //以L.r[low]作为枢轴,对顺序表L中的子序列L.r[low..high]进行划分,返回枢轴的位置
- void QSort(SqList &L,int low,int high) ; //对顺序表L中的序列L.r[low..high]做快速排序
测试说明
平台会对你编写的代码进行测试:
测试输入: 10
输入说明: 第一行为整数
n
,代表待排序的整数的个数,同时也代表随机数的种子,系统自动产生n
个整数插入顺序表中。
#include<iostream>
#include<fstream>
using namespace std;
#include <time.h>
#define MAXSIZE 100 //顺序表的最大长度
#define OK 0
#define ERROR -1
typedef char* InfoType;
typedef struct {
int key;//关键字项
InfoType otherinfo;//其他数据项
}RedType;//记录类型
typedef struct {
RedType r[MAXSIZE+1];//r[0]闲置或用做哨兵单元
int length;//顺序表长度
}SqList;//顺序表类型
//初始化一个空的顺序表L
void InitSqList(SqList &L) {
L.length = 0;
}
//将待排序记录依次插入顺序表L
void InsertSqList(SqList &L) {
int n;//待排序记录的个数
cin>>n;
srand(n);
if(n > MAXSIZE) exit(ERROR);
for(int i=1; i<=n; ++i) {
L.r[i].key = rand() % 100;
++L.length;
}
}
//打印顺序表L
void show(SqList L) {
for(int i=1; i<=L.length; ++i)
cout<<L.r[i].key<<" ";
cout<<endl;
}
//对顺序表L中的子序列L.r[low..high]进行划分,返回枢轴的位置
//以L.r[low]作为枢轴
int Partition(SqList &L,int low,int high) {
/*-------------代码开始------------------*/
int pivot = L.r[low].key; // 选择第一个元素作为枢轴
while (low < high) {
// 从右向左找到第一个小于枢轴的元素
while (low < high && L.r[high].key >= pivot) --high;
// 将找到的元素移动到枢轴的左边
L.r[low] = L.r[high];
// 从左向右找到第一个大于枢轴的元素
while (low < high && L.r[low].key <= pivot) ++low;
// 将找到的元素移动到枢轴的右边
L.r[high] = L.r[low];
}
// 将枢轴元素放到正确的位置
L.r[low] = {pivot, L.r[low].otherinfo};
return low; // 返回枢轴的位置
/*-------------代码结束------------------*/
}
//对顺序表L中的子序列L.r[low..high]做快速排序
//要求调用show函数打印每一趟划分的结果
void QSort(SqList &L,int low,int high) {
/*-------------代码开始------------------*/
if (low < high) {
int pivotIndex = Partition(L, low, high); // 划分并获取枢轴位置
show(L); // 打印当前划分的结果
QSort(L, low, pivotIndex - 1); // 对枢轴左边的子序列进行快速排序
QSort(L, pivotIndex + 1, high); // 对枢轴右边的子序列进行快速排序
}
/*-------------代码结束------------------*/
}
//对顺序表L做快速排序
void QuickSort(SqList &L) {
show(L);//打印初始待排序序列
QSort(L,1,L.length);
}
int main() {
SqList L;
InitSqList(L);
InsertSqList(L);
QuickSort(L);
return OK;
}