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++; }

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 19RQ
icon
Related questions
Question
100%

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++;
}


5:NaturalMergeSort() unit test
Five more test cases
Test feedback
2/5
Case 1:
PASS: Array with 11 elements is sorted.
PASS: Sorted array's elements are the same as the original array's eleme
FAIL: NaturalMergeSorter's merge () method was called a different number
Case 2:
PASS: Array with 12 elements is sorted.
PASS: Sorted array's elements are the same as the original array's eleme
PASS: merge () was called 1 time when sorting array [50, 55, 60, 65, 70,
Case 3:
PASS: Array with 12 elements is sorted.
PASS: Sorted array's elements are the same as the original array's eleme
FAIL: NaturalMerge Sorter's merge () method was called a different number
Case 4:
PASS: Array with 8 elements is sorted.
PASS: Sorted array's elements are the same as the original array's eleme
PASS: merge () was called 3 times when sorting array [98, 54, 77, 60, 12,
Case 5:
PASS: Array with 11 elements is sorted.
PASS: Sorted array's elements are the same as the original array's eleme
FAIL: NaturalMergeSorter's merge () method was called a different number
◄
Transcribed Image Text:5:NaturalMergeSort() unit test Five more test cases Test feedback 2/5 Case 1: PASS: Array with 11 elements is sorted. PASS: Sorted array's elements are the same as the original array's eleme FAIL: NaturalMergeSorter's merge () method was called a different number Case 2: PASS: Array with 12 elements is sorted. PASS: Sorted array's elements are the same as the original array's eleme PASS: merge () was called 1 time when sorting array [50, 55, 60, 65, 70, Case 3: PASS: Array with 12 elements is sorted. PASS: Sorted array's elements are the same as the original array's eleme FAIL: NaturalMerge Sorter's merge () method was called a different number Case 4: PASS: Array with 8 elements is sorted. PASS: Sorted array's elements are the same as the original array's eleme PASS: merge () was called 3 times when sorting array [98, 54, 77, 60, 12, Case 5: PASS: Array with 11 elements is sorted. PASS: Sorted array's elements are the same as the original array's eleme FAIL: NaturalMergeSorter's merge () method was called a different number ◄
3:Natural MergeSort() unit test
Test feedback
4:Natural MergeSort() unit test
Test feedback
0/1
PASS: Array with 11 elements is sorted.
PASS: Sorted array's elements are the same as the original array's eleme
FAIL: NaturalMergeSorter's merge () method was called a different number
0/1
PASS: Array with 45 elements is sorted.
PASS: Sorted array's elements are the same as the original array's eleme
FAIL: NaturalMergeSorter's merge () method was called a different number
Transcribed Image Text:3:Natural MergeSort() unit test Test feedback 4:Natural MergeSort() unit test Test feedback 0/1 PASS: Array with 11 elements is sorted. PASS: Sorted array's elements are the same as the original array's eleme FAIL: NaturalMergeSorter's merge () method was called a different number 0/1 PASS: Array with 45 elements is sorted. PASS: Sorted array's elements are the same as the original array's eleme FAIL: NaturalMergeSorter's merge () method was called a different number
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,