Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Information Systems 1 International Baltic Conference Dbandis 2020 Tallinn Estonia June 19 2020 Proceedings

Authors: Tarmo Robal ,Hele-Mai Haav ,Jaan Penjam ,Raimundas Matulevicius

1st Edition

303057671X, 978-3030576714

More Books

Students also viewed these Databases questions

Question

How do we organise for international logistics?

Answered: 1 week ago