| New Reply |
Algorithm Complexity: Sorted Arrays into Sorted Array |
Share Thread | Thread Tools |
| Mar8-12, 02:40 AM | #1 |
|
|
Algorithm Complexity: Sorted Arrays into Sorted Array
1. The problem statement, all variables and given/known data
We have k >= 1 sorted arrays, each one containing n >= 1 elements (all equal length). We want to combine all of them in to a single sorted array with kn elements. We have a "naive" algorithm: merge the first two arrays, then merge the third array into the result, then merge the fourth array into the result, and so on. What's the complexity of the naive algorithm, as a function of k and n? 2. The attempt at a solution Assuming k > 1. When comparing first two arrays, worst-case requires (n log n) comparisons. Adding a third array to the resulting array has a worst-case of (2n log 2n) comparisons. Similarly, adding a fourth has (3n log 3n) comparisons. This suggests a general worst-case equation for the algorithm: (k-1)n log (k-1)n I don't think this is right... the next part of the question asks us to talk about a less expensive implementation, but (k-1)n log(k-1)n is already in big theta of n log n, which is plenty efficient. |
| Mar8-12, 04:00 AM | #2 |
|
Recognitions:
|
You could try some simple cases and count the number of operations. I'm not sure if there's supposed to be a complexity factor for "comparing" versus "copying" data. As an example of a simple case, here are two sets of integers to be merged:
{1, 2, 5, 8, 9} and {3, 4, 6, 7, 10} What would the complexity be if each set only had 4 integers instead 5? What if each set only had 3 integers instead of 5? |
| Mar8-12, 11:53 AM | #3 |
|
|
Well, merging will happen k-1 times. So the merging complexity would be (k-1)n? Which is in big theta of n? (The merging part of mergesort is linear, not logarithmic, my bad).
|
| Mar8-12, 12:17 PM | #4 |
|
|
Algorithm Complexity: Sorted Arrays into Sorted Array
http://www.wolframalpha.com/input/?i...to+k+%28i*n%29
Reason: The first merge will take O(n+n) operations (go through both lists once). The second merge will take (2*n + n = 3n) operations (since you will have to go through the long list and once through the 3rd list. This results in an O(k^2*n) complexity. |
| Mar8-12, 12:23 PM | #5 |
|
Recognitions:
|
I don't know how copies and compares are factored into complexity or big theta. |
| Mar8-12, 01:06 PM | #6 |
|
|
|
| Mar8-12, 02:35 PM | #7 |
|
|
|
| Mar8-12, 04:38 PM | #8 |
|
|
Shouldn't it just be kn? That's what the pattern seems to suggest.
|
| Mar8-12, 04:54 PM | #9 |
|
|
[tex] \sum_{i=2}^k in = n\sum_{i=2}^k i = n\frac{k^2+k-2}{2} \in O(k^2*n)[/tex] |
| Mar8-12, 05:25 PM | #10 |
|
Recognitions:
|
|
| Mar8-12, 06:00 PM | #11 |
|
|
Merging two lists of size n takes at most 2*n-1 comparisons. In the first round you do this for k/2 lists. |
| Mar8-12, 09:12 PM | #12 |
|
Recognitions:
|
|
| Mar9-12, 05:06 AM | #13 |
|
|
You can also merge them in the same way merge sort does, that will give you something like O(log(k)*k*n) worst case.
|
| New Reply |
| Thread Tools | |
Similar Threads for: Algorithm Complexity: Sorted Arrays into Sorted Array
|
||||
| Thread | Forum | Replies | ||
| Help with algorithm complexity | Engineering, Comp Sci, & Technology Homework | 0 | ||
| Algorithm complexity analysis | General Math | 1 | ||
| What's wrong with my sorted linked list algorithm? (Java) | Programming & Comp Sci | 6 | ||
| find the median of a set of sorted lists, Computer Science | Engineering, Comp Sci, & Technology Homework | 1 | ||
| Merging sorted lists | Engineering, Comp Sci, & Technology Homework | 0 | ||