基于easyx的自动排序系统(C语言)
工具:vsvs2019,easyx
主要功能:
-
进行浮点型和整型的十种排序算法
-
从文件中导入数据进行排序
-
对十种排序算法进行比较,推荐出在当前数据情况下最优的一种
-
将十种排序所花费的时间显示在控制台中
-
可以将数据导入到excel中
注意:
- 由于堆排序的特殊性,需要更改栈的大小,以防因递归过深而报错
- 需要提前在指定位置下载图片,方便倒入到图形化界面中
- 需要提前创建excel,text文件
代码块:
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<malloc.h>
#include<math.h>
#include<easyx.h>
int number[10] = { 0,0,0,0,0,0,0,0,0,0 };
double Total_time[7] = { 0,0,0,0,0,0,0 };
int number1[10] = { 0,0,0,0,0,0,0,0,0,0 };
double Total_time1[9] = { 0,0,0,0,0,0,0,0,0 };
int cunt = 0;
void Print(int* p, int n)
{
int a = 0;
printf("%d", p[a]);
for (a = 1; a < n; a++)
{
if (a % 100 != 0)
printf(" ");
if (a % 100 == 0)
printf("\n");
printf("%d", p[a]);
}
}
char name1[10][20] = { "冒泡排序",
"选择排序",
"插入排序",
"希尔排序",
"快速排序",
"归并排序",
"堆排序" };
char name2[10][20] = { "冒泡排序",
"选择排序",
"插入排序",
"希尔排序",
"归并排序",
"快速排序",
"计数排序",
"桶排序",
"基数排序",
};
int min_(double p[], int n)
{
int i, t = 0;
double tmp;
tmp = p[1];
for (i = 0; i < n; i++)
{
if (p[i] < tmp)
{
tmp = p[i];
t = i;
}
}
return t;
}
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void Random(int min, int max, int n, double* num)//生成随机数组
{
int i;
srand((unsigned)time(NULL));
printf("生成随机数\n");
for (i = 0; i < n; i++)
{
double RandNum = (rand() / (RAND_MAX + 1.0) * (max - min) + min);
num[i] = RandNum;
//printf("%lf ", RandNum);
}
printf("生成完成\n");
}
void Random1(int min, int max, int n, int* num)//生成随机数组
{
int i;
srand((unsigned)time(NULL));
printf("生成随机数\n");
for (i = 0; i < n; i++)
{
int RandNum = (rand() / (RAND_MAX + 1.0) * (max - min) + min);
num[i] = RandNum;
//printf("%d ", RandNum);
}
//printf("生成完成\n");
}
void copy1(int array[], int array2[], int begin, int end)
{
for (int i = 0; i < end + 1; i++)
{
array[i] = array2[i];
}
}
int get1(double* a, int n)
{
FILE* fp;
fp = fopen("E:\\1.txt", "r");
if (fp == NULL)
{
printf("file is error.");
return 0;
}
for (int j = 0; j < n; j++)
{
fscanf_s(fp, "%lf", &a[j]);
}
fclose(fp);
return 0;
}
int get2(int* a, int n)
{
FILE* fp;
fp = fopen("E:\\3.txt", "r");
if (fp == NULL)
{
printf("file is error.");
return 0;
}
for (int j = 0; j < n; j++)
{
fscanf_s(fp, "%d", &a[j]);
}
fclose(fp);
return 0;
}
int put(double* p, int n)
{
FILE* fp;
int i;//i为数组元素下标
fp = fopen("E:\\2.txt", "w");
if (fp == NULL)
{
printf("Can't open the file!\n");
return 0;
}
for (i = 0; i < n; i++)
{
fprintf(fp, "%lf", p[i]);
}
fclose(fp);
return 0;
}
void zhibiao(int n)
{
char name1[10][20] = { "冒泡排序",
"选择排序",
"插入排序",
"希尔排序",
"恩泽快速排序",
"归并排序",
"堆排序" };
int i;
FILE* fp;
fp = fopen("E:\\3.csv", "w+");
fprintf(fp, "排序法,交换次数,时间\n");
for (i = 0; i < n; i++)
{
fprintf(fp, "%s,%d,%f\n", name1[i], number[i], Total_time[i]);
}
}
void zhibiao1(int n)
{
char name2[10][20] = { "冒泡排序",
"选择排序",
"插入排序",
"希尔排序",
"归并排序",
"快速排序",
"计数排序",
"桶排序",
"基数排序",
};
int i;
FILE* fp;
fp = fopen("E:\\4.csv", "w+");
fprintf(fp, "排序法,交换次数,时间\n");
for (i = 0; i < n; i++)
{
fprintf(fp, "%s,%d,%f\n", name2[i], number1[i], Total_time1[i]);
}
}
void BubbleSort(double* arr, int size)//冒泡法
{
int i, j;
double tmp;
for (i = 0; i < size - 1; i++)
{
for (j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
number[0]++;
}
}
}
}
void SelectionSort(double* arr, int size)//选择排序
{
int i, j, k;
double tmp;
for (i = 0; i < size - 1; i++)
{
k = i;
for (j = i + 1; j < size; j++)
{
if (arr[j] < arr[k])
{
k = j;
}
}
tmp = arr[k];
arr[k] = arr[i];
arr[i] = tmp;
number[1]++;
}
}
void InsertionSort(double* arr, int size)//插入排序
{
int i, j;
double tmp;
for (i = 1; i < size; i++)
{
if (arr[i] < arr[i - 1])
{
tmp = arr[i];
for (j = i - 1; j >= 0 && arr[j] > tmp; j--)
{
arr[j + 1] = arr[j];
number[2]++;
}
arr[j + 1] = tmp;
}
}
}
void ShellSort(double* arr, int size)//希尔排序
{
int i, j, increment;
double tmp;
for (increment = size / 2; increment > 0; increment /= 2)
{
for (i = increment; i < size; i++)
{
tmp = arr[i];
for (j = i - increment; j >= 0 && tmp < arr[j]; j -= increment)
{
arr[j + increment] = arr[j];
number[3]++;
}
arr[j + increment] = tmp;
}
}
}
void QuickSort(double array[], int begin, int end)//快速排序
{
int c;
double* a;
a = array + begin;
double* b;
b = array + end;
double compare = array[begin];
if (begin < end) {
for (;;)
{
if (*b <= compare)
{
*a = *b;
number[4]++;
a++;
for (;;) {
if (a == b) {
*a = compare;
number[4]++;
break;
}
if (*a >= compare) {
*b = *a;
number[4]++;
break;
}
else { a++; }
}
}
else {
b--;
}
if (a == b) {
*a = compare;
number[4]++;
c = b - array;
break;
}
}
QuickSort(array, c + 1, end);
QuickSort(array, begin, c - 1);
}
}
void copy(double array[], double array2[], int begin, int end)
{
for (int i = 0; i < end + 1; i++)
{
array[i] = array2[i];
}
}
void Merge(double* k, int start, int mid, int end, double* p)
{
//start到mid为数组1,mi+1到end为数学2
int a = start, j = mid + 1, b = 0;
while ((a <= mid) && (j <= end))
{
//从两个数最低位开始比较,数值小的存入临时数组
p[b++] = (k[a] <= k[j]) ? k[a++] : k[j++], cunt++;
number[5]++;
}
while (a <= mid)
{
p[b++] = k[a++];
number[5]++;
}
while (j <= end)
{
p[b++] = k[j++];
number[5]++;
}
b = 0;
//将排序完成的临时数组拷贝到原数组
for (a = start; a <= end; a++)
k[a] = p[b++];
number[5]++;
}
void MergeSoft(double* k, int start, int end, double* p)
{
int mid;
//当数组至少有两个元素时,进行二路归并
if (start < end)
{
mid = (start + end) / 2;
MergeSoft(k, start, mid, p);
MergeSoft(k, mid + 1, end, p);
Merge(k, start, mid, end, p);
}
}
void heapify(double array[], int n, int total)
{
int max = n;
if ((2 * max <= total) && (array[max - 1] < array[2 * max]))
{
double temp;
temp = array[max - 1];
array[max - 1] = array[2 * max];
array[2 * max] = temp;
number[6]++;
}
if (((2 * max - 1) <= total) && (array[max - 1] < array[2 * max - 1]))
{
double temp;
temp = array[max - 1];
array[max - 1] = array[2 * max - 1];
array[2 * max - 1] = temp;
number[6]++;
}
if (n > 1)
{
heapify(array, n - 1, total);
}
else
{
return;
}
}
void victory(double array[], double array_1[], int a)
{
for (int b = 0; b < a; b++)
{
if ((a - b) / 2 != 0)
{
heapify(array, (a - b) / 2, a);
}
array_1[b] = array[0];
for (int i = 0; i + 1 < a; i++)
{
array[i] = array[i + 1];
}
}
}
void output(double* a, int size)//输出
{
int i;
for (i = 0; i < size; i++)
{
printf("%lf ", a[i]);
}
printf("\n");
}
void output1(int* a, int size)//输出
{
int i;
for (i = 0; i < size; i++)
{
printf("%d ", a[i]);
}
//printf("\n");
}
void randone(int* k, int n)
{
int a, b, swap;
for (a = 0; a < n - 1; a++)
for (b = 0; b < n - a - 1; b++)
{
if (k[b] > k[b + 1])
{
swap = k[b + 1];
k[b + 1] = k[b];
k[b] = swap;
number1[0]++;
}
}
}//冒泡排序法
void randtwo(int* k, int n)
{
int a, b, c, swap, cunt = 0;
for (a = 0; a < n - 1; a++)
{
b = a;
for (c = a + 1; c < n; c++)
{
if (k[c] < k[b])
{
swap = k[b];
k[b] = k[c];
k[c] = swap;
number1[1]++;
}
}
}
}//选择排序法
void randthree(int* k, int n)
{
int a, b, swap;
for (a = 0; a < n - 1; a++)
{
for (b = a; b < n; b++)
{
if (k[a] > k[b])
{
swap = k[b];
k[b] = k[a];
k[a] = swap;
number1[2]++;
}
}
}
}//插入排序法
void randfour(int* k, int n)
{
int a, b, c, swap;
for (c = n / 2; c >= 1; c = c / 2)
{
for (a = 0, b = 0; a < c || (c == 1 && a < n); a++)
{
for (b = a + c; b < n; b = b + c)
{
if (k[a] > k[b])
{
swap = k[b];
k[b] = k[a];
k[a] = swap;
number1[3]++;
}
}
}
}
}//希尔排序法
void Merge1(int* k, int start, int mid, int end, int* p)
{
//start到mid为数组1,mi+1到end为数学2
int a = start, j = mid + 1, b = 0;
while ((a <= mid) && (j <= end))
{
//从两个数最低位开始比较,数值小的存入临时数组
p[b++] = (k[a] <= k[j]) ? k[a++] : k[j++], number1[4]++;
}
while (a <= mid)
{
p[b++] = k[a++];
}
while (j <= end)
{
p[b++] = k[j++];
}
b = 0;
//将排序完成的临时数组拷贝到原数组
for (a = start; a <= end; a++)
k[a] = p[b++];
}
void merge_soft1(int* k, int start, int end, int* p)
{
int mid;
//当数组至少有两个元素时,进行二路归并
if (start < end)
{
mid = (start + end) / 2;
merge_soft1(k, start, mid, p);
merge_soft1(k, mid + 1, end, p);
Merge1(k, start, mid, end, p);
}
}
void MergeSort1(int* k, int i)
{
int* p = (int*)calloc(i + 1, sizeof(int));
merge_soft1(k, 0, i - 1, p);
//Print(k,i);
//printf("一共交换%d次\n\n", number1[4]);
}//归并排序法
void randsix_s(int* k, int begin, int end)
{
if (begin >= end)
return;
int l = begin, r = end;
int swap = k[l];
while (l < r)
{
while (l < r && k[r] >= swap)
{
r--;
}
if (l < r)
{
k[l] = k[r];
number[5]++;
}
while (l < r && k[l] <= swap)
l++;
if (l < r)
{
k[r] = k[l];
}
if (l >= r)
{
k[l] = swap;
number1[5]++;
}
}
randsix_s(k, begin, r - 1);
randsix_s(k, r + 1, end);
}
void randsix(int* k, int begin, int end, int n)
{
cunt = 0;
randsix_s(k, 0, n - 1);
//Print(k);
}//快速排序法
void CountingSort(int* k, int i)
{
int a;
int max = k[0];
for (a = 1; a < i; a++)
{
if (k[a] > max)
max = k[a];
}
int* number = (int*)calloc(max + 1, sizeof(int));
for (a = 0; a < i; a++)
number[k[a]]++;//计数
for (a = 1; a < max + 1; a++)
number[a] = number[a] + number[a - 1];//累计值
int* output = (int*)calloc(i, sizeof(int));
for (a = 0; a < i; a++)
{
output[number[k[a]] - 1] = k[a];
number[k[a]]--;
}
//Print(output,i);输出
free(number);
free(output);
}//计数排序法
void BucketSort(int* k, int n)
{
int a, b;
int max = k[0];
for (a = 1; a < n; a++)
{
if (k[a] > max)
max = k[a];
}
int* number = (int*)calloc(max + 1, sizeof(int));
for (a = 0; a < n; a++)
{
number[k[a]]++;
}
for (a = 0; a <= max; a++)
{
for (b = 0; b < number[a]; b++);
/*printf("%d ", a);*/
}
}//桶排序法
void RadixSort(int* k, int n)
{
{
int max_digit = 0;
int a, j;
int radix = 10;
//求数组中的最大位数
for (a = 0; a < n; ++a)
{
while (k[a] > (pow(10, max_digit)))
{
max_digit++;
}
}
int flag = 1;
for (j = 1; j <= max_digit; j++)
{
//建立数组 统计每个位上不同数字出现的次数
int digit_count_arr[10] = { 0 };
for (a = 0; a < n; a++)
digit_count_arr[(k[a] / flag) % radix]++;
//建立数组 统计A[i]在temp中的起始下标
int BeginIndex[10] = { 0 };
for (a = 1; a < radix; a++)
BeginIndex[a] = BeginIndex[a - 1] + digit_count_arr[a - 1];
//建立临时数组 用于收集数据
int* tmp = (int*)malloc(sizeof(int) * n);
//初始化
for (a = 0; a < n; a++)
tmp[a] = 0;
//将数据写入临时数组
for (a = 0; a < n; a++)
{
int index = (k[a] / flag) % 10;
tmp[BeginIndex[index]++] = k[a]; // 写入一个同时要把该位置上的计数加1,因为同一个位置可能有多个元素
}
//将数据重新写回原数组
for (a = 0; a < n; a++)
k[a] = tmp[a];
flag = flag * 10; // 用于下一位的排序
free(tmp);
}
}
//Print(k,n);
}//基数排序法
void button(int x, int y, int w, int h, const char* text)
{
setbkmode(TRANSPARENT);
setfillcolor(BROWN);
fillroundrect(x, y, x + w, y + h, 10, 10);
settextstyle(30, 0, _T("黑体"));
char text_[50] = "按钮";
strcpy(text_, text);
int tx = x + (w - textwidth(_T("按钮"))) / 2;
int ty = y + (h - textheight(_T("按钮"))) / 2;
outtextxy(x + 20, y + 10, _T("生成浮点随机数"));
}
void button_1(int x, int y, int w, int h, const char* text)
{
setbkmode(TRANSPARENT);
setfillcolor(BROWN);
fillroundrect(x, y, x + w, y + h, 10, 10);
settextstyle(30, 0, _T("黑体"));
char text_[50] = "按钮";
strcpy(text_, text);
int tx = x + (w - textwidth(_T("按钮"))) / 2;
int ty = y + (h - textheight(_T("按钮"))) / 2;
outtextxy(x + 20, y + 10, _T("生成整型随机数"));
}
void button_2(int x, int y, int w, int h, const char* text)
{
setbkmode(TRANSPARENT);
setfillcolor(BROWN);
fillroundrect(x, y, x + w, y + h, 10, 10);
settextstyle(30, 0, _T("黑体"));
char text_[50] = "按钮";
strcpy(text_, text);
int tx = x + (w - textwidth(_T("按钮"))) / 2;
int ty = y + (h - textheight(_T("按钮"))) / 2;
outtextxy(x + 20, y + 10, _T("导入文本文件"));
}
void button_3(int x, int y, int w, int h, const char* text)
{
setbkmode(TRANSPARENT);
setfillcolor(BROWN);
fillroundrect(x, y, x + w, y + h, 10, 10);
settextstyle(30, 0, _T("黑体"));
char text_[50] = "按钮";
strcpy(text_, text);
int tx = x + (w - textwidth(_T("按钮"))) / 2;
int ty = y + (h - textheight(_T("按钮"))) / 2;
outtextxy(x + 20, y + 10, _T("导入文本文件"));
}
int main(void)
{
int t;
printf("选择数据类型\n");
printf("0-----int型\n");
printf("1--double型\n");
//scanf_s("%d", &t);
int max, min, n;
initgraph(803, 526, EW_SHOWCONSOLE);//设置X,Y坐标值,并让图形化页面和控制台一起
IMAGE img;
loadimage(&img, L"D:\\桌面\\照片保存\\Snipaste_2022-04-14_21-26-29.png", 803, 526);//绝对路径
putimage(0, 0, &img);
setbkmode(TRANSPARENT);//把文字变成透明
settextstyle(60, 0, _T("楷体"));
outtextxy(300, 50, L"排序系统");//打印字体
button(550, 175, 250, 50, "按钮");
button_1(550, 275, 250, 50, "按钮");
button_2(550, 375, 250, 50, "按钮");
ExMessage msg;//鼠标操作
while (1) {
if (peekmessage(&msg, EM_MOUSE))
{
switch (msg.message) {
case WM_LBUTTONDOWN:
if (msg.x >= 550 && msg.x <= 550 + 250 && msg.y >= 175 && msg.y <= 170 + 50) {
cleardevice();//清屏
loadimage(&img, L"D:\\桌面\\照片保存\\Snipaste_2022-04-14_21-26-29.png", 803, 526);//绝对路径
putimage(0, 0, &img);
setbkmode(TRANSPARENT);//把文字变成透明
settextstyle(60, 0, _T("楷体"));
outtextxy(300, 50, L"排序系统");//打印字体
settextcolor(RGB(40, 99, 85));//设置字体颜色,可以用ctrl+Alt+A来提取色块
settextstyle(40, 0, _T("楷体"));
outtextxy(200, 150, L"请输入随机数大小与范围");//打印字体
int x;
int begin = 0, end;
double* num1, * num2, * num3;
printf("输入数据总数\n");
scanf_s("%d", &n);
end = n - 1;
num1 = (double*)malloc(n * sizeof(double));
num2 = (double*)malloc(n * sizeof(double));
num3 = (double*)malloc(n * sizeof(double));
printf("输入数据区间\n");
scanf_s("%d%d", &max, &min);
Random(max, min, n, num1);
clock_t start, finish;
copy(num2, num1, begin, end);
{
start = clock();
BubbleSort(num2, n);
finish = clock();
Total_time[0] = (double)(finish - start) / CLOCKS_PER_SEC;
printf("冒泡排序所用时间");
printf("%f", Total_time[0]);
printf(" 交换的次数:%d\n", number[0]);
copy(num2, num1, begin, end);
}//冒泡排序
{
start = clock();
SelectionSort(num2, n);
finish = clock();
Total_time[1] = (double)(finish - start) / CLOCKS_PER_SEC;
printf("选择排序所用时间");
printf("%f", Total_time[1]);
printf(" 交换的次数:%ld\n", number[1]);
copy(num2, num1, begin, end);
}//选择排序
{
start = clock();
InsertionSort(num2, n);//插入排序
finish = clock();
Total_time[2] = (double)(finish - start) / CLOCKS_PER_SEC;
printf("插入排序所用时间");
printf("%f", Total_time[2]);
printf(" 交换的次数:%d\n", number[2]);
copy(num2, num1, begin, end);
}//插入排序
{
start = clock();
ShellSort(num2, n);//插入排序
finish = clock();
Total_time[3] = (double)(finish - start) / CLOCKS_PER_SEC;
printf("希尔排序所用时间");
printf("%f", Total_time[3]);
printf(" 交换的次数:%d\n", number[3]);
copy(num2, num1, begin, end);
}//希尔排序
{
start = clock();
QuickSort(num2, 0, end);
finish = clock();
Total_time[4] = (double)(finish - start) / CLOCKS_PER_SEC;
printf("恩泽快速排序所用时间");
printf("%f", Total_time[4]);
printf(" 交换的次数:%d\n", number[4]);
copy(num2, num1, begin, end);
}//快速排序
{
start = clock();
copy(num3, num2, 0, end);
MergeSoft(num2, 0, end, num3);
finish = clock();
Total_time[5] = (double)(finish - start) / CLOCKS_PER_SEC;
printf("归并排序所用时间");
printf("%f", Total_time[5]);
printf(" 交换的次数:%d\n", number[5]);
copy(num2, num1, begin, end);
}//归并排序
{
start = clock();
copy(num3, num2, 0, end);
victory(num3, num2, n);
finish = clock();
Total_time[6] = (double)(finish - start) / CLOCKS_PER_SEC;
printf("堆排序所用时间");
printf("%f", Total_time[6]);
printf(" 交换的次数:%d\n", number[6]);
}//堆排序
printf("最快的排序方法是%s,用时%f秒", name1[min_(Total_time,7)], Total_time[min_(Total_time,7)]);
zhibiao(7);
free(num1);
free(num2);
free(num3);
cleardevice();//清屏
loadimage(&img, L"D:\\桌面\\照片保存\\Snipaste_2022-04-14_21-26-29.png", 803, 526);//绝对路径
putimage(0, 0, &img);
setbkmode(TRANSPARENT);//把文字变成透明
settextstyle(60, 0, _T("楷体"));
outtextxy(300, 50, L"排序系统");//打印字体
settextcolor(RGB(201, 232, 224));//设置字体颜色,可以用ctrl+Alt+A来提取色块
outtextxy(230, 150, L"查看结果");//打印字体
break;
}
if (msg.x >= 550 && msg.x <= 550 + 250 && msg.y >= 275 && msg.y <= 270 + 50) {
cleardevice();//清屏
loadimage(&img, L"D:\\桌面\\照片保存\\Snipaste_2022-04-14_21-26-29.png", 803, 526);//绝对路径
putimage(0, 0, &img);
setbkmode(TRANSPARENT);//把文字变成透明
settextstyle(60, 0, _T("楷体"));
outtextxy(300, 50, L"排序系统");//打印字体
settextcolor(RGB(40, 99, 85));//设置字体颜色,可以用ctrl+Alt+A来提取色块
outtextxy(200, 150, L"请输入随机数大小与范围");//打印字体
int x;
int begin1 = 0, end1;
int* num4, * num5, * num6;
printf("输入数据总数\n");
scanf_s("%d", &n);
end1 = n - 1;
num4 = (int*)malloc(n * sizeof(double));
num5 = (int*)malloc(n * sizeof(double));
num6 = (int*)malloc(n * sizeof(double));
printf("输入数据区间\n");
scanf_s("%d%d", &max, &min);
Random1(max, min, n, num4);
clock_t start1, finish1;
copy1(num5, num4, begin1, end1);
{
start1 = clock();
randone(num5, n);
finish1 = clock();
Total_time1[0] = (double)(finish1 - start1) / CLOCKS_PER_SEC;
printf("冒泡排序所用时间");
printf("%f", Total_time1[0]);
printf(" 交换的次数:%d\n", number1[0]);
copy1(num5, num4, begin1, end1);
}//冒泡排序
{
start1 = clock();
randtwo(num5, n);
finish1 = clock();
Total_time1[1] = (double)(finish1 - start1) / CLOCKS_PER_SEC;
printf("选择排序所用时间");
printf("%f", Total_time1[1]);
printf(" 交换的次数:%d\n", number1[1]);
copy1(num5, num4, begin1, end1);
}//选择排序
{
start1 = clock();
randthree(num5, n);//插入排序
finish1 = clock();
Total_time1[2] = (double)(finish1 - start1) / CLOCKS_PER_SEC;
printf("插入排序所用时间");
printf("%f", Total_time1[2]);
printf(" 交换的次数:%d\n", number1[2]);
copy1(num5, num4, begin1, end1);
}//插入排序
{
start1 = clock();
randfour(num5, n);//插入排序
finish1 = clock();
Total_time1[3] = (double)(finish1 - start1) / CLOCKS_PER_SEC;
printf("希尔排序所用时间");
printf("%f", Total_time1[3]);
printf(" 交换的次数:%d\n", number1[3]);
copy1(num5, num4, begin1, end1);
}//希尔排序
{
start1 = clock();
MergeSort1(num5, n);
finish1 = clock();
Total_time1[4] = (double)(finish1 - start1) / CLOCKS_PER_SEC;
printf("归并排序所用时间");
printf(" %f", Total_time1[4]);
printf("交换的次数:%d\n", number1[4]);
copy1(num5, num4, begin1, end1);
}//归并排序
{
start1 = clock();
randsix(num5, 0, end1, n);
finish1 = clock();
Total_time1[5] = (double)(finish1 - start1) / CLOCKS_PER_SEC;
printf("快速排序所用时间");
printf("%f", Total_time1[4]);
printf(" 交换的次数:%d\n", number1[5]);
copy1(num5, num4, begin1, end1);
}//快速排序
{
start1 = clock();
CountingSort(num5, n);
finish1 = clock();
Total_time1[6] = (double)(finish1 - start1) / CLOCKS_PER_SEC;
printf("计数排序所用时间");
printf("%f\n", Total_time1[6]);
//printf(" 交换的次数:%d\n", number1[6]);
copy1(num5, num4, begin1, end1);
}//计数排序法
{
start1 = clock();
BucketSort(num5, n);
finish1 = clock();
Total_time1[7] = (double)(finish1 - start1) / CLOCKS_PER_SEC;
printf("桶排序所用时间");
printf("%f\n", Total_time1[7]);
//printf("交换的次数:%d\n", number1[0]);
copy1(num5, num4, begin1, end1);
}//桶排序
{
start1 = clock();
RadixSort(num5, n);
finish1 = clock();
Total_time1[8] = (double)(finish1 - start1) / CLOCKS_PER_SEC;
printf("基数排序所用时间");
printf("%f\n", Total_time1[8]);
//printf("交换的次数:%d\n", number1[0]);
copy1(num5, num4, begin1, end1);
}//基数排序法
printf("最快的排序方法是%s,用时%f秒", name2[min_(Total_time1,9)], Total_time1[min_(Total_time1,9)]);
zhibiao1(9);
free(num4);
free(num5);
free(num6);
cleardevice();//清屏
loadimage(&img, L"D:\\桌面\\照片保存\\Snipaste_2022-04-14_21-26-29.png", 803, 526);//绝对路径
putimage(0, 0, &img);
setbkmode(TRANSPARENT);//把文字变成透明
settextstyle(60, 0, _T("楷体"));
outtextxy(300, 50, L"排序系统");//打印字体
settextcolor(RGB(201, 232, 224));//设置字体颜色,可以用ctrl+Alt+A来提取色块
outtextxy(230, 150, L"请查看结果");//打印字体
break;
}
if (msg.x >= 550 && msg.x <= 550 + 250 && msg.y >= 375 && msg.y <= 370 + 50) {
cleardevice();//清屏
loadimage(&img, L"D:\\桌面\\照片保存\\Snipaste_2022-04-14_21-26-29.png", 803, 526);//绝对路径
putimage(0, 0, &img);
setbkmode(TRANSPARENT);//把文字变成透明
settextstyle(60, 0, _T("楷体"));
outtextxy(300, 50, L"排序系统");//打印字体
settextcolor(RGB(40, 99, 85));//设置字体颜色,可以用ctrl+Alt+A来提取色块
outtextxy(200, 150, L"请输入文件数字范围");//打印字体
int x;
int begin = 0, end;
double* num1, * num2, * num3;
printf("输入数据总数\n");
scanf_s("%d", &n);
end = n - 1;
num1 = (double*)malloc(n * sizeof(double));
num2 = (double*)malloc(n * sizeof(double));
num3 = (double*)malloc(n * sizeof(double));
get1(num1, n);
printf("文件中的原始数据\n");
output(num1, n);
printf("排序后的数据\n");
InsertionSort(num1, n);//插入排序
output(num1, n);
cleardevice();//清屏
loadimage(&img, L"D:\\桌面\\照片保存\\Snipaste_2022-04-14_21-26-29.png", 803, 526);//绝对路径
putimage(0, 0, &img);
setbkmode(TRANSPARENT);//把文字变成透明
settextstyle(60, 0, _T("楷体"));
outtextxy(300, 50, L"排序系统");//打印字体
settextcolor(RGB(201, 232, 224));//设置字体颜色,可以用ctrl+Alt+A来提取色块
outtextxy(230, 150, L"请查看结果");//打印字体
break;
}
default:
break;
}
}
}
getchar();
closegraph();
}