Kat H.

asked • 10/05/21

MergeSort. Write the mergeSortInternal function. Do not use std::algorithms.

Write the mergeSortInternal function. Do not use std::algorithms.


You need to add your mergeFunc from the Merge Function problem. You cannot solve this problem without doing that one first.


Code:


#include <iostream>


using namespace std;


//-------------------------------------------------

//Put your mergeFunc here!

//-------------------------------------------------


/**

* @brief mergeSortInternal Recursive merge sort function

* @param arr Array of ints to sort

* @param low Index we should start sort at (inclusive)

* @param high Index we should sort up to (inclusive)

* @param temp Extra array that will be used to merge

*

* Note: we take temp in to avoid consantly allocating/deallocating

* new arrays

*/

void mergeSortInternal (int arr[], int low, int high, int temp[]) {

//TODO - Fixme


// base case: 1 or fewer elements is sorted

if (low >= high)

return;


//Set mid to halfway between low and high

//Recursively sort first low to mid

//Recursively sort second mid+1 to high


//Call mergeFunc to merge the two halves

}



/**

* @brief mergeSort Sorts the given array by building a temporary array

* and then calling the recursive mergeSortInternal

*/

void mergeSort(int arr[], int arrSize) {

int* temp = new int[arrSize];

mergeSortInternal (arr, 0, arrSize-1, temp);

delete [] temp;

}


tests.cpp:


//Bring in unit testing code and tell it to build a main function

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN

//This pragma supresses a bunch of warnings QTCreator produces (and should not)

//#pragma clang diagnostic ignored "-Woverloaded-shift-op-parentheses"

#include "doctest.h"


//Use Approx from doctest without saying doctest::Approx

using doctest::Approx;


//Declare functions we will test

void mergeFunc(int arr[], int low, int mid, int high, int temp[]);

void mergeSort(int arr[], int arrSize);


#include <algorithm>

#include <random>


using namespace std;



TEST_CASE( "MergeSort/Simple" ) {

int list[] = {12, 9, 16, 2, 1, 16, 18, 7};

const int size = 8;


//Copy to compare against

int list_key[size];

std::copy(list, list + size, list_key);

std::sort(list_key, list_key + size);


mergeSort(list, size);


bool isSorted = std::is_sorted(list, list + size);

REQUIRE( isSorted == true );


bool correctVals = std::equal(list, list + size, list_key);

INFO("Your array does not have the right elements!");

REQUIRE( correctVals == true );

}


//get value 0-999,999

int randInt() {

static default_random_engine generator;

static uniform_int_distribution<int> distribution(0,999999);

return distribution(generator);

}


TEST_CASE( "MergeSort/Big" ) {

const int size = 10001;

int* list = new int[size];

//Fill with random numbers

std::generate(list, list + size, randInt);


//Copy to compare against

int* list_key = new int[size];

std::copy(list, list + size, list_key);

std::sort(list_key, list_key + size);


mergeSort(list, size);


bool isSorted = std::is_sorted(list, list + size);

REQUIRE( isSorted == true );


bool correctVals = std::equal(list, list + size, list_key);

INFO("Your array does not have the right elements!");

REQUIRE( correctVals == true );

}

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.