排序算法速度比较

为了应付考试,重新温习一下各种排序算法hhhhh
各种排序方法的综合比较
希尔排序和基排就暂时忽略了。。。。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <ctime>
using namespace std;
const int MAXN=10000;

clock_t start,stop;
double duration;

void GetArray(int ary[]) {
    srand((int)time(0));
    for (long long i=0;i<MAXN;i++){
        ary[i]=rand()%MAXN;
    }
}

void QuickSort(int a[],long long left,long long right){
    long long lower=left+1;
    long long upper=right;
    swap(a[left],a[(left+right)/2]);
    int temp=a[left];
    while (lower<=upper){
        while (a[lower]<temp){
            lower++;
        }
        while (a[upper]>temp){
            upper--;
        }
        if(lower<upper){
            swap(a[lower++],a[upper--]);
        }else{
            lower++;
        }
    }
    swap(a[upper],a[left]);
    if(left<upper-1){
        QuickSort(a,left,upper-1);
    }
    if(upper+1<left){
        QuickSort(a,upper+1,right);
    }
    return ;
}

void Merge(int sourceArr[],int tempArr[],int startIndex,int midIndex,int endIndex){
    int i=startIndex;
    int j=midIndex+1;
    int k=startIndex;
    while(i!=midIndex+1 && j!=endIndex+1){
        if(sourceArr[i]>=sourceArr[j]){
            tempArr[k++]=sourceArr[j++];
        }else{
            tempArr[k++]=sourceArr[i++];
        }
    }
    while(i != midIndex+1){
        tempArr[k++]=sourceArr[i++];
    }
    while(j != endIndex+1){
        tempArr[k++]=sourceArr[j++];
    }
    for(i=startIndex;i<=endIndex;i++){
        sourceArr[i]=tempArr[i];
    }
}
void MergeSort(int sourceArr[],int tempArr[],int startIndex,int endIndex)
{
    int midIndex;
    if(startIndex < endIndex){
        midIndex=(startIndex + endIndex) / 2;
        MergeSort(sourceArr,tempArr,startIndex,midIndex);
        MergeSort(sourceArr,tempArr,midIndex+1,endIndex);
        Merge(sourceArr,tempArr,startIndex,midIndex,endIndex);
    }
    return ;
}

void HeapAdjust(int array[],int i,int nLength){
    int nChild;
    int nTemp;
    for(;2*i+1<nLength;i=nChild){
        nChild=2*i+1;
        if(nChild<nLength-1 && array[nChild+1]>array[nChild]){
            ++nChild;
        }
        if(array[i]<array[nChild]){
            nTemp = array[i];
            array[i] = array[nChild];
            array[nChild] = nTemp;
        }else{
            break;
        }
    }
}
void HeapSort(int array[],int length){
    for(int i=length/2-1;i>=0;--i){
         HeapAdjust(array,i,length);
    }
    for(int i=length-1;i>0;--i){
        array[i]=array[0]^array[i];
        array[0]=array[0]^array[i];
        array[i]=array[0]^array[i];
        HeapAdjust(array,0,i);
    }
    return ;
}

void BubbleSort(int a[],long long len){
    for (long long i=0;i<len-1;i++){
        for (long long j=0;j<len-1-i;j++){
            if(a[j]>a[j+1]){
                swap(a[j+1],a[j]);
            }
        }
    }
    return ;
}

void SelectSort(int a[],long long len){
    for (int i=0;i<len;i++){
        int index=i;
        int j;
        for (j=i+1;j<len;j++){
            if(a[j]<a[index]){
                index=j;
            }
        }
        swap(a[index],a[i]);
    }
    return ;
}

void InsertSort(int a[],long long n){
    int i,j;
    int temp;
    for(i=0;i<n;i++){
        temp=a[i];
        for(j=i;j>0&&a[j-1]>temp;j--){
            a[j]=a[j-1];
        }
        a[j]=temp;
    }
    return ;
}


int main (){
    int ary[MAXN];
    int s[MAXN];
    GetArray(ary);

    start=clock();
    for (int i=0;i<MAXN;i++){
        QuickSort(ary,0,MAXN-1);
    }
    stop=clock();
    duration=((double)(stop-start))/CLK_TCK;
    printf ("The consumption of MAXN-QuickSort time: %lf\n",duration);

    start=clock();
    for (int i=0;i<MAXN/10;i++){
        MergeSort(ary,s,0,MAXN-1);
    }
    stop=clock();
    duration=((double)(stop-start))/CLK_TCK;
    printf ("The consumption of MAXN/10-MergeSort time: %lf\n",duration);

    start=clock();
    for (int i=0;i<MAXN/10;i++){
        HeapSort(ary,MAXN);
    }
    stop=clock();
    duration=((double)(stop-start))/CLK_TCK;
    printf ("The consumption of MAXN/10-HeapSort time: %lf\n",duration);

    start=clock();
    BubbleSort(ary,MAXN);
    stop=clock();
    duration=((double)(stop-start))/CLK_TCK;
    printf ("The consumption of BubbleSort time: %lf\n",duration);

    start=clock();
    SelectSort(ary,MAXN);
    stop=clock();
    duration=((double)(stop-start))/CLK_TCK;
    printf ("The consumption of SelectSort time: %lf\n",duration);

    start=clock();
    InsertSort(ary,MAXN);
    stop=clock();
    duration=((double)(stop-start))/CLK_TCK;
    printf ("The consumption of MAXN-InsertSort time: %lf\n",duration);

    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值