简体   繁体   English

命名排序算法。 是QuickSort吗?

[英]Naming the sorting algorithm. Is it QuickSort?

For this sorting algorithm I wrote, I have two questions: 对于我编写的这种排序算法,我有两个问题:

1.When I range fill a vector [max-num, 0] (worst-case scenario) I get a result way better than O(n^2) . 1.当我对向量[max-num, 0]填充时(最坏的情况),我得到的结果要好于O(n^2) (I'm not even sure if I even wrote a quicksort anymore). (我什至不能确定我是否再写过Quicksort)。

2.When I mix up the range, ex: I fill the unsorted vector [0, max-num/2] then [max-num/2, 0] , weirdly enough it runs with crashing up to numbers 900,000 but crashes right after. 2.当我混合范围时,例如:我先填充未排序的向量[0, max-num/2]然后再填充[max-num/2, 0] ,这很奇怪,它崩溃到数字900,000,但随后崩溃。

template<class writeIter>
void quicksort(writeIter begin, writeIter end)
{
if (begin!= end) {
    int diff = end-begin;
    if (diff > 2) {

        writeIter pivot = ((end-begin) / 2) + begin;
        writeIter itFirst = begin;
        writeIter itSecnd = end-1;
        auto pivotVal = *pivot;

        swap(*pivot, *(end-1));
        while (itFirst < itSecnd) {
            if (*itFirst > pivotVal) {
                while (*itSecnd > pivotVal && itSecnd > itFirst) --itSecnd;
                if (itSecnd > itFirst)
                    swap(*itFirst, *itSecnd);
            }
            ++itFirst;
        }
        swap(*itSecnd, *(end-1));

        quicksort(begin, itSecnd);
        quicksort(itSecnd, end);
    }
    else if (diff  == 2)
        if (*begin > *(begin+1))
            swap(*begin, *(begin+1));
 }
}

Yes, it's the naive quick sort. 是的,这是天真的快速排序。 But you choose the middle element instead of the last element as your pivot. 但是,您选择中间元素而不是最后一个元素作为枢轴。

  1. When you fill a vector [max-num, 0], it's actually not the worst-case scenario at all. 当您填充向量[max-num,0]时,实际上根本不是最坏的情况 Because each time you choose the middle element as the pivot, you divide the vector into two parts with almost the same size, so the time complexity is O(nlogn). 由于每次选择中间元素作为枢轴时,都将向量分为几乎相同大小的两部分,因此时间复杂度为O(nlogn)。
  2. However, when you fill the unsorted vector [0, max-num/2] then [max-num/2, 0], it's the worst-case for you algorithm as you divide the vector into two parts with one extremely long and one extremely short. 但是,当您填充未排序的矢量[0,max-num / 2]然后再填充[max-num / 2,0]时,这对于您的算法来说是最坏的情况,因为您将矢量分为两部分,其中一个非常长而另一个非常短。 So the time complexity is O(n^2). 因此,时间复杂度为O(n ^ 2)。

To gain a better performance on almost all vectors, you can: 要在几乎所有矢量上获得更好的性能,您可以:

  • pick a random element as your pivot 选择一个随机元素作为枢轴
  • pick three random elements and choose the second largest one 选择三个随机元素,然后选择第二大元素
  • when the size of vector is small enough, eg, smaller than 10, apply insert sort on it 当向量的大小足够小(例如小于10)时,对其应用插入排序
  • to deal with the situation that all elements are close to each other, you can do some extra work before recursively sorting subvectors to skip the elements equal to the pivot 为了处理所有元素彼此靠近的情况,可以在递归排序子向量以跳过等于枢轴的元素之前做一些额外的工作

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

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