
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
Write the full Java code for NaturalMergeSorter.java

Transcribed Image Text:12.19 LAB: Natural merge sort
The merge sort algorithm recursively divides the array in half until an array with one element is reached. A variant of merge
sort, called natural merge sort, instead finds already-sorted runs of elements and merges the runs together.
Ex: The unsorted array below has five sorted runs.
17
58 96 24 88 70 23 64 74
81
55
A
B
C
D
E
Natural merge sort starts at index 0 and finds sorted runs A and B. Runs A and B are merged, using the same merging
algorithm as merge sort, to make run F.
17 24 58
88 96 70 23
64 74 81
55
F
с
D
E
Next, the algorithm starts after the merged part F, again looking for two sequential, sorted runs. Runs C and D are found and
merged to make run G.
17 24 58 88
96 23 64
70 74 81 55
F
E
The algorithm then starts after the merged portion G. Only one run exists, run E, until the end of the array is reached. So the
algorithm starts back at index 0, finds runs F and G, and merges to make run H.
17 23 24 58
64 70 74 81 88
96
55
H
E
Again a single run is found after the just-merged part, so the search starts back at index 0. Runs H and E are found and
merged.
One last search for a sorted run occurs, finding a sorted run length equal to the array's length. So the array is sorted and the
algorithm terminates.
17 23 24 55 58 64 70 74 81 88 96
Step 1: Implement the getSorted RunLength() method
Implement the getSorted RunLength() method in Natural MergeSorter.java. Access Natural MergeSorter.java by clicking on
the orange arrow next to Natural Merge.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 getSorted RunLength() that can be run by clicking the "Run program"
button. One test case also exists for natural MergeSort(), 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 getSorted RunLength() unit tests pass before proceeding.
Step 2: Implement the natural MergeSort() method
Implement the natural MergeSort() method in Natural MergeSorter.java. naturalMergeSort() must:
1. Start at index i=0
2. 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
3. Get the length of the second sorted run, starting immediately after the first
4. Merge the two runs with the provided merge() method
5. Reassign i with the first index after the second run, or 0 if the second run ends at the array's end
6. Go to step 2
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by stepSolved in 2 steps

Knowledge Booster
Similar questions
- PRACTICE CODE import java.util.TimerTask;import org.firmata4j.ssd1306.MonochromeCanvas;import org.firmata4j.ssd1306.SSD1306;public class CountTask extends TimerTask {private int countValue = 10;private final SSD1306 theOledObject;public CountTask(SSD1306 aDisplayObject) {theOledObject = aDisplayObject;}@Overridepublic void run() {for (int j = 0; j <= 3; j++) {theOledObject.getCanvas().clear();theOledObject.getCanvas().setTextsize(1);theOledObject.getCanvas().drawString(0, 0, "Hello");theOledObject.display();try {Thread.sleep(2000);} catch (InterruptedException e) {throw new RuntimeException(e);}theOledObject.clear();theOledObject.getCanvas().setTextsize(1);theOledObject.getCanvas().drawString(0, 0, "My name is ");theOledObject.display();try {Thread.sleep(2000);} catch (InterruptedException e) {throw new RuntimeException(e);}while (true) {for (int i = 10; i >= 0; i--)…arrow_forwardImplement the following in the .NET Console App. Write the Bus class. A Bus has a length, a color, and a number of wheels. a. Implement data fields using auto-implemented Properies b. Include 3 constructors: default, the one that receives the Bus length, color and a number of wheels as input (utilize the Properties) and the Constructor that takes the Bus number (an integer) as input.arrow_forwardneed help finishing/fixing this java code so it prints out all the movie names that start with the first 2 characters the user inputs the code is as follows: Movie.java: import java.io.File;import java.io.FileNotFoundException;import java.util.ArrayList;import java.util.Scanner;public class Movie{public String name;public int year;public String genre;public static ArrayList<Movie> loadDatabase() throws FileNotFoundException {ArrayList<Movie> result=new ArrayList<>();File f=new File("db.txt");Scanner inputFile=new Scanner(f);while(inputFile.hasNext()){String name= inputFile.nextLine();int year=inputFile.nextInt();inputFile.nextLine();String genre= inputFile.nextLine();Movie m=new Movie(name, year, genre);//System.out.println(m);result.add(m);}return result;}public Movie(String name, int year, String genre){this.name=name;this.year=year;this.genre=genre;}public boolean equals(int year, String genre){return this.year==year&&this.genre.equals(genre);}public String…arrow_forward
- Modify this code to make a moving animated house. import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D; /**** @author**/public class MyHouse { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(750, 500); panel.setBackground (new Color(65,105,225)); Graphics g = panel.getGraphics(); background(g); house (g); houseRoof (g); lawnRoof (g); windows (g); windowsframes (g); chimney(g); } /** * * @param g */ static public void background(Graphics g) { g.setColor (new Color (225,225,225));//clouds g.fillOval (14,37,170,55); g.fillOval (21,21,160,50); g.fillOval (351,51,170,55); g.fillOval (356,36,160,50); } static public void house (Graphics g){g.setColor (new Color(139,69,19)); //houseg.fillRect (100,250,400,200);g.fillRect (499,320,200,130);g.setColor(new Color(190,190,190)); //chimney and doorsg.fillRect (160,150,60,90);g.fillRect…arrow_forwardIn Java: -Using a BST Interface (Binary Search Tree) -Create a class Disk. Disk contains four fields: model (String), size (int), measure (String, for GB or TB), and performance index (int). -Disk needs a constructor to initialize all fields, a toString method, and an equals. Make the class Comparable and write a compareTo method that compares Disks based on the size of the disk. Make sure your comparison accounts for the fact that some numbers are in GB and some are in TB. Write a method that will return true if the disk is the same size as the parms; parms are the size and measure("GB" or "TB"). Also write a method to create a Comparator which compares Disks based on the performance rating. -The client code should do the following: Write a function called build, which will open the disks.txt file, read in each disk and add it to the BST. For each disk the model is on the first line, and the second line contains the size, measure, and performance index. The BST is passed to build as…arrow_forwardWrite Java source code to take an integer n and returns the sum of all odd positive integers less than or equals to n. Please implement a sumOddo method for major functionality and a main method with input data for testing cases. Please pay attention to the neat coding style in Java, including grammar, indentations, naming of variables and methods, and comments. a) Conduct an experimental study regarding the running time of your Java code in nanoseconds. Please draw on a separate sheet of paper for the chart/figure on the evaluation results.arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education