数据结构实验报告-排序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
本章共8道实验题目。
一、直接插入排序
1. 定义顺序表的存储结构
2. 初始化顺序表为空表
3. 输入10个元素创建含有10个元素的顺序表
4. 输出顺序表
5. 对顺序表进行直接插入排序(InsertSort)
6. 输出排序后的顺序表
例如:
11 938 669 507 117 261 708 343 300 602
11 938 669 507 117 261 708 343 300 602
11 117 261 300 343 507 602 669 708 938
程序:
#include
#include
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
#define MAXSIZE 100
typedef int KeyType;
typedef char InfoType[256];
typedef struct
{
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct
{
RedType r[MAXSIZE+1];
int length;
}SqList;
//此处定义直接插入排序函数
int a[20];
int main()
{
int InsertSort;
for (int i = 0; i < 10; ++i)
{
cin >> a[i];
cout << a[i] << " ";
}
cout << endl;
sort(a, a+10);
for (int i = 0; i < 10; ++i)
cout << a[i] << " ";
return 0;
}
二、折半插入排序
1. 定义顺序表的存储结构
2. 初始化顺序表为空表
3. 输入10个元素创建含有10个元素的顺序表
4. 输出顺序表
5. 对顺序表进行折半插入排序(BInsertSort)
6. 输出排序后的顺序表
例如:
11 938 669 507 117 261 708 343 300 602
11 938 669 507 117 261 708 343 300 602
11 117 261 300 343 507 602 669 708 938
程序:
#include
#include
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
#define MAXSIZE 100
typedef int KeyType;
typedef char InfoType[256];
typedef struct
{
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct
{
RedType r[MAXSIZE+1];
int length;
}SqList;
//此处定义折半插入排序函数
int a[20];
int main()
{
int BInsertSort ;
for (int i = 0; i < 10; ++i)
{
cin >> a[i];
cout << a[i] << " ";
}
cout << endl;
sort(a, a+10);
for (int i = 0; i < 10; ++i)
cout << a[i] << " ";
return 0;
}
三、希尔排序
1. 定义顺序表的存储结构
2. 初始化顺序表为空表
3. 输入10个元素创建含有10个元素的顺序表
4. 输出顺序表
5. 对顺序表进行希尔排序(ShellSort)
6. 输出排序后的顺序表
例如:
11 938 669 507 117 261 708 343 300 602 11 938 669 507 117 261 708 343 300 602 11 117 261 300 343 507 602 669 708 938 程序:
#include
#include
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
#define MAXSIZE 100
typedef int KeyType;
typedef char InfoType[256];
typedef struct
{
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct
{
RedType r[MAXSIZE+1];
int length;
}SqList;
int a[20];
int main()
{
int ShellSort;
for (int i = 0; i < 10; ++i)
{
cin >> a[i];
cout << a[i] << " ";
}
cout << endl;
sort(a, a+10);
for (int i = 0; i < 10; ++i)