This commit is contained in:
iridiumR 2021-10-28 20:48:14 +08:00
parent 442a76e226
commit 40850ae9b1
3 changed files with 80 additions and 47 deletions

View file

@ -6,9 +6,9 @@ int main()
int flag = 0;
int temp[5];
clock_t astart_t, aend_t;
clock_t astart_t, aend_t, a_start_t, a_end_t;
clock_t bstart_t, bend_t;
clock_t bstart_t, bend_t, b_start_t, b_end_t;
srand(time(NULL));
int len;
@ -16,60 +16,75 @@ int main()
scanf("%d", &len);
printf("生成数组\n");
// scanf("%d", &len);
Vec a(len,1);
Vec b(len,2);
Vec a(len, 1);
Vec b(len, 2);
Vec a_(&a);
Vec b_(&b);
printf("A归并排序...\n");
int amode =0,bmode=0;
printf("排序方法 1merge 2bubble\n");
scanf("%d %d",&amode,&bmode);
astart_t = clock();
a.mergeSort(0, a.getused());
aend_t = clock();
if(amode==1)
{
astart_t = clock();
a.mergeSort(0,a.getused());
aend_t = clock();
}
else if(amode==2)
{
astart_t = clock();
a.bubbleSort();
aend_t = clock();
}
if(bmode==1)
{
bstart_t = clock();
b.mergeSort(0,a.getused());
bend_t = clock();
}
else if(bmode==2)
{
bstart_t = clock();
b.bubbleSort();
bend_t = clock();
}
printf("A运算时间: %.4f 秒\n", ((double)(aend_t - astart_t) / CLOCKS_PER_SEC));
printf("B运算时间: %.4f 秒\n", ((double)(bend_t - bstart_t) / CLOCKS_PER_SEC));
printf("A冒泡排序...\n");
a_start_t = clock();
a_.bubbleSort();
a_end_t = clock();
printf("输出样本A:\n[0-19]\n");
for(int i=0;i<=19;i++)
printf("%d\n",a.get(i));
printf("B归并排序...\n");
bstart_t = clock();
b.mergeSort(0, a.getused());
bend_t = clock();
printf("B冒泡排序...\n");
b_start_t = clock();
b_.bubbleSort();
b_end_t = clock();
printf("A归并运算时间: %.4f 秒\n", ((double)(aend_t - astart_t) / CLOCKS_PER_SEC));
printf("A冒泡运算时间: %.4f 秒\n", ((double)(a_end_t - a_start_t) / CLOCKS_PER_SEC));
printf("B归并运算时间: %.4f 秒\n", ((double)(bend_t - bstart_t) / CLOCKS_PER_SEC));
printf("B冒泡运算时间: %.4f 秒\n", ((double)(b_end_t - b_start_t) / CLOCKS_PER_SEC));
printf("输出归并样本A:\n[0-19]\n");
for (int i = 0; i <= 19; i++)
printf("%d\n", a.get(i));
printf("[50000-50019]\n");
for(int i=50000;i<=50019;i++)
printf("%d\n",a.get(i));
for (int i = 50000; i <= 50019; i++)
printf("%d\n", a.get(i));
printf("[99980-99999]\n");
for(int i=99980;i<=99999;i++)
printf("%d\n",a.get(i));
for (int i = 99980; i <= 99999; i++)
printf("%d\n", a.get(i));
printf("输出样本B:\n[0-19]\n");
for(int i=0;i<=19;i++)
printf("%d\n",b.get(i));
printf("输出冒泡样本A:\n[0-19]\n");
for (int i = 0; i <= 19; i++)
printf("%d\n", a_.get(i));
printf("[50000-50019]\n");
for(int i=50000;i<=50019;i++)
printf("%d\n",b.get(i));
for (int i = 50000; i <= 50019; i++)
printf("%d\n", a_.get(i));
printf("[99980-99999]\n");
for(int i=99980;i<=99999;i++)
printf("%d\n",b.get(i));
for (int i = 99980; i <= 99999; i++)
printf("%d\n", a_.get(i));
printf("输出归并样本B:\n[0-19]\n");
for (int i = 0; i <= 19; i++)
printf("%d\n", b.get(i));
printf("[50000-50019]\n");
for (int i = 50000; i <= 50019; i++)
printf("%d\n", b.get(i));
printf("[99980-99999]\n");
for (int i = 99980; i <= 99999; i++)
printf("%d\n", b.get(i));
printf("输出冒泡样本B:\n[0-19]\n");
for (int i = 0; i <= 19; i++)
printf("%d\n", b_.get(i));
printf("[50000-50019]\n");
for (int i = 50000; i <= 50019; i++)
printf("%d\n", b_.get(i));
printf("[99980-99999]\n");
for (int i = 99980; i <= 99999; i++)
printf("%d\n", b_.get(i));
system("pause");
}

View file

@ -236,3 +236,14 @@ void Vec::merge(int lo, int mi, int hi)
_v[i] = temp[k];
delete[] temp;
}
Vec::Vec(Vec *v)
{
_used = v->getused();
_len = v->getlen();
_v = new int[_len];
for(int i=0;i<=_used;i++)
_v[i]=(*v)[i];
}

View file

@ -23,6 +23,8 @@ private:
public:
Vec(int _len,int mode);
Vec(Vec* v);
int get(int a);
int search(int value);
@ -51,6 +53,11 @@ public:
void mergeSort(int lo, int hi);
int operator[](int i)
{
return _v[i];
}
private:
void merge(int lo, int mi, int hi);
};