Question

In: Computer Science

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

Solutions

Expert Solution

#include<stdio.h>

// Merges two sorted arrays avoiding duplicates and returns the size of merged array

// arr1 --> First array
// arr2 --> Second array
// merged_arr --> Array formed upon merging, the final array
// l1 --> Length of the first array
// l2 --> Length of the second array
// idx1 --> Current index of arr1 in recursion
// idx2 --> Current index of arr2 in recursion
// idx3 --> Current index of merged_arr in recursion
int merge(int arr1[], int arr2[], int merged_arr[], int l1, int l2, int idx1, int idx2, int idx3)
{
    
    // Both arrays exist
        if(idx1 < l1 && idx2 < l2)
        {
            // Element under consideration of first array is smaller than the second array 
                if(arr1[idx1] < arr2[idx2])
                {
                        // Element under consideration = arr1[idx1]
                        
                    // At least one element exists in the merged array
                    // and the last element added to merged array is equal
                    // to this element. So, skip it otherwise it would result in duplicates
                        if(idx3 >=1 && merged_arr[idx3-1] == arr1[idx1])
                                return merge(arr1,arr2,merged_arr,l1,l2,idx1+1,idx2,idx3);
                                
                        // Add this element to merged_arr 
                        else
                        {
                                merged_arr[idx3] = arr1[idx1];
                                return merge(arr1,arr2,merged_arr,l1,l2,idx1+1,idx2,idx3+1);
                        }
                }
                
        // If the element under consideration of second array is smaller than the first array 
                else
                {
                    // Element under consideration = arr2[idx2]
                    
                    // At least one element exists in the merged array
                    // and the last element added to merged array is equal
                    // to this element. So, skip it otherwise it would result in duplicates
                        if(idx3 >=1 && merged_arr[idx3-1] == arr2[idx2])
                                return merge(arr1,arr2,merged_arr,l1,l2,idx1,idx2+1,idx3);
                                
            // Add this element to merged_arr                   
                        else
                        {
                                merged_arr[idx3] = arr2[idx2];
                                return merge(arr1,arr2,merged_arr,l1,l2,idx1,idx2+1,idx3+1);
                        }
                }
        }
        
    // One of the array is exhausted (finished traversing)
        else
        {
            
            // The first array still remains 
                if(idx1 < l1)
                {
                    
                    // This element is equal to last element of merged_arr
                    // Skip it  
                        if(idx3 >= 1 && merged_arr[idx3-1] == arr1[idx1]) 
                                return merge(arr1,arr2,merged_arr,l1,l2,idx1+1,idx2,idx3);
                                
            // Add this element to merged_arr
                        else
                        {
                                merged_arr[idx3] = arr1[idx1];
                                return merge(arr1,arr2,merged_arr,l1,l2,idx1+1,idx2,idx3+1);
                        }
                }
                
        // The second array still remains               
                if(idx2 < l2)
                {
                    
                    // This element is equal to last element of merged_arr
                    // Skip it  
                        if(idx3 >= 1 && merged_arr[idx3-1] == arr2[idx2]) 
                                return merge(arr1,arr2,merged_arr,l1,l2,idx1,idx2+1,idx3);
                                
                        // Add this element to merged_arr
                        else
                        {
                                merged_arr[idx3] = arr2[idx2];
                                return merge(arr1,arr2,merged_arr,l1,l2,idx1,idx2+1,idx3+1);
                        }       
                }
        }
        
    // Returning the size of merged array       
        return idx3;
}

// Driver method
int main()
{
    // Size of array 1 and array 2
        int n1 = 5;
        int n2 = 3;
        int arr1[] = {1, 2, 3, 4, 5};
        int arr2[] = {1, 2, 10};
        
    // Declaring merged_arr of size n1 + n2     
        int merged_arr[n1+n2];
        
        int size = merge(arr1, arr2, merged_arr, n1, n2, 0, 0, 0);
        for(int i=0;i<size;i++)
            printf("%d ", merged_arr[i]);
            
        return 0;
} 



Related Solutions

Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
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.
Ackermann’s function is a recursive mathematical algorithm that can be used to test how well a...
Ackermann’s function is a recursive mathematical algorithm that can be used to test how well a computer performs recursion. Write a method ackermann(m, n), which solves Ackermann’s function. Use the following logic in your method: If m = 0, then return n + 1 If n = 0, then return ackermann(m-1, 1) Otherwise, return ackermann(m -1, ackermann(m, n - 1)) Test your method in a java program that displays the return values of the following method calls: ackermann(0, 0), ackermann(0,...
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.
C++ Ackermann’s function is a recursive mathematical algorithm that can be used to test how well...
C++ Ackermann’s function is a recursive mathematical algorithm that can be used to test how well a computer performs recursion. Write a function A(m, n) that solves Ackermann’s function. Use the following logic in your function: If m = 0 then      return n + 1 If n = 0 then       return A(m−1, 1) Otherwise,          return A(m–1, A(m, n–1)) Test your function in a driver program that displays the following values: A(0, 0)   A(0, 1)   A(1, 1)    A(1,...
I'm having trouble implementing the merge function on this recursive merge sort. I am not allowed...
I'm having trouble implementing the merge function on this recursive merge sort. I am not allowed to change the parameter list in the functions. This is what I have so far, written in C. So far I've been getting segfaults, and I feel like it's because I'm off by 1, somewhere... void merge_the_array(int *list, int l1, int h1, int l2, int h2){ // create a temp array for l1-h1 int arr_a[(h1-l1)+1]; // create a temp array for l2-h2, adding 1...
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.
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
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++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT