Question: Write an merge sort algorithm in C using the given codes. It includes four files: mergeSort.c, mySort.h, metrics.h, and sortDriver.c. Note: You only need to

Write an merge sort algorithm in C using the given codes. It includes four files: mergeSort.c, mySort.h, metrics.h, and sortDriver.c.

Note: You only need to modify the mergeSort.c, based on the code for mySort.h, metrics.h, and sortDriver.c. DO NOT modify the code for mySort.h, metrics.h, and sortDriver.c.

Code for mergeSort.c

#include "mySort.h"

void mySort(int array[], unsigned int first, unsigned int last)

{

}

Code for mySort.h

#ifndef MYSORT_H

#define MYSORT_H

#include "metrics.h"

/* The restriction to testing your sort algorithms to collections

* of no more than one hundred thousand elements is encapsulated

* with the following "#define":

*/

#define MAX_SIZE_N_TO_SORT 100000

/* prototype for "sort" function */

void mySort(int data[], unsigned int first, unsigned int last);

#endif /* #ifndef MYSORT_H */

Code for metrics.h

#ifndef METRICS_H

#define METRICS_H

int myCompare(int, int);

void mySwap(int *, int *);

void myCopy(const int *, int *);

unsigned int getNumCompares();

unsigned int getNumCopies();

unsigned int getNumSwaps();

#endif /* #ifndef METRICS_H */

Code for sortDriver.c

#include

#include

#include "mySort.h"

#include "metrics.h"

int main(int argc, char * argv[])

{

int a[MAX_SIZE_N_TO_SORT];

unsigned int array_size, i;

if (argc != 1) {

fprintf(stderr, "Usage: %s ALONE with NO additional command line args ",

argv[0]);

exit(1);

}

/* Read ints from stdin into an array */

for(array_size = 0; (scanf("%d", &a[array_size]) != EOF)

&& (array_size < MAX_SIZE_N_TO_SORT);

array_size++)

;

/* sort the array */

if(array_size > 0) {

mySort(a, 0, array_size-1);

}

/* Print out the modified array */

for(i = 0; i < array_size; i++)

printf("%d ", a[i]);

/* Print stats */

fprintf(stderr, "Comparisons: %d ", getNumCompares());

fprintf(stderr, "Swaps: %d ", getNumSwaps());

fprintf(stderr, "Copy operations: %d ", getNumCopies());

exit(0);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!