合并排序词典排序的最坏情况运行时间?

时间:2012-02-14 11:37:47

标签: algorithm complexity-theory asymptotic-complexity

使用合并排序算法将每个长度为n的n个字符串列表排序为字典顺序。最坏的情况是这个计算的运行时间是?

我把这个问题作为家庭作业。我知道在O(nlogn)时间内合并排序排序。对于长度的词典顺序是n次nlogn?还是n ^ 2?

4 个答案:

答案 0 :(得分:7)

算法的每次比较都是O(n) [比较两个字符串是O(n)最坏的情况 - 你可能会检测到哪一个只在最后一个字符上“更大”,你有O(nlogn)个比较在mergesort。

因此,您得到O(nlogn * n) = O(n^2 * logn)

答案 1 :(得分:0)

但根据递归关系

T(n)= 2T(n / 2)+ O(m * n)

会的 当m = n时,T(n)= 2T(n / 2)+ O(n ^ 2)

然后结果将是O(n ^ 2)而不是O(n ^ 2logn)。

如果我错了,请纠正我。

答案 2 :(得分:0)

**answer is O(n^2logn)
  , 
we know Merge sort has recurrence form
T(n) = a T(n/b) + O(n)
in case of merge sort 
it is 
T(n) = 2T(n/2) + O(n) when there are n elements
but here the size of the total is not "n" but "n string of length n"
so a/c to this in every recursion we are breaking the n*n elements in to half
for each recursion as specified by the merge sort algorithm
MERGE-SORT(A,P,R)  ///here A is the array P=1st index=1, R=last index in our case it 
                      is n^2 
if P<R
then Q = lower_ceiling_fun[(P+R)/2]
      MERGE-SORT(A,P,Q)
      MERGE-SORT(A,Q+1,R)
      MERGE (A,P,Q,R)
MERGE(A,P,Q,R) PROCEDURE ON AN N ELEMENT SUBARRAY TAKES TIME O(N)
BUT IN OUR CASE IT IS N*N
SO A/C to this merge sort recurrence equation for this problem becomes
T(N^2)= 2T[(N^2)/2] + O(N^2)
WE CAN PUT K=N^2 ie.. substitute to solve the recurrence
T(K)= 2T(K/2) + O(K)
a/c to master method condition T(N)=A T(N/B) + O(N^d)
                               IF A<=B^d  then T(N)= O(NlogN)
therefore T(K) = O(KlogK)
substituting K=N^2
we get T(N^2)= O(n*nlogn*n)
       ie.. O(2n*nlogn)
         .. O(n*nlogn)

因此解决了

答案 3 :(得分:0)

时间复杂度递归关系是

  

T(A,B)= 2T(A / 2,B)+ O(B ^ 2)

很明显树的高度是登录的。 因此,时间复杂度为O(n ^ 2 * logn)。