简体   繁体   English

为什么快速排序的这种实现比qsort慢?

[英]Why is this implementation of Quick Sort slower than qsort?

Doing some refreshing on algorithms. 对算法进行一些刷新。 I wrote this Quick sort implementation: 我写了这个快速排序实现:

#define SWAP(X, Y, Type) { Type Temp = (X); (X) = (Y); (Y) = Temp; }

int QSPartition(int* Array, int StartIndex, int EndIndex)
{
    int PivotDestinationIndex = StartIndex;
    int Pivot = Array[EndIndex];
    for(int i = StartIndex; i <= EndIndex; i++)
    {
        if (Array[i] < Pivot)
        {
            SWAP(Array[i], Array[PivotDestinationIndex], int);
            PivotDestinationIndex++;
        }
    }
    SWAP(Array[PivotDestinationIndex], Array[EndIndex], int);
    return PivotDestinationIndex;
}

void QuickSort(int* Array, int StartIndex, int EndIndex)
{
    if (StartIndex >= EndIndex)
        return;

    int PivotIndex = QSPartition(Array, StartIndex, EndIndex);
    QuickSort(Array, PivotIndex + 1, EndIndex);
    QuickSort(Array, StartIndex, PivotIndex - 1);
}

In my main program: 在我的主程序中:

#define Measure(Iterations, What, Code)\
{\
    clock_t Begin = clock();\
    for(int _im_ = 0; _im_ < Iterations; _im_++)\
    {\
        Code;\
    }\
    clock_t End = clock();\
    printf("Time took to " What ": %.1f ms\n", (double)(End - Begin));\
}

int CompareInt(const void* x, const void* y) { return *(int*)x - *(int*)y; }

int main()
{
    srand(time(0));

    int Count = 100000;
    int* Array0 = (int*)malloc(sizeof(int) * Count);
    int* Array1 = (int*)malloc(sizeof(int) * Count);

    for (int i = 0; i < Count; i++)
    {
        int RandomValue = rand() % 100;
        Array0[i] = RandomValue;
        Array1[i] = RandomValue;
    }

    Measure(1, "My Quick", QuickSort(Array0, 0, Count - 1));
    Measure(1, "C  Quick", qsort(Array1, Count, sizeof(int), CompareInt));

    getchar();
    return 0;
}

The output is that "My Quick" is always around 125 ms while "C Quick" is always around 25 ms . 输出是“ My Quick”始终约为125 ms而“ C Quick”始终约为25 ms (32-bit DEBUG build - On RELEASE still the same 5 times slower) (32位DEBUG构建-在RELEASE上仍然慢5倍)

What is going on? 到底是怎么回事? Why is my implementation slower? 为什么我的执行速度较慢? (I tried inlining the functions but it didn't do much) Is there something else wrong? (我尝试内联函数,但并没有做太多事情)还有其他问题吗? (the way I'm timing this, or the way I'm populating the arrays with random values, or...?) (我计时的方式,或用随机值填充数组的方式,或...?)

Try one of these, you'll need to change the std::swap to swap for C. 尝试其中之一,您需要更改std :: swap才能交换为C。

QuickSort using middle value for pivot: QuickSort使用中间值作为枢轴:

void QuickSort(uint32_t a[], int lo, int hi) {
    int i = lo, j = hi;
    uint32_t pivot = a[(lo + hi) / 2];
    while (i <= j) {            // partition
        while (a[i] < pivot)
            i++;
        while (a[j] > pivot)
            j--;
        if (i <= j) {
            std::swap(a[i], a[j]);
            i++;
            j--;
        }
    }
    if (lo < j)                 // recurse
        QuickSort(a, lo, j);
    if (i < hi)
        QuickSort(a, i, hi);
}

QuickSort using median of low, middle, high, values as pivot QuickSort使用低,中,高值的中位数作为枢轴

void QuickSort(uint32_t a[], int lo, int hi) {
    int i = lo, j = (lo + hi)/2, k = hi;
    uint32_t pivot;
    if (a[k] < a[i])            // median of 3
        std::swap(a[k], a[i]);
    if (a[j] < a[i])
        std::swap(a[j], a[i]);
    if (a[k] < a[j])
        std::swap(a[k], a[j]);
    pivot = a[j];
    while (i <= k) {            // partition
        while (a[i] < pivot)
            i++;
        while (a[k] > pivot)
            k--;
        if (i <= k) {
            std::swap(a[i], a[k]);
            i++;
            k--;
        }
    }
    if (lo < k)                 // recurse
        QuickSort(a, lo, k);
    if (i < hi)
        QuickSort(a, i, hi);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM