Question: Step 1 : Implement the GetSortedRunLength ( ) member function Implement the GetSortedRunLength ( ) member function in NaturalMergeSorter.h . Access NaturalMergeSorter.h by clicking on
Step : Implement the GetSortedRunLength member function
Implement the GetSortedRunLength member function in NaturalMergeSorter.h Access NaturalMergeSorter.h by clicking on the orange arrow next to main.cpp at the top of the coding window.
GetSortedRunLength has three parameters:
array: a pointer to an array of integers,
arrayLength: an integer for the array's length, and
startIndex: an integer for the run's starting index.
The function returns the number of array elements sorted in ascending order, starting at startIndex and ending either at the end of the sorted run, or the end of the array, whichever comes first. The function returns if startIndex is out of bounds.
File main.cpp has several test cases for GetSortedRunLength that can be run by clicking the "Run program" button. One test case also exists for NaturalMergeSort but that can be ignored until step two is completed.
The program's output does not affect grading.
Submit for grading to ensure that the GetSortedRunLength unit tests pass before proceeding.
Step : Implement the NaturalMergeSort member function
Implement the NaturalMergeSort member function in NaturalMergeSorter.h NaturalMergeSort must:
Start at index i
Get the length of the first sorted run, starting at i
Return if the first run's length equals the array length
If the first run ends at the array's end, reassign i and repeat step
Get the length of the second sorted run, starting immediately after the first
Merge the two runs with the provided Merge function
Reassign i with the first index after the second run, or if the second run ends at the array's end
Go to step
NaturalMergeSorter.h: #ifndef NATURALMERGESORTERH
#define NATURALMERGESORTERH
class NaturalMergeSorter
public:
virtual int GetSortedRunLengthint array, int arrayLength, int startIndex
Your code here
virtual void NaturalMergeSortint array, int arrayLength
Your code here
virtual void Mergeint numbers, int leftFirst, int leftLast,
int rightLast
int mergedSize rightLast leftFirst ;
int mergedNumbers new intmergedSize;
int mergePos ;
int leftPos leftFirst;
int rightPos leftLast ;
Add smallest element from left or right partition to merged numbers
while leftPos leftLast && rightPos rightLast
if numbersleftPos numbersrightPos
mergedNumbersmergePos numbersleftPos;
leftPos;
else
mergedNumbersmergePos numbersrightPos;
rightPos;
mergePos;
If left partition isn't empty, add remaining elements to mergedNumbers
while leftPos leftLast
mergedNumbersmergePos numbersleftPos;
leftPos;
mergePos;
If right partition isn't empty, add remaining elements to mergedNumbers
while rightPos rightLast
mergedNumbersmergePos numbersrightPos;
rightPos;
mergePos;
Copy merged numbers back to numbers
for mergePos ; mergePos mergedSize; mergePos
numbersleftFirst mergePos mergedNumbersmergePos;
Free temporary array
delete mergedNumbers;
;
#endif
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
