算法实验.docx

上传人:b****7 文档编号:10902092 上传时间:2023-02-23 格式:DOCX 页数:18 大小:24.13KB
下载 相关 举报
算法实验.docx_第1页
第1页 / 共18页
算法实验.docx_第2页
第2页 / 共18页
算法实验.docx_第3页
第3页 / 共18页
算法实验.docx_第4页
第4页 / 共18页
算法实验.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

算法实验.docx

《算法实验.docx》由会员分享,可在线阅读,更多相关《算法实验.docx(18页珍藏版)》请在冰豆网上搜索。

算法实验.docx

算法实验

实验课程:

算法分析与设计

第一部分实验内容

1.实验目标:

(1)几种排序算法在平均情况下哪一个更快。

(2)加深对时间复杂度概念的理解。

2.实验任务:

(1)实现几种排序算法(selectionsort,insertionsort,bottomupsort,quicksort,mergesort)。

对于快速分类,SPLIT中的划分元素采用三者A(low),A(high),A((low+high)/2)中其值居中者。

(2)随机产生20组数据(比如n=5000i,1≤i≤20)。

数据均属于范围(0,105)内的整数。

对于同一组数据,运行以上几种排序算法,并记录各自的运行时间(以毫秒为单位)。

(3)根据实验数据及其结果来比较这几种分类算法的平均时间和比较次数,并得出结论。

3.实验设备及环境:

PC;vs2005C/C++等编程语言。

 

4.实验主要步骤:

(1)明确实验目标和具体任务;

(2)理解实验所涉及的几个分类算法;

(3)编写程序实现上述分类算法;

(4)设计实验数据并运行程序、记录运行的结果;

(5)根据实验数据及其结果得出结论;

(6)实验后的心得体会。

第二部分问题及算法

1.问题描述:

(1)实现几种排序算法(selectionsort,insertionsort,bottomupsort,quicksort,mergesort)。

对于快速分类,SPLIT中的划分元素采用三者A(low),A(high),A((low+high)/2)中其值居中者。

(2)随机产生20组数据(比如n=5000i,1≤i≤20)。

数据均属于范围(0,105)内的整数。

对于同一组数据,运行以上几种排序算法,并记录各自的运行时间(以毫秒为单位)。

2.所求:

根据实验数据及其结果来比较这几种分类算法的平均时间和比较次数,并得出结论。

3.算法思想:

Selectionsort思想:

通过从前往后比较,交换后面元素中比当前元素最小的元素。

Insertionsort思想:

通过从后往前比较,逐个向后移动比当前元素笑得元素。

Bottomupsort思想:

通过把元素分成2的幂的子集,然后进行n/(2^j)次的合并。

Quicksort思想:

通过以基准元素为标准,结合分治的思想。

Mergesort思想:

方法跟Bottomupsort相似。

第三部分实验结果与分析

1.实验数据和结果

实验环境:

vs2005C++环境

基于随机生成数据n(5000*i)(1<=i<=20)

Selectionsort

时间、次数

Insertionsort

时间、次数

Bottomupsort

时间、次数

Quicksort

时间、次数

Mergesort

时间、次数

1

47ms

12497500

47ms

6206785

0ms

56868

0ms

111139

0ms

38528

2

203ms

49995000

203ms

24888469

16ms

123500

0ms

234023

0ms

82440

3

453ms

112492500

469ms

56535937

15ms

189973

0ms

363645

16ms

123430

4

828ms

199990000

844ms

100008516

16ms

267373

0ms

508543

15ms

176137

5

1328ms

312487500

1328ms

157364239

0ms

341699

16ms

651399

15ms

223612

6

1938ms

449985000

1906ms

224904486

16ms

410396

0ms

799837

0ms

264543

7

2656ms

612482500

2610ms

305848690

15ms

508484

0ms

972991

15ms

335453

8

3485ms

799980000

3453ms

402610714

16ms

574710

0ms

1098546

16ms

376823

9

4422ms

1012477500

4344ms

505041090

15ms

646959

16ms

1267471

16ms

421620

10

5484ms

1249975000

5406ms

625620823

16ms

733365

16ms

1427203

16ms

47871

11

6656ms

1512472500

6562ms

756629466

16ms

802657

15ms

1525995

16ms

52056

12

7906ms

1799970000

7844ms

900489818

15ms

880554

15ms

1716185

16ms

567641

13

9297ms

2112467500

9203ms

1055900844

16ms

957358

16ms

1930781

16ms

609511

14

10797ms

2449965000

10750ms

1223270396

32ms

1086710

15ms

2050705

15ms

716426

15

12500ms

2812462500

12453ms

1403128060

31ms

1158097

16ms

2231477

31ms

762446

16

14218ms

3199960000

14047ms

1603215830

31ms

1229235

16ms

2428780

32ms

806797

17

16031ms

3612457500

15891ms

1806437789

31ms

1311721

32ms

2480519

31ms

861183

18

18188ms

4049955000

17750ms

2018652619

31ms

1383465

16ms

2791490

31ms

903263

19

20343ms

4512452500

20110ms

2263480353

31ms

1463945

15ms

2823246

31ms

952812

20

22750ms

4999950000

22218ms

2500813494

31ms

1566502

32ms

3076558

46ms

1023982

2.实验分析及结论

从上面表格得出结论:

在相同的测试数据请况下,Quicksort算法的时间复杂度最低,效率最高;但Mergesort算法的比较次数最少。

第四部分心得与展望

1.自我评价及心得体会

此次试验十分繁杂,理解倒是很容易理解,但是写起算法来却和想象中的很不一样,有点想放弃,在与同学讨论和帮助下,终于找到了解决问题的方法

2.展望

用Quicksort可以很好的解决排序问题,当正确准备地掌握了这种排序法之后,觉得接下来的排序法遇到的问题都可以迎刃而解。

 

第五部分附录

1.源程序

#include

#include

#include

#definems100005

usingnamespacestd;

typedefunsignedlonglongLL;

LLcnt;//计算次数

intarry1[ms],tmp1[ms],arry[ms];

intnum;

classSort

{

public:

Sort(void);

~Sort(void);

voidInit();

voidRandominit(intn);

voidSelectionsort(intarry[],intn);

voidInsertionsort(intarry[],intn);

voidBottomupsort(intarry[],inttmp1[],intn);

voidQuicksort(intarry[],intlow,inthigh,LL&cnt);

intSplit(intarrty[],intlow,inthigh,LL&cnt);

voidMergesort(intarry[],inttmp1[],intn);

voidMerge(intarry[],inttmp1[],intlow,intmid,inthigh);

voidMergepass(intarry[],inttmp1[],intlength,intn);

}sort1;//对象

 

Sort:

:

Sort(){}

 

voidSort:

:

Init()

{

inti;

cout<<"请输入数组的元素个数:

";

cin>>num;

cout<<"请输入数组的元素:

"<

for(i=1;i<=num;i++)

cin>>arry1[i];

}

 

voidSort:

:

Randominit(intn)

{

inti;

arry1[0]=0;

srand((unsigned)time(0));

for(i=1;i<=n*5000;i++)

{

arry1[i]=rand()%(ms-4);

}

num=n*5000;

}

 

voidSort:

:

Selectionsort(intarry1[],intn)

{

cnt=0;

memcpy(arry,arry1,100005*sizeof(int));

inti,j,pos;

for(i=1;i

{

pos=i;

for(j=i+1;j<=n;j++)

{

cnt++;

if(arry[j]

pos=j;

}

if(pos!

=i)//交换位置

{

inttmp=arry[pos];

arry[pos]=arry[i];

arry[i]=tmp;

}

}

}

 

voidSort:

:

Insertionsort(intarry1[],intn)

{

cnt=0;

memcpy(arry,arry1,100005*sizeof(int));

inttmp,i,j;

for(i=2;i<=n;i++)

{

j=i;

tmp=arry[i];

while(j>1&&arry[j-1]>tmp)//当前元素与前面的元素比较

{

cnt++;

arry[j]=arry[j-1];

j--;

}

arry[j]=tmp;

}

}

 

voidSort:

:

Merge(intarry[],inttmp1[],intlow,intmid,inthigh)

{

inti=low,j=mid+1,k=low;//对三个位置指示器赋初值

while(i<=mid&&j<=high)

{

cnt++;

if(arry[i]

{

tmp1[k++]=arry[i++];

}

else

{

tmp1[k++]=arry[j++];

}

}

//复制数组的剩余元素

if(i>mid)

for(i=j;i<=high;i++)

tmp1[k++]=arry[i];

else

{

for(j=i;j<=mid;j++)

tmp1[k++]=arry[j];

}

for(i=low;i<=high;i++)

{

arry[i]=tmp1[i];

}

}

 

voidSort:

:

Bottomupsort(intarry1[],inttmp1[],intn)

{

cnt=0;

memcpy(arry,arry1,100000*sizeof(int));

memset(tmp1,0,sizeof(tmp1));

intt=1,s,i;

while(t

{

s=t;

t=2*s;//n个数分数的幂个数

i=0;

while(i+t<=n)

{

sort1.Merge(arry,tmp1,i+1,i+s,i+t);

i=i+t;

}

if((i+s)

{

sort1.Merge(arry,tmp1,i+1,i+s,n);//对剩余元素复制

}

}

}

 

intSort:

:

Split(intarry[],intlow,inthigh,LL&cnt)

{

inti,j;

i=low,j=high;

arry[0]=arry[low];

while(i

{

while((i=arry[0]))

{

cnt++;

j--;

}

if(i

{

arry[i++]=arry[j];

}

while((i

{

cnt++;

i++;

}

if(i

arry[j--]=arry[i];

}

arry[i]=arry[0];

returni;

}

voidSort:

:

Quicksort(intarry[],intlow,inthigh,LL&cnt)

{

intmid;

if(low

{

mid=sort1.Split(arry,low,high,cnt);

Quicksort(arry,low,mid-1,cnt);

Quicksort(arry,mid+1,high,cnt);

}

}

 

voidSort:

:

Mergepass(intarry[],inttmp1[],intlength,intn)

{

intl=0,j;

while(l+2*length-1<=n)

{

sort1.Merge(arry,tmp1,l,l+length-1,l+2*length-1);

l=l+2*length;

}

if(l+length-1

sort1.Merge(arry,tmp1,l,length+l-1,n);

elsefor(j=l;j<=n;j++)

tmp1[j]=arry[j];

for(l=0;l<=n;l++)

{

arry[l]=tmp1[l];

}

}

 

voidSort:

:

Mergesort(intarry1[],inttmp1[],intn)

{

cnt=0;

memcpy(arry,arry1,100005*sizeof(int));

memset(tmp1,0,sizeof(tmp1));

intlength=1;

while(length<=n)

{

Mergepass(arry1,tmp1,length,n);

length=2*length;

}

}

Sort:

:

~Sort(){}

intmain()

{

intchoice,i;

while

(1)

{

cout<<"*************菜单***************"<

cout<<"1.手动输入数据"<

cout<<"2.随机输入数据"<

cout<<"3.退出"<

cout<<"请选择一个:

";

cin>>choice;

clock_tbegin,end;

switch(choice)

{

case1:

sort1.Init();

cout<<"手动输入数据时间比较如下"<

begin=clock();

sort1.Selectionsort(arry1,num);

end=clock();

cout<<"Selectionsort的时间是:

"<<(double)end-begin<<"ms"<

cout<<"比较次数是:

"<

 

begin=clock();

sort1.Insertionsort(arry1,num);

end=clock();

cout<<"Insertionsort的时间是:

"<<(double)end-begin<<"ms"<

cout<<"比较次数是:

"<

 

begin=clock();

sort1.Bottomupsort(arry1,tmp1,num);

end=clock();

cout<<"Bottomupsort的时间是:

"<<(double)end-begin<<"ms"<

cout<<"比较次数是:

"<

 

intarry[100005];

memcpy(arry,arry1,100005*sizeof(int));

begin=clock();

sort1.Quicksort(arry,1,num,cnt);

end=clock();

cout<<"Quicksort的时间是:

"<<(double)end-begin<<"ms"<

cout<<"比较次数是:

"<

 

begin=clock();

sort1.Mergesort(arry1,tmp1,num);

end=clock();

cout<<"Mergesort的时间是:

"<<(double)(end-begin)<<"ms"<

cout<<"比较次数是:

"<

break;

 

case2:

cout<<"随机生成数据时间比较如下"<

for(i=1;i<=20;i++)

{

cout<<"第"<

sort1.Randominit(i);

begin=clock();

sort1.Selectionsort(arry1,num);

end=clock();

cout<<"Selectionsort的时间是:

"<<(double)(end-begin)<<"ms"<

cout<<"比较次数是:

"<

begin=clock();

sort1.Insertionsort(arry1,num);

end=clock();

cout<<"Insertionsort的时间是:

"<<(double)(end-begin)<<"ms"<

cout<<"比较次数是:

"<

 

begin=clock();

sort1.Bottomupsort(arry1,tmp1,num);

end=clock();

cout<<"Bottomupsort的时间是:

"<<(double)(end-begin)<<"ms"<

cout<<"比较次数是:

"<

 

intarry[100005];

memcpy(arry,arry1,100005*sizeof(int));

begin=clock();

sort1.Quicksort(arry1,1,num,cnt);

end=clock();

cout<<"Quicksort的时间是:

"<<(double)(end-begin)<<"ms"<

cout<<"比较次数是:

"<

 

begin=clock();

sort1.Mergesort(arry1,tmp1,num);

end=clock();

cout<<"Mergesort的时间是:

"<<(double)(end-begin)<<"ms"<

cout<<"比较次数是:

"<

}

break;

case3:

exit

(1);

}

}

return0;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 简洁抽象

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1