排序比较

写了几个排序算法的比较,然后发现手写的快排比std的sort要快?这尼玛肯定是不合理的,但是我至今不知道神马原因。。

#include<iostream>
#include<ctime>
#include<cmath>
#include<algorithm>
using namespace std;

#define Cutoff 10
const int MAXN = 10408;
int o[MAXN];
int a[MAXN];

clock_t start, endt;

//初始化
void init()
{
    //srand(unsigned(time(NULL)));
    for (int i = 0; i < MAXN; i++)
    {
        o[i] = a[i] = rand();
    }
    cout << "init end\n";
}

//重置a数组
void Reset()
{
    for (int i = 0; i < MAXN; i++)
    {
        a[i] = o[i];
    }
}

//插入排序  O(n^2) 
void InsertionSort(int A[], int N, bool Print = true)
{
    if (Print)
    {
        Reset();
        start = clock();
    }

    int temp,j;
    for (int i = 1; i < N; i++)
    {
        temp = A[i];
        for (j = i; j > 0 && A[j - 1] > temp; j--)
            A[j] = A[j - 1];
        A[j] = temp;
    }

    if (Print)
    {
        endt = clock();
        cout << "InsertionSort: " << (double)(endt - start) / CLOCKS_PER_SEC << endl;
    }

}

//希尔排序
void ShellSort(int A[], int N)
{
    Reset();
    start = clock();
    for (int div = N / 2; div >= 1; div /= 2)
        for (int i = div; i < N; i++)
            for (int j = i; j >= div && (A[j] < A[j - div]); j -= div)
                swap(A[j], A[j - div]);
    endt = clock();
    cout << "ShellSort: " << (double)(endt - start) / CLOCKS_PER_SEC << endl;
}

//归并排序
void Merge(int A[], int tmpArray[], int Lpos, int Rpos, int RightEnd)
{
    int i, LeftEnd, NumElements, TmpPos;
    LeftEnd = Rpos - 1;
    TmpPos = Lpos;
    NumElements = RightEnd - Lpos + 1;

    while (Lpos <= LeftEnd && Rpos <= RightEnd)
        if (A[Lpos] <= A[Rpos])
            tmpArray[TmpPos++] = A[Lpos++];
        else
            tmpArray[TmpPos++] = A[Rpos++];

    while (Lpos <= LeftEnd)
        tmpArray[TmpPos++] = A[Lpos++];
    while (Rpos <= RightEnd)
        tmpArray[TmpPos++] = A[Rpos++];

    for (int i = 0; i < NumElements; i++, RightEnd--)
        A[RightEnd] = tmpArray[RightEnd];
}

void MSort(int A[], int tmpArray[], int left, int right)
{
    int center;
    if (left < right)
    {
        center = (left + right) / 2;
        MSort(A, tmpArray, left, center);
        MSort(A, tmpArray, center + 1, right);
        Merge(A, tmpArray, left, center + 1, right);
    }
}

void MergeSort(int A[], int N)
{
    int *tmpArray;
    tmpArray = (int*) malloc(N*sizeof(int));
    if (tmpArray != NULL)
    {
        Reset();
        start = clock();
        MSort(A, tmpArray, 0, N - 1);
        endt = clock();
        cout << "MergeSort: " << (double)(endt - start) / CLOCKS_PER_SEC << endl;
        free(tmpArray);
    }
    else
        perror("Allocate dynamic memory");
}

//快速排序
int Median3(int A[], int left, int right)
{
    int center = (left + right) / 2;

    if (A[left] > A[center])
        swap(A[left], A[center]);
    if (A[left] > A[right])
        swap(A[left], A[right]);
    if (A[center] > A[right])
        swap(A[center], A[right]);

    swap(A[center], A[right - 1]);
    return A[right - 1];
}

void Qsort(int A[], int left, int right)
{
    int i, j;
    int Pivot;

    if (left + Cutoff <= right)
    {
        Pivot = Median3(A, left, right);
        i = left, j = right - 1;
        for (;;)
        {
            while (A[++i] < Pivot) {}
            while (A[--j] > Pivot) {}
            if (i < j)
                swap(A[i], A[j]);
            else
                break;
        }
        swap(A[i], A[right - 1]);
        Qsort(A, left, i - 1);
        Qsort(A, i + 1, right);
    }
    else
        InsertionSort(A + left, right - left + 1,false);
}

void QuickSort(int A[], int N)
{
    Reset();
    start = clock();
    Qsort(A, 0, N - 1);
    endt = clock();
    cout << "QuickSort: " << (double)(endt - start) / CLOCKS_PER_SEC << endl;
}

int main()
{
    init();

    InsertionSort(a,MAXN);
    ShellSort(a,MAXN);
    MergeSort(a, MAXN);
    QuickSort(a, MAXN);

    Reset();
    //std快排
    start = clock();
    sort(a, a + MAXN);
    endt = clock();
    cout <<"Std::sort: "<< (double)(endt - start) / CLOCKS_PER_SEC << endl;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值