Question

In: Computer Science

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.

Solutions

Expert Solution

1.#include<iostream>

using namespace std;

  

// Merge arr1[0..n1-1] and arr2[0..n2-1] into

// arr3[0..n1+n2-1]

void mergeArrays(int arr1[], int arr2[], int n1,

int n2, int arr3[])

{

int i = 0, j = 0, k = 0;

  

// Traverse both array

while (i<n1 && j <n2)

{

// Check if current element of first

// array is smaller than current element

// of second array. If yes, store first

// array element and increment first array

// index. Otherwise do same with second array

if (arr1[i] < arr2[j])

arr3[k++] = arr1[i++];

else

arr3[k++] = arr2[j++];

}

  

// Store remaining elements of first array

while (i < n1)

arr3[k++] = arr1[i++];

  

// Store remaining elements of second array

while (j < n2)

arr3[k++] = arr2[j++];

}

  

// Driver code

int main()

{

int arr1[] = {1, 3, 5, 7};

int n1 = sizeof(arr1) / sizeof(arr1[0]);

  

int arr2[] = {2, 4, 6, 8};

int n2 = sizeof(arr2) / sizeof(arr2[0]);

  

int arr3[n1+n2];

mergeArrays(arr1, arr2, n1, n2, arr3);

  

cout << "Array after merging" <<endl;

for (int i=0; i < n1+n2; i++)

cout << arr3[i] << " ";

  

return 0;

}

2.#include<bits/stdc++.h>

using namespace std;

  

// Function to merge arrays

void mergeArrays(int a[], int b[], int n, int m)  

{

// Declaring a map.

// using map as a inbuilt tool

// to store elements in sorted order.

map<int, bool> mp;

  

// Inserting values to a map.

for(int i = 0; i < n; i++)

mp[a[i]] = true;

  

for(int i = 0;i < m;i++)

mp[b[i]] = true;

  

// Printing keys of the map.

for(auto i: mp)

cout<< i.first <<" ";

}

  

// Driver Code

int main()

{  

int a[] = {1, 3, 5, 7}, b[] = {2, 4, 6, 8};

  

int size = sizeof(a)/sizeof(int);

int size1 = sizeof(b)/sizeof(int);

  

// Function call

mergeArrays(a, b, size, size1);

  

return 0;

}


Related Solutions

Write a function called is_valid_phone_number matches that takes two int arrays and their respective sizes, and...
Write a function called is_valid_phone_number matches that takes two int arrays and their respective sizes, and returns the number of consecutive values that match between the two arrays starting at index 0. Suppose the two arrays are {3, 2, 5, 6, 1, 3} and {3, 2, 5, 2, 6, 1, 3} then the function should return 3 since the consecutive matches are for values 3, 2, and 5. in C++ with explanations
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.
write the “largerComponents” method that takes in two integer arrays and returns true or false if...
write the “largerComponents” method that takes in two integer arrays and returns true or false if the first array’s components are strictly greater than the second array’s components. The arrays must be the same size or else return false. Clarification Note: Components meaning each value at a specific index. For instance, if we had the arrays {5,2,7} and {1,3,1} then this method would return false as the value “2” is not greater than “3”. here is a provided code //Do...
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
In C++, write a function that takes in as inputs two arrays, foo and bar, and...
In C++, write a function that takes in as inputs two arrays, foo and bar, and their respective array sizes. The function should then output the concatenation of the two arrays as a singly linked list. You may assume that you are provided a singly linked list header file.
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these...
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these 2 Arrays are equal or not, and also if the elements are all even numbers. Describe under what conditions these 2 Arrays would be considered equal.
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop...
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop invariant for loop 1: “the contents ofnewlistare in sorted order,newlistcontainsaelements oflistAandbelements oflistB” def mergeSortedLists(listA, listB):  newlist = [ ]  a = 0  b = 0  # loop 1  while a < len(listA) and b < len(listB):   if listA[a] < listB[b]:    newlist.append(listA[a])    a +=1   else:    newlist.append(listB[b])    b +=1  if a < len(listA):   newlist.extend(listA[a:])  if b < len(listB):   newlist.extend(listB[b:])  return newlist (a) Write down (in regular...
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and...
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and returns true if the matrix is an upper matrix and returns false otherwise. Remark: A matrix M is an upper matrix if it is a square matrix (row size = column size) and every element M[i][j] = 0 for all i > j. That is all the elements of the matrix below the main diagonal are zero.
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...
Edit question Write a program that merges two files as follows. The two files are in...
Edit question Write a program that merges two files as follows. The two files are in the docsharing which you can download it. One file will contain usernames(usernames.txt):foster001smith023nyuyen002...The other file will contain passwords(passwords.txt):x34rdf3ep43e4rddw32eds22...The program should create a third file matching username and passwords(usernamesPasswords.txt):foster001x34rdf3esmith023p43e4rddnyuyen002w32eds22......Give the user of your programs the option of displaying you output file. CAN ANYONE SOLVE THIS IN C
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT