简体   繁体   English

QuickSort比std :: sort慢

[英]QuickSort is slower than std::sort

I have a quick_sort code (С++) that looks like this 我有一个类似于此的quick_sort代码(С++)

template< typename BidirectionalIterator, typename Compare >
BidirectionalIterator quick_sort_partition( BidirectionalIterator left, BidirectionalIterator right, Compare cmp ) {
    BidirectionalIterator q = left - 1;
    std::mt19937 gen(time(0)); 
    std::uniform_int_distribution<int> uid(0, right - left - 1);
    int pivot_1 = uid(gen) ;
    BidirectionalIterator randomNum = pivot_1 + left;
    std::iter_swap( randomNum, right );
    bool index = 0;
    for (BidirectionalIterator i = left; i < right; i++){
        if (*i < *right){
            ++q;
            std::iter_swap( q, i );
        }
        if (*i == *right){   
            index = 1 - index;
            if(index){
                ++q;
                std::iter_swap( q, i );
            }
        }
    }

    ++q;
    std::iter_swap( q, right );


    return q;

}
template< typename BidirectionalIterator, typename Compare >
void quick_sort( BidirectionalIterator first, BidirectionalIterator last, Compare cmp ) {
    if (first < last){
        BidirectionalIterator q = quick_sort_partition(first, last, cmp);
        quick_sort(first, q - 1, cmp);
        quick_sort(q + 1, last, cmp);
    }
}

but he is slower(more 6 times) than std::sort on big tests. 但他在大考试中比std :: sort慢(多6次)。

Any ideas why? 有什么想法吗?

How to optimize my code for good job? 如何优化我的代码以获得好工作?

Your QuickSort implementation is pretty vanilla. 您的QuickSort实现非常香草。 It does use random pivot selection, which ensures that there are no "killer" inputs that cause performance degredation, so that's better than the absolute basic QuickSort. 它确实使用随机数据透视选择,这确保没有“杀手”输入导致性能降级,因此这比绝对基本的QuickSort更好。

There are a number of optimizations that might be used, among them: 可以使用许多优化,其中包括:

  • It is typical for "Quick Sort" to in fact be implemented as a hybrid sort that falls back to (say) Insertion Sort for partitions smaller than some fixed threshold. 实际上,“快速排序”通常被实现为混合排序,其可以回退到(例如)插入排序以用于小于某个固定阈值的分区。 Once you get to small partitions, the overhead of Quick Sort tends to overcome its asymptotic complexity advantages. 一旦进入小分区,Quick Sort的开销往往会克服其渐近的复杂性优势。

  • Maximum recursion depth can be minimized and function call overhead can be reduced by switching to a hybrid recursive / iterative approach, wherein upon each partitioning, the smaller sub-array is sorted recursively, but the code just loops to sort the larger one. 通过切换到混合递归/迭代方法,可以最小化最大递归深度并且可以减少函数调用开销,其中在每次分区时,递归地对较小的子数组进行排序,但是代码只是循环以对较大的子数组进行排序。

  • When partitioning, you can reduce the number of swaps performed by finding pairs of elements for which a swap puts both in the correct sub-partition, and swapping those, instead of alternating between swapping into one sub-partition and swapping into the other. 在分区时,您可以通过查找交换将两者放入正确的子分区中的元素对,并交换这些元素来减少交换次数,而不是在交换到一个子分区和交换到另一个子分区之间交替。

  • It would probably help to come up with a way to reuse the same random number source throughout the sort, instead of instantiating a new one upon every partitioning. 它可能有助于提出一种在整个排序中重用相同随机数源的方法,而不是在每次分区时实例化一个新的。

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

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