(算法设计与分析)归并分类与快速分类平均时间之比较 (验证型实验)

本文通过实验对比了归并排序和快速排序算法在平均情况下的效率。使用C/C++实现两种算法,对20组随机生成的整数数据(范围0-105,5000i项,i从1到20)进行排序,并记录运行时间。结果显示,随着数据量增加,快速排序的平均时间少于归并排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验目标:

(1) 通过实验比较归并分类与快速分类算法在平均情况下哪一个更快。

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

实验任务:

(1)用C/C++语言编程实现归并分类算法6.3 和快速分类算法6.6。对于快速分类,SPLIT中的划分元素采用三者A(low),A(high),A((low+high)/2)中其值居中者。

(2)随机产生20组数据(比如n=5000i,1≤i≤20)。数据均属于范围(0,105)内的整数。对于同一组数据,运行快速分类和归并分类算法,并记录各自的运行时间(以毫秒为单位)。

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

实验设备及环境:

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

实验主要步骤:

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

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

(3)    编写程序实现两个分类算法;

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

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

 


实验数据及运行结果、实验结果分析及结论:

A.关键代码

1.      快速排序

int Split(int a[],int low,int high)

{// SPLIT中的划分元素采用三者A(low),A(high),A((low+high)/2)中其值居中者,排序//结果a[low]左侧都比a[low]少,a[high]右侧都比a[low]大

int i=low,temp;

//找出A(low),A(high),A((low+high)/2)中其值居中者,并将其值居中者与A(low)交换

if(a[low]< a[(low+high)/2])

{

if(a[low]<a[high])

{

if(a[(low+high)/2]<a[high])

{//其值居中者为a[(low+high)/2]

temp= a[(low+high)/2];

a[(low+high)/2]=a[low];

a[low]=temp;

}

else

{//其值居中者为a[high]

temp=a[high];

a[high]=a[low];

a[low]=temp;

}

}

else

{

if(a[(low+high)/2]<a[high])

{

if(a[low]>a[high])

{//其值居中者为a[high]

temp=a[high];

a[high]=a[low];

a[low]=temp;

}

}

else

{//其值居中者为a[(low+high)/2]

temp= a[(low+high)/2];

a[(low+high)/2]=a[low];

a[low]=temp;

}

}

for(int j=low+1;j<=high;j++)

        if(a[j]<a[low])

        {

               i++;

               if(i!=j)

               {

                      temp=a[i];

                      a[i]=a[j];

                      a[j]=temp;

               }

        }

        temp=a[i];

        a[i]=a[low];

        a[low]=temp;

        return i;}

//快速排序

void QuickSort(int a[],int low,int high)

{

if(low<high)

{

        intw=Split(a,low,high);

        QuickSort(a,low,w-1);

        QuickSort(a,w+1,high);

}

}

2.归并分类

void merge(int a[],int first,int mid,int end,intc[])//a[first]~a[mid]和a[mid+1]~a[end]已经排序

{

int pbotton=first;

int f=first,m=mid;

while(f<mid&&m<=end)

        if(a[f]<a[m])

               c[pbotton++]=a[f++];

        else

               c[pbotton++]=a[m++];

if(f==mid)

        while(m<=end)

               c[pbotton++]=a[m++];

else

        while(f<mid)

              c[pbotton++]=a[f++];

for(inti=first;i<=end;i++)//重新赋值

        a[i]=c[i];

}

void mergeSort(int a[],int low,int high,intc[])

{

  if(low<high)

  {

         mergeSort(a,low,(low+high)/2,c);

         mergeSort(a,(low+high)/2+1,high,c);

         merge(a,low,(low+high)/2+1,high,c);

  }

}

3.主函数

int main()

{

int random=5000,j=1;//随机数量

int*merge_array,*quick_array,*c;//归并分类数组和快速分类数组

clock_tstart,end,start1,end1;//记录时间

        srand((unsignedint(time(NULL))));//导入时间种子加每次运行结样试下伪随机数种子

        cout<<"随机数数量\t归并排序时间\t快速排序算法时间"<<endl;

        while(j<=20)

        {

        merge_array=new int[random*j];

        quick_array=new int[random*j];

        c=new int [random*j];

        for(inti=0;i<random*j;i++)

        {

               merge_array[i]=rand();

               quick_array[i]=merge_array[i];

        }

        //归并分类算法时间统计

        start=clock();

        mergeSort(merge_array,0,random*j-1,c);

        end=clock();

        delete []merge_array;//释放空间

        //快速分类算法时间统计

        start1=clock();

        QuickSort(quick_array,0,random*j-1);

        end1=clock();

        delete []quick_array;//释放空间

        delete []c;//释放空间

        cout<<random*j<<"\t\t"<<static_cast<double>(end-start)/CLOCKS_PER_SEC*1000<<"ms\t\t";

        cout<<static_cast<double>(end1-start1)/CLOCKS_PER_SEC*1000<<"ms"<<endl;

        j++;

        }

        return 0;

}

B.实验过程和实验结果分析


由于手动输入的数据量较少,固程序没有手动输入这个选项,全部数据由随机数产生。随机产生20组数据(random=5000i,1≤i≤20)。数据均属于范围(0,105)内的整数。对于同一组数据,运行快速分类和归并分类算法。可以看出,当随机数数量较少的时候,归并排序和快速排序的时间差别不大,当数量逐渐增多时候,排序开始有差别,且快速排序时间少于归并排序时间。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值