Question

In: Computer Science

You are given 2 sorted sequences of log(n) and n-1 keys. We would like to merge...

You are given 2 sorted sequences of log(n) and n-1 keys. We would like to merge those 2 sorted sequences by performing o(n) comparisons.[Note that we are interested in the comparisons and not the running time.]

Show how this can be done or argue how this cannot be done.

In class we show that ordinary merging would require no more than lg(n)+n-1+1 = n+lg(n) comparisons.

Solutions

Expert Solution

If you have two sorted sequences of log(n) and (n-1) keys, merging those two sequences canbe performed with O(n) comparisons. Let's take an example to understand it.

Let's say the value of n = 100. So, log(n) with base 10 = log(100) = 10.
Also, n-1 = 100-1 = 99.

Now, let's say the two sequences are -

seq 1 = [ 100, 101, 102, 103, 104, 105, 106, 107, 108, 109 ] . Size is log(100) = 10.

seq 2 = [1, 2, 3, .... , 99]. Size is n-1 = 99.

We can see that, the number of comparisons that would require will be 99 in this case. This is the worst case scenario and for larger value of N, O(N-1) ~ O(N).

In the above example, in each iteration, 100 is compared with all the elements in seq 2 and since 100 is less than all the elements in seq 2, so everytime the elements of the seq 2 is being inserted in the final sorted sequence. After all the values 1 ... 99 are inserted, the seq 1 elements are then inserted that would require no more comparisons as by now the seq 2 is empty.


Related Solutions

(Subject: Data Structures and Algorithms) Faster merging. You are given two sorted sequences of $\lg{n}$ and...
(Subject: Data Structures and Algorithms) Faster merging. You are given two sorted sequences of $\lg{n}$ and $n-1$ keys. We would like to merge those two sorted sequences by performing $o(n)$ comparisons. (Note we are interested in comparisons, not running time.) Show how this can be done or argue that it cannot be done. Note: In class we showed that ordinary merging would require no more than $\lg{n}+n-1+1= n + \lg{n}$ comparisons.
Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n]...
Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n] and B[1 . . . n] and an integer k. Describe an algorithm to find the kth smallest element in the union of A and B in O(log n) time. For example, if k = 1, your algorithm should return the smallest element of A ∪ B; if k = n, your algorithm should return the median of A ∪ B.) You can assume...
Why would log-structured merge trees be inappropriate (bad) for an application like git that manages source...
Why would log-structured merge trees be inappropriate (bad) for an application like git that manages source code? Explain
Given an array of n numbers, not necessarily in sorted order, we wish to find: (i)...
Given an array of n numbers, not necessarily in sorted order, we wish to find: (i) the range (max - min) and (ii) the interquartile range (IQR) of the numbers. IQR is defined as the difference between the number at the 25% percentile and the number at the 75% percentile. What is the exact number of comparisons needed for computing the range and why? Using any algorithm from the chapters of the textbook covered so far as a subroutine, give...
We would like to align two DNA sequences: (v) C G A T A C T,...
We would like to align two DNA sequences: (v) C G A T A C T, and (w) G A T T C G T : i) s(i, j) = 1.5 if vi = wj (matches); ii) s(i, j) = -1.0 if vi != wj (mismatches); iii) d = 0.25 (indels: insertions or deletions). What would be the maximum alignment score? Explain how you get the result.
Recall that in MergeSort algorithm, we used a linear-time subroutine called Merge which merges two sorted...
Recall that in MergeSort algorithm, we used a linear-time subroutine called Merge which merges two sorted lists into a single sorted list. Now suppose there are k sorted lists and there are n elements in total in these k lists. Design an efficient algorithm that merges these k sorted lists into one sorted list. The running time of your algorithm should be a function of both n and k.
Evaluate if the followings are Cauchy sequences or not. (a) an= (-1)n (b) an= (-1)n/n (c)...
Evaluate if the followings are Cauchy sequences or not. (a) an= (-1)n (b) an= (-1)n/n (c) an = n/(n+1) (d) an = (cos n)/n
We are given a non sorted array so that the values are not pairwise disjoint (the...
We are given a non sorted array so that the values are not pairwise disjoint (the same values may appear in many entries). Say that we can find the k-th smallest element number in the array in time O(n) for any 1 <= k <= n. Give an algorithm that finds if there is a value that appears at least n/3 times. Please explain the algorithm in words and analyze the run time.
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We...
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We want to return the k smallest element of A[1.....n], in non-decreasing order. For example: A = [5, 4, 6, 2, 10] and k = 4, the algorithm returns [2, 4, 5, 6]. There are at least the following four approaches: a. heapify A and then extract k elements one by one b. sort the array (e.g. using MergeSort or HeapSort) and then read the...
given the sequences x1 = cos (0.5*pi*n) + cos (0.25*pi*n) + cos (0.125*pi*n); for n =...
given the sequences x1 = cos (0.5*pi*n) + cos (0.25*pi*n) + cos (0.125*pi*n); for n = 0 to 7; x2 = sin (0.5*pi*n) - cos (o.25*pi*n) + sin (0.125*pi*n); for n = 0 to 7; plot the sequences and comment on the results. increasing the number of samples to n = 0 to 99, compute the DFT of the two sequences in MATLAB and plot the magnitude and phase of the computed DFTs. comment on the results
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT