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...
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.
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.
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
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
Section 1: Given a system y[n]-y[n-1]+y[n-2]=x[n] (refer to M3.2 on textbook) In class, we analytically derived...
Section 1: Given a system y[n]-y[n-1]+y[n-2]=x[n] (refer to M3.2 on textbook) In class, we analytically derived the solutions of second order difference equations, including zero-input response, unit impulse response, zero-state response and total response. The Matlab has imbedded commands to do the same job. Get familiar with the following commends, and use them to get (0≤n≤40) a) unit impulse response and plot it b) zero-input response and plot it, with initial conditions of y[-1]=1 and y[-2]=2 c) zero-state response and...
given the sequences  x1 = [2, 6, -4, 1] x2 = [8, 0, 2, 0, -9,...
given the sequences  x1 = [2, 6, -4, 1] x2 = [8, 0, 2, 0, -9, 0, 1, 0] x3 = [2, 0, -8, -8, 2] x4 = [0, 1, 5i, 0, 6i, 0] x5 = [9, 3, 7] plot the 1. DFT magnitude of the computed sequences in MATLAB  2. phase responses in degrees and radians against frequency and number of samples 3. comment on the plots
You are given a system with n equations and n-2 variables. Is it possible for the...
You are given a system with n equations and n-2 variables. Is it possible for the solution set to be the span of a single vector? Why or why not
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT