Home / Expert Answers / Computer Science / step-1-implement-the-getsortedrunlength-method-implement-the-getsortedrunlength-method-in-natu-pa969

(Solved): Step 1: Implement the getSortedRunLength() method Implement the getSortedRunLength() method in Natu ...



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.java

public class NaturalMergeSorter {
public static 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 static int getSortedRunLength(double[] 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 static 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 static void naturalMergeSort(double[] 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 static 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++;
}

// If left partition isn't empty, add remaining elements to mergedNumbers
while (leftPos <= leftLast) {
mergedNumbers[mergePos] = numbers[leftPos];
leftPos++;
mergePos++;
}

// If right partition isn't empty, add remaining elements to mergedNumbers
while (rightPos <= rightLast) {
mergedNumbers[mergePos] = numbers[rightPos];
rightPos++;
mergePos++;
}

// Copy merged numbers back to numbers
for (mergePos = 0; mergePos < mergedSize; mergePos++) {
numbers[leftFirst + mergePos] = mergedNumbers[mergePos];
}

// Free temporary array
mergedNumbers = null;
}
public static void merge(double[] numbers, int leftFirst, int leftLast, int rightLast) {
int mergedSize = rightLast - leftFirst + 1;
double[] mergedNumbers = new double[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++;
}

// If left partition isn't empty, add remaining elements to mergedNumbers
while (leftPos <= leftLast) {
mergedNumbers[mergePos] = numbers[leftPos];
leftPos++;
mergePos++;
}

// If right partition isn't empty, add remaining elements to mergedNumbers
while (rightPos <= rightLast) {
mergedNumbers[mergePos] = numbers[rightPos];
rightPos++;
mergePos++;
}

// Copy merged numbers back to numbers
for (mergePos = 0; mergePos < mergedSize; mergePos++) {
numbers[leftFirst + mergePos] = mergedNumbers[mergePos];
}

// Free temporary array
mergedNumbers = null;
}

}

The errors I've received. Please fix the code and explain what happened.

3:NaturalMergeSort() unit testkeyboard_arrow_up

0 / 1

Compilation failed

Compilation failed

./GradingSorter.java:17: error: getSortedRunLength(int[],int,int) in GradingSorter cannot override getSortedRunLength(int[],int,int) in NaturalMergeSorter public int getSortedRunLength(int[] array, int arrayLength, int startIndex) { ^ overridden method is static ./GradingSorter.java:16: error: method does not override or implement a method from a supertype @Override ^ ./GradingSorter.java:30: error: merge(int[],int,int,int) in GradingSorter cannot override merge(int[],int,int,int) in NaturalMergeSorter public void merge(int[] array, int leftFirst, int leftLast, int rightLast) { ^ overridden method is static ./GradingSorter.java:29: error: method does not override or implement a method from a supertype @Override ^ ./GradingSorter.java:54: error: naturalMergeSort(int[],int) in GradingSorter cannot override naturalMergeSort(int[],int) in NaturalMergeSorter public void naturalMergeSort(int[] array, int arrayLength) { ^ overridden method is static ./GradingSorter.java:53: error: method does not override or implement a method from a supertype @Override ^ 6 errors

4:NaturalMergeSort() unit testkeyboard_arrow_up

0 / 1

Compilation failed

Compilation failed

./GradingSorter.java:17: error: getSortedRunLength(int[],int,int) in GradingSorter cannot override getSortedRunLength(int[],int,int) in NaturalMergeSorter public int getSortedRunLength(int[] array, int arrayLength, int startIndex) { ^ overridden method is static ./GradingSorter.java:16: error: method does not override or implement a method from a supertype @Override ^ ./GradingSorter.java:30: error: merge(int[],int,int,int) in GradingSorter cannot override merge(int[],int,int,int) in NaturalMergeSorter public void merge(int[] array, int leftFirst, int leftLast, int rightLast) { ^ overridden method is static ./GradingSorter.java:29: error: method does not override or implement a method from a supertype @Override ^ ./GradingSorter.java:54: error: naturalMergeSort(int[],int) in GradingSorter cannot override naturalMergeSort(int[],int) in NaturalMergeSorter public void naturalMergeSort(int[] array, int arrayLength) { ^ overridden method is static ./GradingSorter.java:53: error: method does not override or implement a method from a supertype @Override ^ 6 errors

5:NaturalMergeSort() unit testkeyboard_arrow_up

0 / 5

Five more test cases

Compilation failed

Compilation failed

./GradingSorter.java:17: error: getSortedRunLength(int[],int,int) in GradingSorter cannot override getSortedRunLength(int[],int,int) in NaturalMergeSorter public int getSortedRunLength(int[] array, int arrayLength, int startIndex) { ^ overridden method is static ./GradingSorter.java:16: error: method does not override or implement a method from a supertype @Override ^ ./GradingSorter.java:30: error: merge(int[],int,int,int) in GradingSorter cannot override merge(int[],int,int,int) in NaturalMergeSorter public void merge(int[] array, int leftFirst, int leftLast, int rightLast) { ^ overridden method is static ./GradingSorter.java:29: error: method does not override or implement a method from a supertype @Override ^ ./GradingSorter.java:54: error: naturalMergeSort(int[],int) in GradingSorter cannot override naturalMergeSort(int[],int) in NaturalMergeSorter public void naturalMergeSort(int[] array, int arrayLength) { ^ overridden method is static ./GradingSorter.java:53: error: method does not override or implement a method from a supertype @Override ^ 6 errors



We have an Answer from Expert

View Expert Answer

Expert Answer


We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe