長さ n の n 個の文字列のリストは、マージ ソート アルゴリズムを使用して辞書順にソートされます。この計算の最悪の場合の実行時間は?
宿題としてこの質問を受けました。O(nlogn)時間でマージソートソートを知っています。長さの辞書式順序では、 nlogn の n 倍ですか? または n^2 ?
長さ n の n 個の文字列のリストは、マージ ソート アルゴリズムを使用して辞書順にソートされます。この計算の最悪の場合の実行時間は?
宿題としてこの質問を受けました。O(nlogn)時間でマージソートソートを知っています。長さの辞書式順序では、 nlogn の n 倍ですか? または n^2 ?
アルゴリズムの各比較はO(n)
[2 つの文字列を比較するのはO(n)
最悪のケースです。最後の文字だけでどちらが「大きい」かを検出できます]、O(nlogn)
mergesort で比較を行います。
したがって、あなたは得るO(nlogn * n) = O(n^2 * logn)
**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)
したがって解決した
しかし、再帰関係によると
T(n) = 2T(n/2) + O(m*n)
m = n の場合、T(n) = 2T(n/2) + O(n^2) になります。
その場合、結果は O(n^2logn) ではなく O(n^2) になります。
私が間違っている場合は修正してください。