简体   繁体   English

如何分析这个特定版本的quicksort?

[英]How to analyse this particular version of quicksort?

I have this pseudocode and I want to analysis time complexity of this algorithm But I have no idea about it 我有这个伪代码,我想分析这个算法的时间复杂度,但我对此一无所知

Proc Sort(A,l,r)
 if(r-l+1<4)
    then Quicksort(A,l,r)
    else
     Sort(A,l,r-3)
     Sort(A,l+3,r)

So I know that if the element of an array is less than 4 we pass it through the quicksort else we reduce the size of the array by three and then pass the left and right part So we will do this untile we get exactly the array of size n<4 the problem is I can't get to the recurrence and I am not sure if this algorithm works better in the worst case analysis 所以我知道,如果数组的元素小于4,我们将其传递给quicksort,否则我们将数组的大小减小3,然后传递左右部分,因此我们将执行此操作,直到我们得到的数组正好大小n <4,问题是我无法重现,而且我不确定该算法在最坏情况下的分析效果是否更好

Thank you for your help 谢谢您的帮助

Well, whether or not this sorting function actually works , the way to figure out the run time is pretty easy here: 好吧,不管这个排序功能是否真正起作用 ,在这里计算运行时间的方法都非常简单:

You write down a mathematical expression for the run time as a function of array size: 您可以将运行时的数学表达式记为数组大小的函数:

T(N) = ??? T(N)= ???

Well, if N <= 4, then we call Quicksort. 好吧,如果N <= 4,那么我们称为Quicksort。 Now, we don't have the function definition available, but regardless of that, since it will only ever called with input of at most size 4, we can treat its run time as a constant and just call it C. 现在,我们没有可用的函数定义,但是无论如何,由于它只能用最大为4的输入来调用,因此我们可以将其运行时视为一个常量,而仅将其称为C。

And if N >= 4, then we call Sort again, with arrays that are 3 smaller. 如果N> = 4,则我们再次调用Sort,数组要小3。

So: 所以:

T(N) for N >= 4 is 2 * T(N-3). N> = 4的T(N)为2 * T(N-3)。

Now at this point that should give you all the information you need for runtime analysis. 现在,这将为您提供运行时分析所需的所有信息。 Why don't you try it from here and get back to us when you get stuck? 您为什么不从这里尝试一下,却遇到麻烦就找我们?

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

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