Question

In: Computer Science

Write a generic method mergeSort based on the sort program of Fig. 19.6 (the source code...

Write a generic method mergeSort based on the sort program of Fig. 19.6 (the source code is given as a separate file along with this final document, and also appended at the end of this document). Test your program that prints before sorting, sorts, and prints after sorting an Integer array, a Double array, and a String array as follows:

      Integer[] dataInt = {63, 19, 65, 38, 26, 74, 27, 25, 70, 38};

      Double[] dataDouble = {102.5, 1.98, -3.12, 45.23, 4.5, -56.78, 38.24, 12.34, -105.23, 77.39};

     String[] dataString = {"zebra", "wolf", "panda", "octopus", "monkey", "fox", "elephant", "dog", "cat", "allegator"};

// Fig. 19.6: 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     

         // output split step
         System.out.printf("split:   %s%n", 
            subarrayString(data, low, high));
         System.out.printf("         %s%n", 
            subarrayString(data, low, middle1));
         System.out.printf("         %s%n%n",
            subarrayString(data, middle2, high));

         // 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        
      
      // output two subarrays before merging
      System.out.printf("merge:   %s%n", 
         subarrayString(data, left, middle1));
      System.out.printf("         %s%n", 
         subarrayString(data, middle2, right));

      // 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
      System.out.printf("         %s%n%n", 
         subarrayString(data, left, right));
   } 

   // method to output certain values in array
   private static String subarrayString(int[] data, int low, int high) {
      StringBuilder temporary = new StringBuilder();

      // output spaces for alignment
      for (int i = 0; i < low; i++) {
         temporary.append("   ");
      } 

      // output elements left in array
      for (int i = low; i <= high; i++) {
         temporary.append(" " + data[i]);
      } 

      return temporary.toString();
   }

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

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

MergeSortTest.java

package c15;
import java.lang.reflect.Array;
import java.util.Arrays;

public class MergeSortTest<T> {
   // calls recursive sortArray method to begin merge sorting
   public static<T extends Comparable<? super T>> void mergeSort(T[] data,Class<T> clazz) {
       sortArray(data, 0, data.length - 1,clazz); // sort entire array
   }

   // splits array, sorts subarrays and merges subarrays into sorted array
   private static<T extends Comparable<? super T>> void sortArray(T[] data, int low, int high,Class<T> clazz) {
       // 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   

           // output split step
           System.out.printf("split: %s%n",
                   subarrayString(data, low, high,clazz));
           System.out.printf(" %s%n",
                   subarrayString(data, low, middle1,clazz));
           System.out.printf(" %s%n%n",
                   subarrayString(data, middle2, high,clazz));

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

           // merge two sorted arrays after split calls return
           merge (data, low, middle1, middle2, high,clazz);   
       }
   }   

   // merge two sorted subarrays into one sorted subarray   
   private static<T extends Comparable<? super T>> void merge(T[] data, int left, int middle1,
           int middle2, int right,Class<T> clazz) {

       int leftIndex = left; // index into left subarray
       int rightIndex = middle2; // index into right subarray   
       int combinedIndex = left; // index into temporary working array
       T[] combined = (T[]) Array.newInstance(clazz, data.length);
         
       // output two subarrays before merging
       System.out.printf("merge: %s%n",
               subarrayString(data, left, middle1,clazz));
       System.out.printf(" %s%n",
               subarrayString(data, middle2, right,clazz));

       // 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].compareTo(data[rightIndex])<=0) {   
               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
       System.out.printf(" %s%n%n",
               subarrayString(data, left, right,clazz));
   }

   // method to output certain values in array
   private static<T> String subarrayString(T[] data, int low, int high,Class<T> clazz) {
       StringBuilder temporary = new StringBuilder();

       // output spaces for alignment
       for (int i = 0; i < low; i++) {
           temporary.append(" ");
       }

       // output elements left in array
       for (int i = low; i <= high; i++) {
           temporary.append(" " + data[i]);
       }

       return temporary.toString();
   }

   public static void main(String[] args) {
       Integer[] dataInt = {63, 19, 65, 38, 26, 74, 27, 25, 70, 38};
       Double[] dataDouble = {102.5, 1.98, -3.12, 45.23, 4.5, -56.78, 38.24, 12.34, -105.23, 77.39};
       String[] dataString = {"zebra", "wolf", "panda", "octopus", "monkey", "fox", "elephant", "dog", "cat", "allegator"};
      
       System.out.printf("Unsorted array: %s%n%n", Arrays.toString(dataInt));
       mergeSort(dataInt,Integer.class); // sort array
       System.out.printf("Sorted array: %s%n", Arrays.toString(dataInt));
      
       System.out.printf("Unsorted array: %s%n%n", Arrays.toString(dataDouble));
       MergeSortTest.<Double>mergeSort(dataDouble,Double.class); // sort array
       System.out.printf("Sorted array: %s%n", Arrays.toString(dataDouble));
      
      
       System.out.printf("Unsorted array: %s%n%n", Arrays.toString(dataString));
       MergeSortTest.<String>mergeSort(dataString,String.class); // sort array
       System.out.printf("Sorted array: %s%n", Arrays.toString(dataString));
      
      
      
   }
}


Related Solutions

write a java code Write a generic program to compute the sum of 30 terms of...
write a java code Write a generic program to compute the sum of 30 terms of the following series. (181 - 5)/31 + (186 + 9)/34 - (191 - 13)/37 + (196 + 17)/40 + . . . . . 1 5 11 Note: the 3rd, 6th, 9th term etc, has a negative sign in front of parenthesis. User Input: None Expected output: Term Ratio Sum 1 5.677 5.677 2 5.735 11.413 3 -4.811 6.602
Please write a java code. Write a generic program for New Home Construction Pricing with the...
Please write a java code. Write a generic program for New Home Construction Pricing with the following specifications. Note, each one of the 2, 3 or 4 bedroom homes are priced with standard type bathrooms. Update to deluxe or premium choice of bathrooms can be ordered by paying the difference in prices.    Types of homes Price 2 bedroom, 2 bathroom (standard type) and 1 car garage home = $350,000 3 bedroom, 2 bathroom (standard type) and 2 car garage...
Write a program that solves the Knapsack problem. Code to the following standards. Your source of...
Write a program that solves the Knapsack problem. Code to the following standards. Your source of items to put into the knapsack should consist of five randomly generated integers in the range of 1 to 20. Your knapsack can hold 20 lbs.
Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on...
Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on input two sets represented as hash sets, returns their Jaccard similarity. The following are a few sample runs: Input :    A=1, 2, 3, 4,   B=2, 4, 6, 8 Return:   0.3333333333333333 Input :    A=Larry, Michael, Shaq, Kobe, LeBron                    B=Steve, Kobe, Shaq, LeBron, Steph, Jeremy, Michael Return:   0.5 Your method must have time complexity On and space complexity O1, where n is the size of...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in...
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in zyBooks sections 9.4 and 9.5. Prompt the user for the location of a sequence of numbers, via an external file or data entry by the user. If you choose data entry, prompt the user for the number of values and read them into a data structure of your choice. Then use the mergesort algorithm to sort them in ascending order. Finally, prompt for a...
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
Write an x86 assembly language program that performs equivalently to the C++ source code file shown...
Write an x86 assembly language program that performs equivalently to the C++ source code file shown below.Please note that commented out behavior must be implemented in x86 assembly language. There is no standard, portable way to perform some of these actions in C++. #include void main() { // Use registers for these in your x86 assembly language program // Only use the .data segment for string (character array) variables int eax; int esi; int ecx; int edi; // Loop the...
Write a C++ program that attempts to make the Radix Sort more practical: make it sort...
Write a C++ program that attempts to make the Radix Sort more practical: make it sort strings of a maximum length of 15. Have an array of strings. Then in the Radix Sort, create an array of LinkedQueue with 95 queues (95 are the printable characters starting with space). Those queues will be used to separate the data then combine them (i.e. bins). Randomly generate 10,000 strings with lengths from 1 to 15 (during the sort and with strings less...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT