快速排序和合并排序的比较(时间和比较步数)

来源:互联网 发布:windows系统架构图 编辑:程序博客网 时间:2024/04/29 15:51

/*************************************************************************** Program:     Compare the time between merragesort and quitsort. History:     2013/04/21    dingdong     完成基本排序算法     2013/04/29   dingdong实现随机功能,输出时间以及布数,优化了界面。****************************************************************************/#include<stdio.h>#include<stdlib.h>#include<time.h>intmerge_count,quick_count;//两个算法中比较的次数/************mergesort*************/void merge(int *,int,int,int);void mergesort(int *,int,int);/************quicksort************/int split(int *,int,int);void quicksort(int *,int,int);/**************main***************/int main(void){   int time,*a,*b;clock_t start,finish;srand(1);//set seedprintf("**************************************************************\n");printf("*This Program will print the average time between  mergesort *\n");printf("*and quictsort and the comparative step of each other.       *\n");printf("**************************************************************\n");/*make 20 group data randomly,n=5000*i,1<=i<=20*/for(int i=1;i<21;i++){a=(int *)malloc(sizeof(int)*(5000*i+1));//for mergesort,空间多开辟了一个,因为初始值从1开始的b=(int *)malloc(sizeof(int)*(5000*i+1));//for quicksort,空间多开辟了一个,因为初始值从1开始的merge_count=0;quick_count=0;for(int j=1;j<5000*i+1;j++)//make array ramdanly{a[j]=rand()%100;b[j]=a[j];}printf("***********************************************\n");printf("The number %d group:\n",i);/*********mergesort's time***********/start=clock();mergesort(a,1,5000*i);finish=clock();time=finish-start;printf("MerageSort use %-5d ms and %-8d steps.\n",time,merge_count);/*********quicksort's time***********/start=clock();quicksort(b,1,5000*i);finish=clock();time=finish-start;printf("QuickSort  use %-5d ms and %-8d steps.\n",time,quick_count);getchar();//让控制台初与等待输入的状态free(a);//free memory        a=NULL;free(b);//free memory        b=NULL;}    return 0;}/********************mergesort***********************/void merge(int *a,int low,int mid,int high){    int *c=(int *)malloc(sizeof(int)*(high+1));//空间多开辟了一个,因为初始值从1开始的。    int s=low,q=mid,t=mid+1,k=low,r=high;    while(s<=q&&t<=r)    {        if(a[s]<=a[t])        {merge_count++;            c[k]=a[s];            s++;        }        else        {            c[k]=a[t];            t++;        }        k++;    }    if(s==q+1)    {        for(;t<=r;k++,t++)            c[k]=a[t];    }    else    {        for(;s<=q;k++,s++)            c[k]=a[s];    }    for(int i=low;i<=high;i++)        a[i]=c[i];    free(c);    c=NULL;}void mergesort(int *a,int low,int high){    int mid;    if(low<high)    {        mid=(high+low)/2;        mergesort(a,low,mid);        mergesort(a,mid+1,high);        merge(a,low,mid,high);    }}/*********************quicksort************************/int split(int *a,int low,int high){int i=low;int x=a[low],temp;//temp for exchangefor(int j=low+1;j<=high;j++){if(a[j]<=x){quick_count++;i++;if(i!=j){temp=a[i];a[i]=a[j];a[j]=temp;}}}temp=a[low];a[low]=a[i];a[i]=temp;return i;}void quicksort(int *a,int low,int high){int mid;if(low<high){mid=split(a,low,high);quicksort(a,low,mid-1);quicksort(a,mid+1,high);}}
随机产生20组数据(比如n=5000i1i20)。数据均属于范围(0105)内的整数。对于同一组数据,运行快速排序和归并排序算法,并记录各自的运行时间(以毫秒为单位),以及记录它们各自的比较步数。(虽然快排的比较步数比较多,但是速度比合并排序快很多,特别是数据量越大的时候,这个越能体现)


原创粉丝点击