SlideShare a Scribd company logo
1 of 7
Download to read offline
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
Implement getSortedRunLength

More Related Content

Similar to Implement getSortedRunLength

9781439035665 ppt ch09
9781439035665 ppt ch099781439035665 ppt ch09
9781439035665 ppt ch09Terry Yoast
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory archana singh
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1sotlsoc
 
Mixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented languageMixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented languageMark Needham
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfNiravPanchal50
 
Groovy Refactoring Patterns
Groovy Refactoring PatternsGroovy Refactoring Patterns
Groovy Refactoring PatternsNaresha K
 
Write a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdfWrite a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdffashioncollection2
 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdfarshin9
 
Bubble sort, Selection sort SORTING .pptx
Bubble sort, Selection sort SORTING .pptxBubble sort, Selection sort SORTING .pptx
Bubble sort, Selection sort SORTING .pptxKalpana Mohan
 
Sorting_Algoritm-computee-scienceggn.pdf
Sorting_Algoritm-computee-scienceggn.pdfSorting_Algoritm-computee-scienceggn.pdf
Sorting_Algoritm-computee-scienceggn.pdfMohammed472103
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).pptyasser3omr
 
Java Code The traditional way to deal with these in Parsers is the .pdf
Java Code The traditional way to deal with these in Parsers is the .pdfJava Code The traditional way to deal with these in Parsers is the .pdf
Java Code The traditional way to deal with these in Parsers is the .pdfstopgolook
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2Mohamed Ahmed
 
Javathis is my current code i need help to implement public void s.pdf
Javathis is my current code i need help to implement public void s.pdfJavathis is my current code i need help to implement public void s.pdf
Javathis is my current code i need help to implement public void s.pdfeyelineoptics
 

Similar to Implement getSortedRunLength (20)

9781439035665 ppt ch09
9781439035665 ppt ch099781439035665 ppt ch09
9781439035665 ppt ch09
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
 
Mixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented languageMixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented language
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdf
 
Groovy Refactoring Patterns
Groovy Refactoring PatternsGroovy Refactoring Patterns
Groovy Refactoring Patterns
 
Chapter 3 Arrays in Java
Chapter 3 Arrays in JavaChapter 3 Arrays in Java
Chapter 3 Arrays in Java
 
Write a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdfWrite a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdf
 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
 
Bubble sort, Selection sort SORTING .pptx
Bubble sort, Selection sort SORTING .pptxBubble sort, Selection sort SORTING .pptx
Bubble sort, Selection sort SORTING .pptx
 
Sorting_Algoritm-computee-scienceggn.pdf
Sorting_Algoritm-computee-scienceggn.pdfSorting_Algoritm-computee-scienceggn.pdf
Sorting_Algoritm-computee-scienceggn.pdf
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
Java Code The traditional way to deal with these in Parsers is the .pdf
Java Code The traditional way to deal with these in Parsers is the .pdfJava Code The traditional way to deal with these in Parsers is the .pdf
Java Code The traditional way to deal with these in Parsers is the .pdf
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
Chap09
Chap09Chap09
Chap09
 
Javathis is my current code i need help to implement public void s.pdf
Javathis is my current code i need help to implement public void s.pdfJavathis is my current code i need help to implement public void s.pdf
Javathis is my current code i need help to implement public void s.pdf
 

More from aloeplusint

Starware Software was founded last year to develop software for gami.pdf
Starware Software was founded last year to develop software for gami.pdfStarware Software was founded last year to develop software for gami.pdf
Starware Software was founded last year to develop software for gami.pdfaloeplusint
 
Some obstacles in Project Development. One of the main goals of Proj.pdf
Some obstacles in Project Development. One of the main goals of Proj.pdfSome obstacles in Project Development. One of the main goals of Proj.pdf
Some obstacles in Project Development. One of the main goals of Proj.pdfaloeplusint
 
Sophocles Enterprises had the following pretax income (loss) over it.pdf
Sophocles Enterprises had the following pretax income (loss) over it.pdfSophocles Enterprises had the following pretax income (loss) over it.pdf
Sophocles Enterprises had the following pretax income (loss) over it.pdfaloeplusint
 
Solve using java and using this Singly linked list classpublic cl.pdf
Solve using java and using this Singly linked list classpublic cl.pdfSolve using java and using this Singly linked list classpublic cl.pdf
Solve using java and using this Singly linked list classpublic cl.pdfaloeplusint
 
SQL was created at IBM in the 1970s. Why was SQL createdSQL was .pdf
SQL was created at IBM in the 1970s. Why was SQL createdSQL was .pdfSQL was created at IBM in the 1970s. Why was SQL createdSQL was .pdf
SQL was created at IBM in the 1970s. Why was SQL createdSQL was .pdfaloeplusint
 
SQL- Write a select statement that returns the Freight cost from.pdf
SQL- Write a select statement that returns the Freight cost from.pdfSQL- Write a select statement that returns the Freight cost from.pdf
SQL- Write a select statement that returns the Freight cost from.pdfaloeplusint
 
Some of our most basic questions about the history of life concern w.pdf
Some of our most basic questions about the history of life concern w.pdfSome of our most basic questions about the history of life concern w.pdf
Some of our most basic questions about the history of life concern w.pdfaloeplusint
 
Southeastern Oklahoma State Universitys business program has the fa.pdf
Southeastern Oklahoma State Universitys business program has the fa.pdfSoutheastern Oklahoma State Universitys business program has the fa.pdf
Southeastern Oklahoma State Universitys business program has the fa.pdfaloeplusint
 
Sorensen Systems Inc. is expected to pay a $2.50 dividend at year en.pdf
Sorensen Systems Inc. is expected to pay a $2.50 dividend at year en.pdfSorensen Systems Inc. is expected to pay a $2.50 dividend at year en.pdf
Sorensen Systems Inc. is expected to pay a $2.50 dividend at year en.pdfaloeplusint
 
Subject Computer Architecture & Organization Q-4 Assume that .pdf
Subject Computer Architecture & Organization  Q-4 Assume that .pdfSubject Computer Architecture & Organization  Q-4 Assume that .pdf
Subject Computer Architecture & Organization Q-4 Assume that .pdfaloeplusint
 
Subject Management Information System Please complete the four qu.pdf
Subject Management Information System Please complete the four qu.pdfSubject Management Information System Please complete the four qu.pdf
Subject Management Information System Please complete the four qu.pdfaloeplusint
 
Subject Computer Architecture & Organization i. Show the con.pdf
Subject Computer Architecture & Organization  i. Show the con.pdfSubject Computer Architecture & Organization  i. Show the con.pdf
Subject Computer Architecture & Organization i. Show the con.pdfaloeplusint
 
Solve the following balance and income sheet for this specialty hosp.pdf
Solve the following balance and income sheet for this specialty hosp.pdfSolve the following balance and income sheet for this specialty hosp.pdf
Solve the following balance and income sheet for this specialty hosp.pdfaloeplusint
 
Su clase se ha ofrecido como voluntaria para trabajar por el Refer�n.pdf
Su clase se ha ofrecido como voluntaria para trabajar por el Refer�n.pdfSu clase se ha ofrecido como voluntaria para trabajar por el Refer�n.pdf
Su clase se ha ofrecido como voluntaria para trabajar por el Refer�n.pdfaloeplusint
 
Su compa��a de TI es responsable de crear programas de virus de soft.pdf
Su compa��a de TI es responsable de crear programas de virus de soft.pdfSu compa��a de TI es responsable de crear programas de virus de soft.pdf
Su compa��a de TI es responsable de crear programas de virus de soft.pdfaloeplusint
 
Study the cladogram above and match the names of the five organisms .pdf
Study the cladogram above and match the names of the five organisms .pdfStudy the cladogram above and match the names of the five organisms .pdf
Study the cladogram above and match the names of the five organisms .pdfaloeplusint
 
study of tension, compression, and shear and compare those three dir.pdf
study of tension, compression, and shear and compare those three dir.pdfstudy of tension, compression, and shear and compare those three dir.pdf
study of tension, compression, and shear and compare those three dir.pdfaloeplusint
 
Start Program like this Chapter 7 Validate Password import.pdf
Start Program like this  Chapter 7 Validate Password import.pdfStart Program like this  Chapter 7 Validate Password import.pdf
Start Program like this Chapter 7 Validate Password import.pdfaloeplusint
 
Students may research the local Aboriginal andor Torres Strait Isla.pdf
Students may research the local Aboriginal andor Torres Strait Isla.pdfStudents may research the local Aboriginal andor Torres Strait Isla.pdf
Students may research the local Aboriginal andor Torres Strait Isla.pdfaloeplusint
 
Stevie es un hombre ocupado. El viaja mucho. Usa las palabras entre .pdf
Stevie es un hombre ocupado. El viaja mucho. Usa las palabras entre .pdfStevie es un hombre ocupado. El viaja mucho. Usa las palabras entre .pdf
Stevie es un hombre ocupado. El viaja mucho. Usa las palabras entre .pdfaloeplusint
 

More from aloeplusint (20)

Starware Software was founded last year to develop software for gami.pdf
Starware Software was founded last year to develop software for gami.pdfStarware Software was founded last year to develop software for gami.pdf
Starware Software was founded last year to develop software for gami.pdf
 
Some obstacles in Project Development. One of the main goals of Proj.pdf
Some obstacles in Project Development. One of the main goals of Proj.pdfSome obstacles in Project Development. One of the main goals of Proj.pdf
Some obstacles in Project Development. One of the main goals of Proj.pdf
 
Sophocles Enterprises had the following pretax income (loss) over it.pdf
Sophocles Enterprises had the following pretax income (loss) over it.pdfSophocles Enterprises had the following pretax income (loss) over it.pdf
Sophocles Enterprises had the following pretax income (loss) over it.pdf
 
Solve using java and using this Singly linked list classpublic cl.pdf
Solve using java and using this Singly linked list classpublic cl.pdfSolve using java and using this Singly linked list classpublic cl.pdf
Solve using java and using this Singly linked list classpublic cl.pdf
 
SQL was created at IBM in the 1970s. Why was SQL createdSQL was .pdf
SQL was created at IBM in the 1970s. Why was SQL createdSQL was .pdfSQL was created at IBM in the 1970s. Why was SQL createdSQL was .pdf
SQL was created at IBM in the 1970s. Why was SQL createdSQL was .pdf
 
SQL- Write a select statement that returns the Freight cost from.pdf
SQL- Write a select statement that returns the Freight cost from.pdfSQL- Write a select statement that returns the Freight cost from.pdf
SQL- Write a select statement that returns the Freight cost from.pdf
 
Some of our most basic questions about the history of life concern w.pdf
Some of our most basic questions about the history of life concern w.pdfSome of our most basic questions about the history of life concern w.pdf
Some of our most basic questions about the history of life concern w.pdf
 
Southeastern Oklahoma State Universitys business program has the fa.pdf
Southeastern Oklahoma State Universitys business program has the fa.pdfSoutheastern Oklahoma State Universitys business program has the fa.pdf
Southeastern Oklahoma State Universitys business program has the fa.pdf
 
Sorensen Systems Inc. is expected to pay a $2.50 dividend at year en.pdf
Sorensen Systems Inc. is expected to pay a $2.50 dividend at year en.pdfSorensen Systems Inc. is expected to pay a $2.50 dividend at year en.pdf
Sorensen Systems Inc. is expected to pay a $2.50 dividend at year en.pdf
 
Subject Computer Architecture & Organization Q-4 Assume that .pdf
Subject Computer Architecture & Organization  Q-4 Assume that .pdfSubject Computer Architecture & Organization  Q-4 Assume that .pdf
Subject Computer Architecture & Organization Q-4 Assume that .pdf
 
Subject Management Information System Please complete the four qu.pdf
Subject Management Information System Please complete the four qu.pdfSubject Management Information System Please complete the four qu.pdf
Subject Management Information System Please complete the four qu.pdf
 
Subject Computer Architecture & Organization i. Show the con.pdf
Subject Computer Architecture & Organization  i. Show the con.pdfSubject Computer Architecture & Organization  i. Show the con.pdf
Subject Computer Architecture & Organization i. Show the con.pdf
 
Solve the following balance and income sheet for this specialty hosp.pdf
Solve the following balance and income sheet for this specialty hosp.pdfSolve the following balance and income sheet for this specialty hosp.pdf
Solve the following balance and income sheet for this specialty hosp.pdf
 
Su clase se ha ofrecido como voluntaria para trabajar por el Refer�n.pdf
Su clase se ha ofrecido como voluntaria para trabajar por el Refer�n.pdfSu clase se ha ofrecido como voluntaria para trabajar por el Refer�n.pdf
Su clase se ha ofrecido como voluntaria para trabajar por el Refer�n.pdf
 
Su compa��a de TI es responsable de crear programas de virus de soft.pdf
Su compa��a de TI es responsable de crear programas de virus de soft.pdfSu compa��a de TI es responsable de crear programas de virus de soft.pdf
Su compa��a de TI es responsable de crear programas de virus de soft.pdf
 
Study the cladogram above and match the names of the five organisms .pdf
Study the cladogram above and match the names of the five organisms .pdfStudy the cladogram above and match the names of the five organisms .pdf
Study the cladogram above and match the names of the five organisms .pdf
 
study of tension, compression, and shear and compare those three dir.pdf
study of tension, compression, and shear and compare those three dir.pdfstudy of tension, compression, and shear and compare those three dir.pdf
study of tension, compression, and shear and compare those three dir.pdf
 
Start Program like this Chapter 7 Validate Password import.pdf
Start Program like this  Chapter 7 Validate Password import.pdfStart Program like this  Chapter 7 Validate Password import.pdf
Start Program like this Chapter 7 Validate Password import.pdf
 
Students may research the local Aboriginal andor Torres Strait Isla.pdf
Students may research the local Aboriginal andor Torres Strait Isla.pdfStudents may research the local Aboriginal andor Torres Strait Isla.pdf
Students may research the local Aboriginal andor Torres Strait Isla.pdf
 
Stevie es un hombre ocupado. El viaja mucho. Usa las palabras entre .pdf
Stevie es un hombre ocupado. El viaja mucho. Usa las palabras entre .pdfStevie es un hombre ocupado. El viaja mucho. Usa las palabras entre .pdf
Stevie es un hombre ocupado. El viaja mucho. Usa las palabras entre .pdf
 

Recently uploaded

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 

Recently uploaded (20)

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 

Implement getSortedRunLength

  • 1. 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]) {
  • 2. 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; } }
  • 3. } 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++; }
  • 4. 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++; }
  • 5. 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
  • 6. 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