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>)
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.
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
The Binary Insertion Sort Algorithm is a variation of the Insertion Sort Algorithm that uses a...
The Binary Insertion Sort Algorithm is a variation of the Insertion Sort Algorithm that uses a binary search technique rather than a linear search technique to insert the ith element in the correct place among the previously sorted elements. (i) Express the Binary Insertion Sort Algorithm in pseudocode. (ii) Compare the number of comparisons of elements used by the Insertion Sort Algorithm and the Binary Insertion Sort Algorithm when sorting the list (7,4,3,8,1,5,4,2). (iii) Show that the Insertion Sort Algorithm...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT