实验预备知识:
1. 理解并掌握直接插入排序、折半插入排序、2-路插入排序、起泡法排序和快速排序的基本概念和方法。
2. 掌握排序算法的基本思想。
一、实验目的
- 掌握排序算法基本思想的实现。
- 通过实验掌握直接插入排序、折半插入排序、2-路插入排序的具体实现。
- 掌握交换排序算法基本思想的实现。
- 通过实验掌握起泡法排序和快速排序的具体实现。
二、实验要求
【项目1 –排序算法验证】
用序列{40, 57, 38, 11, 13, 34, 48, 75, 6, 19, 9, 7}作为测试数据,运行相应算法程序,观察运行结果并深刻领会算法的思路和实现方法:
(1)直接插入排序;8-1.cpp
(2)希尔排序;8-2.cpp
(3)冒泡排序;8-3.cpp
(4)快速排序;8-4.cpp
(5)堆排序;8-6.cpp
(6)归并排序;8-7.cpp
(7)基数排序;8-8.cpp
请自行建立排序的算法库。
【项目2 – 在大数据集上进行排序算法性能的体验】
请在给出的参考算法中,用给出的测试数据来测试排序算法运行时间的程序。写成一个主程序调用各排序函数,查看每次排序的结果,并给出各种排序方法的排序时间比较。观察输出结果。得到对复杂度不同的各种算法在运行时间方面的感性认识。本项目旨在获得对于复杂度不同算法的感性认识,由于数据分布特点、计算机运行状态等不同,其结果并不能完全代替对算法复杂度的理论分析;
根据自己的情况可以选择以下两种方案来进行数据测试:
- 请在给出的参考算法中,用给出的测试数据来测试排序算法运行时间的程序。写成一个主程序调用各排序函数,查看每次排序的结果,并给出各种排序方法的排序时间比较。
- 设计一个函数,产生一个至少10万条记录的数据集合。每一种排序方法都单独写成子函数形式,然后用主函数调用它。
代码:
直接插入排序:
main.cpp:
#include <iostream>
#include "head.h"
using namespace std;
int main()
{
int n=0,i=0;
double t1,t2;
RecType R[MaxSize];
FILE *fp1;
fp1=fopen("numbers.txt","r");
if(fp1==NULL)
{
printf("打开文件错!请下载文件,并将之复制到与源程序文件同一文件夹下。\n");
exit(1);
}
while(fscanf(fp1,"%d",&R[n].key)!=EOF)
n++;
printf("数据量: %d,开始排序....",n);
t1=time(0);
InsertSort(R,n);
t2=time(0);
printf("直接插入排序用时 %d 秒",(int)(t2-t1));
printf("\n排序后:");
for (i=0; i<n; i++)
printf("%d ",R[i].key);
printf("\n");
fclose(fp1);
return 0;
}
希尔排序:
main.cpp:
#include <iostream>
#include "head.h"
using namespace std;
int main()
{
int n=0,i=0;
double t1,t2;
RecType R[MaxSize];
FILE *fp1;
fp1=fopen("numbers.txt","r");
if(fp1==NULL)
{
printf("打开文件错!请下载文件,并将之复制到与源程序文件同一文件夹下。\n");
exit(1);
}
while(fscanf(fp1,"%d",&R[n].key)!=EOF)
n++;
printf("数据量: %d,开始排序....",n);
t1=time(0);
ShellSort(R,n);
t2=time(0);
printf("希尔排序用时 %d 秒",(int)(t2-t1));
fclose(fp1);
return 0;
}
冒泡排序:
main.cpp:
#include <iostream>
#include "head.h"
using namespace std;
int main()
{
int n=0,i=0;
double t1,t2;
RecType R[MaxSize];
FILE *fp1;
fp1=fopen("numbers.txt","r");
if(fp1==NULL)
{
printf("打开文件错!请下载文件,并将之复制到与源程序文件同一文件夹下。\n");
exit(1);
}
while(fscanf(fp1,"%d",&R[n].key)!=EOF)
n++;
printf("数据量: %d,开始排序....",n);
t1=time(0);
BubbleSort1(R,n);
t2=time(0);
printf("冒泡排序用时 %d 秒",(int)(t2-t1));
fclose(fp1);
return 0;
}
快速排序:
main.cpp:
#include <iostream>
#include "head.h"
using namespace std;
int main()
{
int n=0,i=0;
double t1,t2;
RecType R[MaxSize];
FILE *fp1;
fp1=fopen("numbers.txt","r");
if(fp1==NULL)
{
printf("打开文件错!请下载文件,并将之复制到与源程序文件同一文件夹下。\n");
exit(1);
}
while(fscanf(fp1,"%d",&R[n].key)!=EOF)
n++;
printf("数据量: %d,开始排序....",n);
t1=time(0);
QuickSort(R,0,n-1);
t2=time(0);
printf("快速排序用时 %d 秒",(int)(t2-t1));
fclose(fp1);
return 0;
}
堆排序:
main.cpp:
#include <iostream>
#include "head.h"
using namespace std;
int main()
{
int n=0,i=0;
double t1,t2;
RecType R[MaxSize];
FILE *fp1;
fp1=fopen("numbers.txt","r");
if(fp1==NULL)
{
printf("打开文件错!请下载文件,并将之复制到与源程序文件同一文件夹下。\n");
exit(1);
}
while(fscanf(fp1,"%d",&R[n].key)!=EOF)
n++;
printf("数据量: %d,开始排序....",n);
t1=time(0);
HeapSort(R,n);
t2=time(0);
printf("堆排序用时 %d 秒",(int)(t2-t1));
fclose(fp1);
return 0;
}
归并排序:
main.cpp:
#include <iostream>
#include "head.h"
using namespace std;
int main()
{
int n=0,i=0;
double t1,t2;
RecType R[MaxSize];
FILE *fp1;
fp1=fopen("numbers.txt","r");
if(fp1==NULL)
{
printf("打开文件错!请下载文件,并将之复制到与源程序文件同一文件夹下。\n");
exit(1);
}
while(fscanf(fp1,"%d",&R[n].key)!=EOF)
n++;
printf("数据量: %d,开始排序....",n);
t1=time(0);
MergeSort(R,n);
t2=time(0);
printf("归并排序用时 %d 秒",(int)(t2-t1));
fclose(fp1);
return 0;
}
运行结果图:
直接插入排序:
希尔排序:
冒泡排序:
快速排序:
堆排序:
归并排序: