本关任务:编写程序,已知待排序记录的关键字序列,利用简单选择排序进行非递减有序排序,要求输出每一趟排序的结果。
//简单选择排序
#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;
}
//交换两个记录
void swap(RedType &a,RedType &b) {
RedType temp;
temp = a;
a = b;
b = temp;
}
//对顺序表L做简单选择排序
//要求调用show函数打印每一趟排序的结果
void SelectSort(SqList &L) {
show(L);//打印初始待排序序列
/*-------------代码开始------------------*/
for(int i = 1; i < L.length; ++i) {
int minIndex = i;
for(int j = i + 1; j <= L.length; ++j) {
if(L.r[j].key < L.r[minIndex].key) {
minIndex = j;
}
}
if(minIndex != i) {
swap(L.r[i], L.r[minIndex]);
}
show(L);
}
/*-------------代码结束------------------*/
}
int main() {
SqList L;
InitSqList(L);
InsertSqList(L);
SelectSort(L);
return OK;
}
本关任务:编写程序,已知待排序记录的关键字序列,利用堆排序进行非递减有序排序,要求输出每一趟排序的结果。
//堆排序
#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;
}
//交换两个记录
void swap(RedType &a,RedType &b) {
RedType temp;
temp = a;
a = b;
b = temp;
}
//假设L.r[s+1..m]已经是大根堆,将L.r[s..m]调整为以L.r[s]为根的大根堆
void HeapAdjust(SqList &L,int s,int m) {
/*-------------代码开始------------------*/
RedType temp = L.r[s];
for (int j = 2 * s; j <= m; j *= 2) {
if (j < m && L.r[j].key < L.r[j + 1].key) ++j;
if (temp.key >= L.r[j].key) break;
L.r[s] = L.r[j];
s = j;
}
L.r[s] = temp;
/*-------------代码结束------------------*/
}
//把无序序列L.r[1..L.length]建成大根堆
void CreateHeap(SqList &L) {
/*-------------代码开始------------------*/
for (int i = L.length / 2; i > 0; --i) {
HeapAdjust(L, i, L.length);
}
/*-------------代码结束------------------*/
}
//对顺序表L进行堆排序
void HeapSort(SqList &L) {
show(L);//打印初始待排序序列
/*-------------代码开始------------------*/
CreateHeap(L); // 将无序序列建成大根堆
show(L); // 显示创建堆之后的状态
for (int i = L.length; i > 1; --i) {
swap(L.r[1], L.r[i]);
HeapAdjust(L, 1, i - 1);
show(L); // 显示每次调整堆之后的状态
}
/*-------------代码结束------------------*/
}
int main() {
SqList L;
InitSqList(L);
InsertSqList(L);
HeapSort(L);
return OK;
}