CS174: Merge Sort Exercise (2 Points)

C++ frontend developed by, Professor Tralie, adopted from code by Todd Fleming. Canvas Autograder Developed by Professor Tralie and Professor Mongan.


Exercise Goals

The goals of this exercise are:
  1. Work with pointers and array indices in C++
  2. Implement recursive methods
  3. Implement merge sort
Fill in the merge method to merge two sorted arrays into one larger sorted array, which is the key step to combine recursive problems in merge sort. Click here to watch a video I made on merge sort if you'd like some help with this step. You can also watch a cute video here on a dance for merge sort.

Student Code

#include <stdio.h> #include <cstring> /** * @brief Print the elements in an array, separated by spaces * * @param x Array to print * @param N Length of array */ void printArray(float* x, int N) { for (int i = 0; i < N; i++) { printf("%g ", x[i]); } printf("\n"); } /** * @brief Merge the sorted elements in A and the sorted elements into B * into one full sorted list in the array y * * @param A First sorted array * @param M Length of first sorted array * @param B Second sorted array * @param N Length of second sorted array * @param y Staging area, assumed to have length at least M+N. The merged solution should go here */ void merge(float* A, int M, float* B, int N, float* y) { // TODO: Fill this in } /** * @brief A recursive helper function for merge sort * * @param x The array to sort * @param N Length of the array * @param y Staging area, which is assumed to have length at least N */ void mergeSortRec(float* x, int N, float* y) { if (N > 1) { int mid = N/2; float* A = x; float* B = A+mid; mergeSortRec(A, mid, y); mergeSortRec(B, N-mid, y); merge(A, mid, B, N-mid, y); memcpy(x, y, N*sizeof(float)); } } /** * @brief Entry point for sorting an array with merge sort. * By the end of this method, the elements in x will be sorted * * @param x Array to sort * @param N Length of array */ void mergeSort(float* x, int N) { float* y = new float[N]; // Staging area mergeSortRec(x, N, y); delete[] y; }

Main Area

int main() { int N = 100; float x[100] = { 94, 112, 151, 125, 5, 24, 147, 2, 111, 168, 42, 148, 30, 196, 173, 53, 47, 71, 63, 191, 29, 112, 8, 92, 15, 147, 92, 51, 15, 45, 30, 4, 15, 42, 153, 164, 132, 126, 122, 56, 21, 9, 114, 41, 10, 8, 146, 47, 6, 186, 191, 142, 85, 175, 132, 143, 165, 168, 68, 152, 37, 109, 165, 40, 104, 172, 177, 74, 168, 46, 18, 77, 47, 55, 172, 190, 151, 12, 6, 101, 99, 71, 35, 189, 41, 181, 154, 127, 175, 118, 179, 29, 14, 37, 67, 198, 37, 6, 105, 78}; mergeSort(x, N); printArray(x, N); return 0; }

Compiler Feedback

Program Output

Click compile to compile your code. If it succeeds with no syntax errors, then click run to run your code. If it passes the tests, it will automatically submit to Canvas and send you an e-mail. You must be connected to the Ursinus Network for submission to be successful! You will receive a copy of your code via e-mail, so you'll know that it was submitted if you receive that e-mail!
Please Enter your Ursinus netid before clicking run. This is not your ID number or your email. For example, my netid is ctralie (non Ursinus students can simply enter their name to get this to run, but they won't get an e-mail record or any form of credit)

Netid