I am working on a problem that has a sorted array of n elements followed by an unsorted array of length
- O(logn)
- O(sqrt(n))
How to sort the entire list most efficiently? Which sorting should I use in the above two cases?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since inserting a single element into array and keeping it sorted is
O(n), you cannot get better then that.Thus, for both cases – sorting the smaller array and then using
merge(part1,part2)will beO(n), and thus optimal in terms of asymptotic complexity.O(logn*loglog(n)), orO(sqrt(n)*log(sqrt(n))respectively of the cases.merge(part1,part2):O(n+logn)orO(n+sqrt(n)), which isO(n)1 anyway.So, the total complexity of both cases is
O(n), which is optimal for this problem.(1) It is true because,
log(n)^kis asymptotically smaller thenn^mfor eachk>0,m>0, and specifically fork=1, m=1/2.Proof is based on taking logs on both sides:
The last is obviously true (for large
nand constantk,m>0), and thus the claim is true.From this we can conclude that
sqrt(n)*log(n) < sqrt(n) * n^1/2 = n, and thus it is indeedO(n).