Question

In: Computer Science

Write the algorithm, following the ideas given in class, for merging two sorted arrays into one...

Write the algorithm, following the ideas given in class, for merging two sorted arrays into one sorted array. Note the algorithm is not the one for merge sort. 2) What is the best asymptotic upper bound for the algorithm? List reasoning steps.

Solutions

Expert Solution

Algorithm:

1. Let two sorted array be A and B of size m and n respectively.

2. Create a new array C of size m+n

3. initialize-variables i=0,j=0,k=0

4. while I<n and j<m

         if A[i] < B[j]

              C[k] = A[i]

i = i+1, k = k+1

          else

               C[k] = B{j]

i = i+1, k=k+1

5. if i=n meaning all values of array A has been added to C. We need to assigned restover value of array B to C

     while j < m

         C[k] = B[j]

          k=k+1, j=j+1

6. if j=m meaning all values of array B has been added to C. We need to assigned restover value of array A to C

     while i < n

         C[k] = A[j]

          k=k+1, i=i+1

7. C is is merged array formed by merging two sorted array A and B


The asymptotic upper bound for the algorithm will be O(m+n).

This is because all the assignment operator takes constant time and loop is looping for m+n times. In this algorithm, we will get time complexity of a + b*(m+n) where a and b are positive constants and a represent assignment operation time and b times assignment operation in each loop for m+n times so b*(m+n). Hence, the asymptotic upper bound for the algorithm will be O(m+n).



Related Solutions

Write and test a merge function that uses a recursive algorithm to merge two sorted arrays...
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays of integers. Neither list contains duplicates, and the resulting list should not contain duplicates either. Hint: You may want to call a helper function from merge. PROGRAM: C
Write a C program. Problem 1: You are given two sorted arrays, A and B, and...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order without using any other array space. Implementation instruction: (1) Define one array of size 18 for A and the other array of size 5 for B. (2) Initialize A by inputting 13 integers in the ascending order, and Initialize B by...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order without using any other array space. Implementation instruction: (1) Define one array of size 18 for A and the other array of size 5 for B. (2) Initialize A by inputting 13 integers in the ascending order, and Initialize B by...
Write a method that takes two Sorted Arrays of different sizes and merges them into one...
Write a method that takes two Sorted Arrays of different sizes and merges them into one sorted array, and use the method to write a full recursive Merge Sort Algorithm.
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
Design an O(nlogn) algorithm that takes two arrays A and B (not necessarily sorted) of size...
Design an O(nlogn) algorithm that takes two arrays A and B (not necessarily sorted) of size n of real numbers and a value v. The algorithm returns i and j if there exist i and j such that A[i] + B[j] = v and no otherwise. Describe your algorithm in English and give its time analysis.
Given two unsorted arrays of integers. a) Write a pseudocode algorithm which will output only the...
Given two unsorted arrays of integers. a) Write a pseudocode algorithm which will output only the integers not common to both arrays. When writing pseudocode, consider that no implementation for data structures or algorithms exist. b) Implement your algorithm in Modern C++
Design an O(n log n) algorithm that takes two arrays A and B (not necessarily sorted)...
Design an O(n log n) algorithm that takes two arrays A and B (not necessarily sorted) of size n of real numbers and a value v. The algorithm returns i and j if there exist i and j such that A[i] + B[j] = v and no otherwise. Describe your algorithm in English and give its time analysis.
(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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT