排序综合
附 录
// 数据结构3一元多项式的加减乘.cpp : 定义控制台应用程序的入口点。 //
//========================二叉树的遍历============================ //========================执行软件VS2010========================== //========================发布日期:2015-11-28==================== #include \#include
//====================头文件===================== #define M 200 //产生随机数个数的定义 const int MAXSIZE = 20000; typedef struct{
int key;
}RedType; typedef struct{ RedType r[MAXSIZE + 1];
int length;
}Sqlist;
typedef Sqlist HeapType; clock_t start, finish; using namespace std;
27
西华大学理学院课程设计说明书
//////============基本排序算法=================/////// void Merge(RedType SR[], RedType TR[], int i, int m, int n); void MSort(RedType SR[], RedType TR1[], int s, int t); int SelectMin(int a[], int i, int m); int Partition(Sqlist &L, int low, int high); void Qsort(Sqlist &L, int low, int high); void HeapAdjust(HeapType &H, int s, int m); void HeapSort(HeapType &H);
double duration(clock_t x, clock_t y);
/////////=========================================/////////////// void control(int a[]); void menu();
/////////==============具体排序函数================////////// //快速选择排序
void quick_select_sort(int a[]); //冒泡排序
void bubble_sort(int a[]); //插入排序
void insert_sort(int a[]); //归并排序
void merge_sort(int a[]); //简单选择排序
void simple_select_sort(int a[]); //堆排序
void heap_sort(int a[]);
/////////////====================================//////////////// void main() {
28
printf(\
printf(\功 能: 排 序 综 合\\n\\n\
排序综合
printf(\编 译 环 境:V S 2 0 1 0\\n\\n\printf(\发 布 日 期:2 0 1 5 - 1 1 - 2 5\\n\\n\
printf(\程序开始!***********************\\n\int i, arr[M];
//定义i,j;待排序数组arr[M];
srand((unsigned)time(NULL));//设置随机种子; for (i = 1; i < M + 1; i++)
arr[i] = rand() % 5000; //随机生成待排序数组a;
printf(\产生200个随机数\\\\/\\n\system(\
for (i = 1; i < M + 1; i++) //每个元素占6个字符,每行十个元素的格式输出随机生成的待
排序数组a;
{ }
system(\
cout << setw(6) << arr[i] << \if (i % 10 == 0)
printf(\
fflush(stdin);//清除文件缓冲区,文件以写方式打开时将缓冲区内容写入文件;为了确保不影响后面的数据读取; }
void control(int a[]) {
menu(); char ch; while (1) {
scanf_s(\switch (ch) {
29
control(arr);
//字符ch,用于记录选项;
//获取选项,选择相应的排序算法;
西华大学理学院课程设计说明书
30
case '1':
//快速选择排序 quick_select_sort(a); system(\getchar(); menu(); break;
case '2':
//冒泡排序; bubble_sort(a); system(\getchar(); menu(); break;
case '3':
//直接插入排序 insert_sort(a); system(\getchar(); menu(); break;
case '4':
//归并排序算法; merge_sort(a); system(\getchar(); menu(); break;
case '5':
//简单选择排序;
排序综合
simple_select_sort(a); system(\ getchar(); menu();
break;
case '6': //堆排序算法; heap_sort(a); system(\ getchar(); menu();
break;
case '7': //结束程序;
printf(\谢谢您的使用!\\n\ system(\ exit(1); getchar();
break;
default: printf(\输入错误,请重新输入!\\n\ break;
}
}
} //主菜单 void menu() { printf(\菜 单\\n\
printf(\
31