Question

MERGE SORTING!!! JAVAAAA - Implement the non-recursive version of MergeSort. Also separately, implement the small-size cutoff...

MERGE SORTING!!! JAVAAAA

- Implement the non-recursive version of MergeSort. Also separately, implement the small-size cutoff optimization, with InsertionSort as the small-size sort. (So there should be a regular Merge Sort and an optimized Merge Sort).

- All implementations should be in the function.

public void mergeSorting (int [] data){

}

- Implement the main method with (at least) the following tests:

  • Create tests to make sure the merge works.
  • Test the sort with 10 elements to see if the result is sorted and that no elements were overwritten or "lost" in the sorting.
  • The same with 100 elements
  • Recall that String's implement the Comparable interface and that anything that implements that interface can be compared using the compareTo method (in terms of strings)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Regular Merge sort implementation(non-recursive):

import java.util.Arrays;
import java.util.Random;

class Main
{
   // Driver function to Merge two sorted sub-arrays
   public static void merge(int[] arr, int[] temp, int from, int mid, int to)
   {
       int k = from, i = from, j = mid + 1;

      
       while (i <= mid && j <= to) {
           if (arr[i] < arr[j]) {
               temp[k++] = arr[i++];
           } else {
               temp[k++] = arr[j++];
           }
       }

      
       while (i < arr.length && i <= mid) {
           temp[k++] = arr[i++];
       }

      
       for (i = from; i <= to; i++) {
           arr[i] = temp[i];
       }
   }

   // This function iteratively sort array A[low..high] using temporary array
   public static void mergeSorting(int[] arr)
   {
       int low = 0;
       int high = arr.length - 1;

       // sort array A[] using temporary array temp
       int[] temp = Arrays.copyOf(arr, arr.length);

      
       for (int m = 1; m <= high - low; m = 2*m)
       {
          
           for (int i = low; i < high; i += 2*m)
           {
               int from = i;
               int mid = i + m - 1;
               int to = Integer.min(i + 2 * m - 1, high);

               merge(arr, temp, from, mid, to);
           }
       }
   }

   // Main function for testing
   // Please change the size of array based on number of element you want
   public static void main(String[] args)
   {
   Random rd = new Random(); // creating Random object
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = rd.nextInt(1000);
}

       System.out.println("Original Array : " + Arrays.toString(arr));
       mergeSorting(arr);
       System.out.println("Sorted Array : " + Arrays.toString(arr));
   }
}

Optimized merge sort implementation:

import java.util.Arrays;
import java.util.Random;

public class Main {
  
private static final int Cutoff = 5;

private static int numberOfRows;
private static int row = 0;


// stably merge a[lo .. mid] with a[mid+1 .. hi] using aux[lo .. hi]
public static void merge(int[] a, int[] aux, int lo, int mid, int hi) {

  
for (int k = lo; k <= hi; k++) {
aux[k] = a[k];
}


int i = lo, j = mid+1;
for (int k = lo; k <= hi; k++) {
if (i > mid) a[k] = aux[j++];
else if (j > hi) a[k] = aux[i++];
else if (less(aux[j], aux[i])) a[k] = aux[j++];
else a[k] = aux[i++];
}
}

// mergesort a[lo..hi] using auxiliary array aux[lo..hi]
private static void sort(int[] a, int[] aux, int lo, int hi) {
int n = hi - lo + 1;
if (n <= Cutoff) {
insertionSort(a, lo, hi);

return;
}
if (hi <= lo) return;
int mid = lo + (hi - lo) / 2;
sort(a, aux, lo, mid);
sort(a, aux, mid + 1, hi);
merge(a, aux, lo, mid, hi);
  
}

public static void mergeSorting(int[] a) {
int[] aux = new int[a.length];
sort(a, aux, 0, a.length-1);
}

// sort from a[lo] to a[hi] using insertion sort
private static void insertionSort(int[] a, int lo, int hi) {
for (int i = lo; i <= hi; i++)
for (int j = i; j > lo && less(a[j], a[j-1]); j--)
exchange(a, j, j-1);
}

private static boolean less(int v, int w) {
return v < w;
}

private static void exchange(int[] a, int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}

// draw one row of trace
  
// Main function for testing
   // Please change the size of array based on number of element you want
public static void main(String[] args)
   {
   Random rd = new Random(); // creating Random object
int[] arr = new int[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = rd.nextInt(1000);
}

       System.out.println("Original Array : " + Arrays.toString(arr));
       mergeSorting(arr);
       System.out.println("Modified Array : " + Arrays.toString(arr));
   }
}

Note: for testing please change the length of the array in the main function according to the requirement.

Add a comment
Know the answer?
Add Answer to:
MERGE SORTING!!! JAVAAAA - Implement the non-recursive version of MergeSort. Also separately, implement the small-size cutoff...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • Hi i will give you a thumbs up if you do this problem correctly. Sorting Analysis Code and Essay ...

    Hi i will give you a thumbs up if you do this problem correctly. Sorting Analysis Code and Essay Due: 4/22/2019(Monday) Introduction And now for something completely different.   Different sorting algorithms are better for different size data sets.   Other sorting algorithms are better for data sets of a specific type – for instance, data that is already ordered. In this assignment you will implement four different sorting algorithms and collect statistics for each of those algorithms while sorting multiple different...

  • Program with generic merge sort and binary search method help. The programming language I'm using is...

    Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...

  • //Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded....

    //Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded. However, //for these algorithms to work correctly, the data objects must //be compared using the method compareTo and equals. //In other words, the classes implementing the list objects //must implement the interface Comparable. The type parameter T //is unbounded because we would like to use these algorithms to //work on an array of objects as well as on objects of the classes //UnorderedArrayList and...

  • A test harness program for testing sorting methods is provided with the rest of the textbook...

    A test harness program for testing sorting methods is provided with the rest of the textbook program files. It is the file Sorts.java in the ch11 package. The program includes a swap method that is used by all the sorting methods to swap array elements. Describe an approach to modifying the program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method. Implement your approach. Test your new program by...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...

  • SHORT ANSWER QUESTIONS Part 1 Classes Abstraction: What is Abstraction in terms of representation? Specifically what...

    SHORT ANSWER QUESTIONS Part 1 Classes Abstraction: What is Abstraction in terms of representation? Specifically what choices does one make when creating a class that is an example of Abstraction? Encapsulation: What is encapsulation? What is information hiding? What does a public interface to a class consist of (not in the sense of actual java interfaces but what the client uses to manipulate objects of the class) What is an object of a class? What is the constructor? How do...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT