Since inserting one element into an array and preserving its sorting is O(n) , you cannot improve it.
Thus, for both cases, sorting a smaller array, and then using merge(part1,part2) will be O(n) and, therefore, optimal in terms of asymptotic complexity.
- sorting a smaller array:
O(logn*loglog(n)) or O(sqrt(n)*log(sqrt(n)) respectively. merge(part1,part2) : O(n+logn) or O(n+sqrt(n)) , which is equal to O(n) 1 .
Thus, the overall complexity of both cases is O(n) , which is optimal for this problem.
(1) This is true because log(n)^k asymptotically less than n^m for each k>0,m>0 and, in particular, for k=1, m=1/2 .
The proof is based on the use of magazines on both sides:
log (log(n)^k) <? log(n^m) <=> k*log(log(n)) <? m*log(n)
The latter is obviously true (for large n and constants k,m>0 ), and, therefore, the statement is true. From this we can conclude that sqrt(n)*log(n) < sqrt(n) * n^1/2 = n , and therefore it is really O(n) .
source share