📄 main.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>
int mergeSort(int p[], int n);
int quickSort(int p[], int n);
int __cdecl compare_int(const void *p1,const void *p2)
{
return *(int*)p1 < *(int*)p2;
}
void main(int ac,char **av)
{
int i;
int count;
int *array;
int op = 0;
struct __timeb64 t1;
struct __timeb64 t2;
if (ac == 1)
{
for (count = 1000; count <= 10000; count += 1000)
{
array = (int*)malloc(sizeof(int) * count);
if (array == NULL)
{
printf("cannot allocate memory for array of count %d", count);
exit(-3);
}
srand((unsigned int)time(NULL));
for (i = 0; i < count; i++)
{
array[i] = rand() % count;
}
_ftime64(&t1);
op = mergeSort(array,count);
_ftime64(&t2);
printf("merge sort count = %d,elapsed time %d ms ",
count,
1000 * (t2.time - t1.time) + t2.millitm - t1.millitm);
printf("operation %d\n",op);
free(array);
srand((unsigned int)time(NULL));
array = (int*)malloc(sizeof(int) * count);
if (array == NULL)
{
printf("cannot allocate memory for array of count %d", count);
exit(-3);
}
srand((unsigned int)time(NULL));
for (i = 0; i < count; i++)
{
array[i] = rand() % count;
}
_ftime64(&t1);
op = quickSort(array,count);
_ftime64(&t2);
printf("quick sort count = %d,elapsed time %d ms ",
count,
1000 * (t2.time - t1.time) + t2.millitm - t1.millitm);
printf("operation %d\n",op);
free(array);
printf("\n",op);
}
}
else if (ac == 3)
{
count = atoi(av[1]);
if (count == 0)
{
printf("arg 1 is wrong\n");
exit(-2);
}
array = (int*)malloc(sizeof(int) * count);
if (array == NULL)
{
printf("cannot allocate memory for array of count %d", count);
exit(-3);
}
srand((unsigned int)time(NULL));
for (i = 0; i < count; i++)
{
array[i] = rand() % count;
}
_ftime64(&t1);
if ( ( strcmpi(av[2],"/M") == 0)
|| ( strcmpi(av[2],"-M") == 0) )
{
op = mergeSort(array,count);
}
else if ( ( strcmpi(av[2],"/Q") == 0)
|| ( strcmpi(av[2],"-Q") == 0) )
{
op = quickSort(array,count);
//qsort((void*)array,count,sizeof(int),compare_int);
}
_ftime64(&t2);
printf("elapsed time %d ms\n",1000 * (t2.time - t1.time) + t2.millitm - t1.millitm);
printf("operation %d\n",op);
free(array);
}
else
{
printf("The command line should be <EXE count mathod>\n");
exit(-1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -