Question

In: Computer Science

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[ ] ).

Solutions

Expert Solution

Hi,

I am attaching the C file as well as pasting it here. You have to create an input file for containing the array elements in a file called array.txt. A sample of this array.txt is also sent for use as an example.

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
int *merge(int left[], int right[], int n)
{
  int *ord, i=0, j=0, k;
  ord = malloc(sizeof(int)*n);
  while((i<n/2) && (j<n-n/2)){
      if(left[i] < right[j]){
          *(ord+i+j) = left[i];
          i++;
        }
      else{
          *(ord+i+j) = right[j];
          j++;
          }
    }
  while(i!=n/2){
       *(ord+i+j) = left[i];
       i++; 
    }
  while(j!=n-n/2){
       *(ord+i+j) = right[j];
       j++; 
    }
  return ord;      
 }


int *merge_sort(int *arr, int n){
      int k = 0, *left, *right, *ord;
      ord = malloc(sizeof(int)*n);
      left = malloc(sizeof(int)*(n/2));
      right = malloc(sizeof(int)*(n-(n/2)));
      if (n<2){
         ord = arr;
      }else{
         for(k=0; k<n/2; k++)
             *(left + k) = *(arr + k);
         for(k=n/2; k<n; k++)
             *(right + k -(n/2)) = *(arr + k);

         left = merge_sort(left, n/2);
         right = merge_sort(right, n-(n/2));
         ord = merge(left, right, n);
      }      
      return ord;
}

int main(){
     FILE *inp;
     inp = fopen("array.txt", "r");
     if(!inp) printf("Error");

     int tot = 100;//the max no. of elements
     
     int *array,i,n;
     array = malloc(sizeof(int)*tot);
     
     int indice = 0;
     while(fscanf(inp,"%d", (array + indice)) != EOF) indice++;

     int *ord = merge_sort(array, indice);

     int k;
     for(k=0; k<indice; k++) printf("%d: %d \n",k, *(ord+k));


     fclose(inp);     
}

Array.txt.

14
7
2
9
31
45
87
34

Output:

Some screenshots of the file

Hope this helps!


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
Consider a sorting algorithm that combines merge sort and insertion sort algorithm. We still use divide...
Consider a sorting algorithm that combines merge sort and insertion sort algorithm. We still use divide and conquer like merge sort, however when the number of elements in an array is at most k elements (k is a parameter), we stop dividing the elements as the regular merge sort, instead, we call the insertion sort. Assuming we have defined the following two procedures: insertion-sort(A[p..q]) which sort the subarray A[p..q] merge(A[p,q,r]) which merges the sorted subarray A[p..r] and A[r+1..q] Try to...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content after each pass (i.e., from pass 1 to pass 9). Each pass is defined as the completion of one merge operation. Suppose an array contains the following integers: -1, 20, 10, -5, 0, -7, 100, -7, 30, -10. Sort the array using the following algorithms: selection sort, bubble sort, and insertion sort. For each algorithm, write the array content after each pass (i.e., from...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
Write a program that performs a merge-sort algorithm without using a recursion. Only using pointers. C++...
Write a program that performs a merge-sort algorithm without using a recursion. Only using pointers. C++ programming language; #include<iostream>
The multi-merge sort algorithm (M&M sort) works like merge sort, except that instead of dividing the...
The multi-merge sort algorithm (M&M sort) works like merge sort, except that instead of dividing the array into 2 partitions, it divides the array into p partitions. It recursively sorts each of the p partitions and then merges the results doing a p-way merge (except, of course, for the base case of a singleton or empty array). (a) Write the recurrence for T(n) for M&M sort for the case where p is a constant. Then, give a closed-form solution to...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick sort? Give a simple scheme that makes any sorting algorithm stable. How much additional time and space does your scheme entail?
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
External sort: Write an external merge sort in c++ that takes in an input file with...
External sort: Write an external merge sort in c++ that takes in an input file with 120 integers. You are allowed to hold a maximum of 20 integers in memory. You must create 2 files to process the integers and they must be sorted and placed in the original file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT