Question

In: Computer Science

1. Implement a method that meets the following requirements: (a) Do not reuse any code for...

1. Implement a method that meets the following requirements:

(a) Do not reuse any code for the following:

i. Try to write this method with as few lines of code as you can

ii. Sorts a group of three integers, x,y and z, into decreasing order (they do not have to be in a sequence).

iii. Assume the value in x is less than the value in z. You can also assume there are no duplicates among x, y and z (none of them contain the same value)

iv. Prints a message each time the order of two elements are changed.

v. Prints the list before and after sorting

(b) You can reuse Java API code and module code for the following:

i. Calls mergesort to sort the sequence of numbers into increasing order

ii. Prints the list after the second sorting.

(c) Implement a main method that calls the above methods, demonstrating their use with all applicable printed output

Solutions

Expert Solution


import java.util.*;
public class MergeSort
{
   // Function for 3-way merge sort process
   public static void mergeSort(Integer[] z)
   {
       // if array of size is zero returns null
       if (z== null)
           return;

       // creating duplicate of given array
       Integer[] x = new Integer[z.length];

       // copying alements of given array into
       // duplicate array
       for (int i = 0; i < x.length; i++)
           x[i] = z[i];

       // sort function
       mergeSort(x, 0, z.length, z);

       // copy back elements of duplicate array
       // to given array
       for (int i = 0; i < x.length; i++)
           z[i] = x[i];
   }

   /* Performing the merge sort algorithm on the
   given array of values in the rangeof indices
   [low, high). low is minimum index, high is
   maximum index (exclusive) */
   public static void mergeSort(Integer[] z,
               int low, int high, Integer[] y)
   {
       // If array size is 1 then do nothing
       if (high - low < 2)
           return;

       // Splitting array into 3 parts
       int mid1 = low + ((high - low) / 3);
       int mid2 = low + 2 * ((high - low) / 3) + 1;

       // Sorting 3 arrays recursively
       mergeSort(y, low, mid1, z);
       mergeSort(y, mid1, mid2, z);
       mergeSort(y, mid2, high, z);

       // Merging the sorted arrays
       merge(y, low, mid1, mid2, high, z);
   }

   /* Merge the sorted ranges [low, mid1), [mid1,
   mid2) and [mid2, high) mid1 is first midpoint
   index in overall range to merge mid2 is second
   midpoint index in overall range to merge*/
   public static void merge(Integer[] z, int low,
                       int mid1, int mid2, int high,
                               Integer[] y)
   {
       int i = low, j = mid1, k = mid2, l = low;

       // choose smaller of the smallest in the three ranges
       while ((i < mid1) && (j < mid2) && (k < high))
       {
           if (z[i].compareTo(z[j]) < 0)
           {
               if (z[i].compareTo(z[k]) < 0)
                   y[l++] = z[i++];

               else
                   y[l++] = z[k++];
           }
           else
           {
               if (z[j].compareTo(z[k]) < 0)
                   y[l++] = z[j++];
               else
                   y[l++] = z[k++];
           }
       }

       // case where first and second ranges have
       // remaining values
       while ((i < mid1) && (j < mid2))
       {
           if (z[i].compareTo(z[j]) < 0)
               y[l++] = z[i++];
           else
               y[l++] = z[j++];
       }

       // case where second and third ranges have
       // remaining values
       while ((j < mid2) && (k < high))
       {
           if (z[j].compareTo(z[k]) < 0)
               y[l++] = z[j++];

           else
               y[l++] = z[k++];
       }

       // case where first and third ranges have
       // remaining values
       while ((i < mid1) && (k < high))
       {
           if (z[i].compareTo(z[k]) < 0)
               y[l++] = z[i++];
           else
               y[l++] = z[k++];
       }

       // copy remaining values from the first range
       while (i < mid1)
           y[l++] = z[i++];

       // copy remaining values from the second range
       while (j < mid2)
           y[l++] = z[j++];

       // copy remaining values from the third range
       while (k < high)
           y[l++] = z[k++];
   }

   // Driver function
   public static void main(String args[])
   {
       // test case of values
       Integer[] data = new Integer[] {45, -2, -45, 78,
                           30, -42, 10, 19, 73, 93};
      
      
       System.out.println("data before sorting decreasing order ");
       for(int i=0;i<data.length;i++)
       System.out.print(data[i]+ " ");
       mergeSort(data);

       System.out.println("\nAfter merge sort: \n");
       for (int i = 0; i < data.length; i++)
           System.out.print(data[i] + " ");
   }
}
output

sorting before sorting decreasing order
45 -2 -45 78 30 -42 10 19 73 93
After merge sort:

-45 -42 -2 10 19 30 45 73 78 93


Related Solutions

Implement a method that meets the following requirements: (a) Has the same requirements as the above...
Implement a method that meets the following requirements: (a) Has the same requirements as the above method, but works with an int array that is sorted in increasing order. Attempt your best complexity (b) In comments above the method, explain what its algorithmic complexity is and why (constant, logarithmic, linear, quadratic...)
Implement a method that meets the following requirements: (a) Takes as parameter 1) an int array...
Implement a method that meets the following requirements: (a) Takes as parameter 1) an int array A 2) an int number x to search for (b) determines whether x exists in A, and prints a message indicating the result (c) has the best worst case Big Oh complexity you can manage, using only your own thinking and the materials (the worst case growth rate for the number of items searched should be as low as possible, given that A contains...
. Implement a method that meets the following requirements: Computer Language:Java (a) Try to write this...
. Implement a method that meets the following requirements: Computer Language:Java (a) Try to write this method with as few lines of code as you can (b) Sorts a group of three integers, x,y and z, into increasing order (they do not have to be in a sequence). (c) Assume the value in x is less than the value in z. You can also assume there are no duplicates among x, y and z (none of them contain the same...
Java . Implement a method that meets the following requirements: (a) Calls mergesort to sort an...
Java . Implement a method that meets the following requirements: (a) Calls mergesort to sort an array/list of at least 5 integers (b) Prints the list before and after sorting.
. Implement a method that meets the following requirements: (a) Calls mergesort to sort an array/list...
. Implement a method that meets the following requirements: (a) Calls mergesort to sort an array/list of at least 5 integers (b) Prints the list before and after sorting.
4 Implement a Java program that meets the following requirements • You can use the Java...
4 Implement a Java program that meets the following requirements • You can use the Java standard sequence data structure API types for sets, lists, stack,queue and priority queue as needed. All are available in the java.util package, which you will want to import in your program. 1. Argue in code comments which data structure, stack or queue, you will use to implement this method. Implement a method which creates some String objects as food orders for a small restaurant,...
Re-do Problem 1, but now implement the following method: i must use this code :- public...
Re-do Problem 1, but now implement the following method: i must use this code :- public static BagInterface intersection (BagInterface bagA, BagInterface bagB) and here how my java program must work This method must return a new bag which is the intersection of the two bags: bagA and bagB. An element appears in the intersection of two bags the minimum of the number of times it appears in either. For example, {1,1,2} ∩{1,1,2,2,3}= {1,1,2}. Do not forget to state the...
1. Write a Java program from scratch that meets the following requirements: a. The program is...
1. Write a Java program from scratch that meets the following requirements: a. The program is in a file called Duplicates.java that defines a class called Duplicates (upper/lower case matters) b. The program includes a Java method called noDuplicates that takes an array of integers and returns true if all the integers in that array are distinct (i.e., no duplicates). Otherwise it returns false. Make sure the method is public static. example tests: noDuplicates({}) returns true noDuplicates({-1, 1}) returns true...
You must implement the delete method. Below are the requirements: • The delete method takes a...
You must implement the delete method. Below are the requirements: • The delete method takes a Price as an argument and removes the Price from the queue if it is present. (If the Price was not present, the method does nothing). • The method returns true if the Price was deleted and false otherwise. • The method must run in logarithmic time. This is a key requirement. Solutions that are linear or worse will not receive credit. (You may assume...
Write a C program that meets the following requirements. Uses a while loop to display the...
Write a C program that meets the following requirements. Uses a while loop to display the first 10 natural numbers (on one row, with a tab separating each number) Uses a while loop to find the sum of the second set of 10 natural numbers. Reads a user entry and displays all the natural numbers up to the user entry (on a column list with a new line separating each number). Finds and displays the sum of all natural numbers...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT