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

发布于 2025-01-05 19:26:39 字数 133 浏览 1 评论 0原文

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

我把这个问题作为作业。我知道合并排序在 O(nlogn) 时间内排序。对于长度的字典顺序,它是 n 乘以 nlogn 吗?或 n^2 ?

A list of n strings each of length n is sorted into lexicographic order using the merge sort algorithm. The worst case running time of this computation is?

I got this question as a homework. I know merge sort sorts in O(nlogn) time. For lexicographic order for length in is it n times nlogn ? or n^2 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南七夏 2025-01-12 19:26:39

该算法的每次比较都是 O(n) [比较两个字符串是 O(n) 最坏的情况 - 您可能仅在最后一个字符上检测到哪个“更大”] ,您在合并排序中进行了 O(nlogn) 比较。

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

Each comparison of the algorithm is O(n) [comparing two strings is O(n) worst case - you might detect which is "bigger" only on the last character], You have O(nlogn) comparisons in mergesort.

Thus you get O(nlogn * n) = O(n^2 * logn)

牛↙奶布丁 2025-01-12 19:26:39

但根据递推关系

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


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

那么结果将是 O(n^2) 而不是 O(n^2logn)。

如果我错了请纠正我。

But according to the recurrence relation

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

Will be
T(n) = 2T(n/2) + O(n^2) when m = n

Then the result will be O(n^2) and not O(n^2logn).

Correct me if I'm wrong.

趴在窗边数星星i 2025-01-12 19:26:39
**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)

因此解决了

**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)

hence solved

牵你手 2025-01-12 19:26:39

时间复杂度递推关系为

T(a,b)=2T(a/2,b)+O(b^2)

显然,树的高度将是 logn 。
因此时间复杂度为O(n^2*logn)。

Time complexity recurrence relation is

T(a,b)=2T(a/2,b)+O(b^2)

So clearly the height of tree would be logn.
Thus time complexity is O(n^2*logn).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文