I have 2 arrays which are not sorted. Would it be faster to sort them individually and then merge them? Or would it be faster to just concatenate the arrays first and sort the combined huge array?
Assuming that concatenation is done in O(1)
, merging takes O(n)
and sorting O(n log n)
, you have the choice between:
O(n log n) + O(n) = O(n log n)
O(1) + O((2n) log (2n)) = O(n log n)
therefore, asymptotically both options are equivalent.
Of course, the whole discussion is moot anyway if you use MergeSort.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With