Question: Write an insertion sort algorithm in C using the given codes. It includes four files: insertionSort.c, mySort.h, metrics.h, and sortDriver.c. Note: You only need to
Write an insertion sort algorithm in C using the given codes. It includes four files: insertionSort.c, mySort.h, metrics.h, and sortDriver.c.
Note: You only need to modify the insertionSort.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. Please post screenshots of your code working for insertionSort.c working.
Code for insertionSort.c
#include "mySort.h"
#include "metrics.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
Get step-by-step solutions from verified subject matter experts
