Step 1: Implement the getSortedRunLength() method Implement the getSortedRunLength() method in NaturalMergeSorter.java. Access NaturalMergeSorter.java by clicking on the orange arrow next to NaturalMerge.java at the top of the coding window. getSortedRunLength() has three parameters: array: a reference to an array of integers, arrayLength: an integer for the array's length, and startIndex: an integer for the run's starting index. The method 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 method returns 0 if startIndex is out of bounds. File NaturalMerge.java 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 2: Implement the naturalMergeSort() method Implement the naturalMergeSort() method in NaturalMergeSorter.java. naturalMergeSort() must: Start at index i=0 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=0 and repeat step 2 Get the length of the second sorted run, starting immediately after the first Merge the two runs with the provided merge() method Reassign i with the first index after the second run, or 0 if the second run ends at the array's end Go to step 2 NaturalMergeSorter sorter = new NaturalMergeSorter(); sorter.naturalMergeSort(arr5Copy, arr5Length); System.out.print("\n"); System.out.print((IsArraySorted.isSorted(arr5Copy, arr5Length) ? "PASS" : "FAIL")); System.out.print(": naturalMergeSort()"); System.out.print("\n"); System.out.print(" Array before calling naturalMergeSort(): " + Arrays.toString(arr5)); System.out.print("\n"); System.out.print(" Array after calling naturalMergeSort(): " + Arrays.toString(arr5Copy)); System.out.print("\n"); } } NaturalMergeSorter.java public class NaturalMergeSorter { public int getSortedRunLength(int[] array, int arrayLength, int startIndex) { if (startIndex < 0 || startIndex >= arrayLength) { return 0; } int length = 1; for (int i = startIndex + 1; i < arrayLength; i++) { if (array[i] >= array[i - 1]) { length++; } else { break; } } return length; } public void naturalMergeSort(int[] array, int arrayLength) { int i = 0; while (i < arrayLength) { int firstRunLength = getSortedRunLength(array, arrayLength, i); if (firstRunLength == arrayLength) { return; } int secondRunLength = getSortedRunLength(array, arrayLength, i + firstRunLength); merge(array, i, i + firstRunLength - 1, i + firstRunLength + secondRunLength - 1); if (i + firstRunLength + secondRunLength == arrayLength) { i = 0; } else { i = i + firstRunLength + secondRunLength; } } } public void merge(int[] numbers, int leftFirst, int leftLast, int rightLast) { int mergedSize = rightLast - leftFirst + 1; int[] mergedNumbers = new int[mergedSize]; int mergePos = 0; int leftPos = leftFirst; int rightPos = leftLast + 1; // Add smallest element from left or right partition to merged numbers while (leftPos <= leftLast && rightPos <= rightLast) { if (numbers[leftPos] <= numbers[rightPos]) { mergedNumbers[mergePos] = numbers[leftPos]; leftPos++; } else { mergedNumbers[mergePos] = numbers[rightPos]; rightPos++; } mergePos++; }
Step 1: Implement the getSortedRunLength() method Implement the getSortedRunLength() method in NaturalMergeSorter.java. Access NaturalMergeSorter.java by clicking on the orange arrow next to NaturalMerge.java at the top of the coding window. getSortedRunLength() has three parameters: array: a reference to an array of integers, arrayLength: an integer for the array's length, and startIndex: an integer for the run's starting index. The method 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 method returns 0 if startIndex is out of bounds. File NaturalMerge.java 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 2: Implement the naturalMergeSort() method Implement the naturalMergeSort() method in NaturalMergeSorter.java. naturalMergeSort() must: Start at index i=0 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=0 and repeat step 2 Get the length of the second sorted run, starting immediately after the first Merge the two runs with the provided merge() method Reassign i with the first index after the second run, or 0 if the second run ends at the array's end Go to step 2
NaturalMergeSorter sorter = new NaturalMergeSorter();
sorter.naturalMergeSort(arr5Copy, arr5Length);
System.out.print("\n");
System.out.print((IsArraySorted.isSorted(arr5Copy, arr5Length) ? "PASS" : "FAIL"));
System.out.print(": naturalMergeSort()");
System.out.print("\n");
System.out.print(" Array before calling naturalMergeSort(): " + Arrays.toString(arr5));
System.out.print("\n");
System.out.print(" Array after calling naturalMergeSort(): " + Arrays.toString(arr5Copy));
System.out.print("\n");
}
}
NaturalMergeSorter.java
public class NaturalMergeSorter {
public int getSortedRunLength(int[] array, int arrayLength,
int startIndex) {
if (startIndex < 0 || startIndex >= arrayLength) {
return 0;
}
int length = 1;
for (int i = startIndex + 1; i < arrayLength; i++) {
if (array[i] >= array[i - 1]) {
length++;
} else {
break;
}
}
return length;
}
public void naturalMergeSort(int[] array, int arrayLength) {
int i = 0;
while (i < arrayLength) {
int firstRunLength = getSortedRunLength(array, arrayLength, i);
if (firstRunLength == arrayLength) {
return;
}
int secondRunLength = getSortedRunLength(array, arrayLength, i + firstRunLength);
merge(array, i, i + firstRunLength - 1, i + firstRunLength + secondRunLength - 1);
if (i + firstRunLength + secondRunLength == arrayLength) {
i = 0;
} else {
i = i + firstRunLength + secondRunLength;
}
}
}
public void merge(int[] numbers, int leftFirst, int leftLast,
int rightLast) {
int mergedSize = rightLast - leftFirst + 1;
int[] mergedNumbers = new int[mergedSize];
int mergePos = 0;
int leftPos = leftFirst;
int rightPos = leftLast + 1;
// Add smallest element from left or right partition to merged numbers
while (leftPos <= leftLast && rightPos <= rightLast) {
if (numbers[leftPos] <= numbers[rightPos]) {
mergedNumbers[mergePos] = numbers[leftPos];
leftPos++;
}
else {
mergedNumbers[mergePos] = numbers[rightPos];
rightPos++;
}
mergePos++;
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images