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
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...
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>)
* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in...
* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in place. * * @param students array to be sorted, can be empty, but cannot be null */ public static void sortGPA(Student[] students) { // TODO: implement this } Student class: public class Student extends Person { private double GPA; public Student(String lastName, String firstName, double gpa) { super(lastName, firstName); this.GPA = gpa; } public double getGPA() { return GPA; } @Override public boolean equals(Object...
using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers....
using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers. It should display the contents of the first array, then call a function to sort it using an ascending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using an ascending order selection sort, modified to print out the...
It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much...
It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much like the previous lab, you will be tasked with prompting the user for a list of values until the user enters 0 (you may use the same initializeVector that you wrote in the last lab). You will then write bubblesort which sorts the vector from smallest to largest. You will then call a function displayVector which displays the vector to the screen. Keep everything...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Comparing (Sort Algorithms) Both of the two sorting algorithms will do "sort" on arrays which would...
Comparing (Sort Algorithms) Both of the two sorting algorithms will do "sort" on arrays which would contain x randomly generated integers where the value of x would be 10000, 20000, 40000 and 80000 (inputs). The parts of the program should be followed as..... 1. Set x = 10000, randomly generate x integers. Call qsort function to sort these integers and get the execution time. 2. Randomly generate another x integers. Call your own sorting algorithm and get the execution time....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT