// MergeSortTest.java // Sorting an array with merge sort. import java.security.SecureRandom; import java.util.Arrays; public class MergeSortTest {    // calls recursive sortArray method to begin merge sorting    public static void mergeSort(int[] data) {       sortArray(data, 0, data.length - 1); // sort entire array    }                                      // splits array, sorts subarrays and merges subarrays into sorted array    private static void sortArray(int[] data, int low, int high) {       // test base case; size of array equals 1            if ((high - low) >= 1) { // if not base case          int middle1 = (low + high) / 2; // calculate middle of array          int middle2 = middle1 + 1; // calculate next element over               // split array in half; sort each half (recursive calls)          sortArray(data, low, middle1); // first half of array                 sortArray(data, middle2, high); // second half of array               // merge two sorted arrays after split calls return          merge (data, low, middle1, middle2, high);                    }                                                }                                       // merge two sorted subarrays into one sorted subarray                 private static void merge(int[] data, int left, int middle1,        int middle2, int right) {       int leftIndex = left; // index into left subarray                     int rightIndex = middle2; // index into right subarray                int combinedIndex = left; // index into temporary working array       int[] combined = new int[data.length]; // working array                      // merge arrays until reaching end of either                while (leftIndex <= middle1 && rightIndex <= right) {          // place smaller of two current elements into result            // and move to next space in arrays                             if (data[leftIndex] <= data[rightIndex]) {                    combined[combinedIndex++] = data[leftIndex++];           }           else {                                                              combined[combinedIndex++] = data[rightIndex++];          }        }            // if left array is empty                                       if (leftIndex == middle2) {                                       // copy in rest of right array                                  while (rightIndex <= right) {                                     combined[combinedIndex++] = data[rightIndex++];          }        }        else { // right array is empty                                       // copy in rest of left array                                   while (leftIndex <= middle1) {                                     combined[combinedIndex++] = data[leftIndex++];           }        }        // copy values back into original array       for (int i = left; i <= right; i++) {           data[i] = combined[i];                 }        // output merged array                   }     // method to output certain values in array    public static void main(String[] args) {       SecureRandom generator = new SecureRandom();       // create unordered array of 10 random ints       int[] data = generator.ints(10, 10, 91).toArray();        System.out.printf("Unsorted array: %s%n%n", Arrays.toString(data));       mergeSort(data); // sort array       System.out.printf("Sorted array: %s%n", Arrays.toString(data));    }  }  //we get 2000 - 2001 = -1... A Negative number, that means m1 < m2. //If we get a positive number then m

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

// MergeSortTest.java
// Sorting an array with merge sort.
import java.security.SecureRandom;
import java.util.Arrays;

public class MergeSortTest {
   // calls recursive sortArray method to begin merge sorting
   public static void mergeSort(int[] data) {
      sortArray(data, 0, data.length - 1); // sort entire array
   }                                  

   // splits array, sorts subarrays and merges subarrays into sorted array
   private static void sortArray(int[] data, int low, int high) {
      // test base case; size of array equals 1     
      if ((high - low) >= 1) { // if not base case
         int middle1 = (low + high) / 2; // calculate middle of array
         int middle2 = middle1 + 1; // calculate next element over     


         // split array in half; sort each half (recursive calls)
         sortArray(data, low, middle1); // first half of array       
         sortArray(data, middle2, high); // second half of array     

         // merge two sorted arrays after split calls return
         merge (data, low, middle1, middle2, high);             
      }                                            
   }                               
   
   // merge two sorted subarrays into one sorted subarray             
   private static void merge(int[] data, int left, int middle1, 
      int middle2, int right) {

      int leftIndex = left; // index into left subarray              
      int rightIndex = middle2; // index into right subarray         
      int combinedIndex = left; // index into temporary working array
      int[] combined = new int[data.length]; // working array        
      


      // merge arrays until reaching end of either         
      while (leftIndex <= middle1 && rightIndex <= right) {
         // place smaller of two current elements into result  
         // and move to next space in arrays                   
         if (data[leftIndex] <= data[rightIndex]) {       
            combined[combinedIndex++] = data[leftIndex++]; 
         } 
         else {                                                 
            combined[combinedIndex++] = data[rightIndex++];
         } 
      } 
   
      // if left array is empty                                
      if (leftIndex == middle2) {                             
         // copy in rest of right array                        
         while (rightIndex <= right) {                        
            combined[combinedIndex++] = data[rightIndex++];
         } 
      } 
      else { // right array is empty                             
         // copy in rest of left array                         
         while (leftIndex <= middle1) {                        
            combined[combinedIndex++] = data[leftIndex++]; 
         } 
      } 

      // copy values back into original array
      for (int i = left; i <= right; i++) { 
         data[i] = combined[i];          
      } 

      // output merged array
     
        
   } 

   // method to output certain values in array


   public static void main(String[] args) {
      SecureRandom generator = new SecureRandom();

      // create unordered array of 10 random ints
      int[] data = generator.ints(10, 10, 91).toArray(); 

      System.out.printf("Unsorted array: %s%n%n", Arrays.toString(data));
      mergeSort(data); // sort array
      System.out.printf("Sorted array: %s%n", Arrays.toString(data));
   } 

//we get 2000 - 2001 = -1... A Negative number, that means m1 < m2.
//If we get a positive number then m

Create a java Project that prints unsorted and sorted sample array of
at least 5 sample Comparable Student data and verify output that the
merge sort work correctly.
The project should contain two java files:
1.Create "Student.java" that implements Comparable<Student>
interface, and necessary members such as "FirstName", "LastName"
and "StudentID" of data type "String" respectively, necessary methods
such as compareTo(), and toString(), and if needed, "getters" and
"setters" methods.
2. Driver class MergeSortTest.java (Provided within question) that
contains the main() method, the merge sort implementation ,and
demonstration of the merge sort algorithm using appropriate mock up
sample comparable Student array data.
You can choose to merge sort your sample Student array based on
"LastName" and or "FirstName, or StudentID|
Transcribed Image Text:Create a java Project that prints unsorted and sorted sample array of at least 5 sample Comparable Student data and verify output that the merge sort work correctly. The project should contain two java files: 1.Create "Student.java" that implements Comparable<Student> interface, and necessary members such as "FirstName", "LastName" and "StudentID" of data type "String" respectively, necessary methods such as compareTo(), and toString(), and if needed, "getters" and "setters" methods. 2. Driver class MergeSortTest.java (Provided within question) that contains the main() method, the merge sort implementation ,and demonstration of the merge sort algorithm using appropriate mock up sample comparable Student array data. You can choose to merge sort your sample Student array based on "LastName" and or "FirstName, or StudentID|
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Quicksort
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
Database System Concepts
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)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education