if I have an array with cells 0-N that sorted, and cells N+1 until M+N, not sorted.
what will be the best time complexity to sort the array?
thanks!
Edit:
thanks !! If I want to do that in-place, it will change the complexity?
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.
First, sort just the M unsorted elements. This takes time O(M log M) using a comparison-based sort (like quicksort, merge sort, or heap sort).
Then merge the two sorted segments (of lengths N and M) together. This takes time O(M + N).
So the best time complexity, using a comparison-based sorted, is O(M + N + M log M).