Question

Write merge method for mergeSort method, it takes the array of data, starting index of first...

Write merge method for mergeSort method, it takes the array of data, starting index of first half, starting index of second half and end index of second half.

please use the code below to test it

public class A4Sort{
   public static void mergeSort(int[] a, int l, int h){
      if (l >= h) return;
      int mid = (h + l) / 2;
      mergeSort(a, l, mid); 
      mergeSort(a, mid + 1, h);
      merge(a, l, mid + 1, h); 
   }
   public static void main(String[] args){
      int[] a = {3,2,7,5,1,6,4,8,9};
      System.out.println("mergeSort a: ");
      long time, nextTime;
      time = System.nanoTime();
      mergeSort(a, 0, a.length - 1);
      nextTime = System.nanoTime();
      System.out.println("\tTime used: " + (nextTime - time) + " nseconds");
      for (int i = 0; i < a.length; i++) System.out.print(a[i] + ",");
      System.out.println();
   }

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

public class A4Sort{
public static void mergeSort(int[] a, int l, int h){
if (l >= h) return;
int mid = (h + l) / 2;
mergeSort(a, l, mid);
mergeSort(a, mid + 1, h);
merge(a, l, mid + 1, h);
}
public static void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
  
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
  
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
  
  
/* Merge the temp arrays */
  
// Initial indexes of first and second subarrays
int i = 0, j = 0;
  
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
  
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
  
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
public static void main(String[] args){
int[] a = {3,2,7,5,1,6,4,8,9};
System.out.println("mergeSort a: ");
long time, nextTime;
time = System.nanoTime();
mergeSort(a, 0, a.length - 1);
nextTime = System.nanoTime();
System.out.println("\tTime used: " + (nextTime - time) + " nseconds");
for (int i = 0; i < a.length; i++) System.out.print(a[i] + ",");
System.out.println();
}

}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Write merge method for mergeSort method, it takes the array of data, starting index of first...
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
  • 2. Write MinheapPriorityQueue constructor, which takes an array of data, and construct the max heap priority...

    2. Write MinheapPriorityQueue constructor, which takes an array of data, and construct the max heap priority queue using bottom-up algorithm. The expected run time should be O(n), where n is the total number of data. BubbleDown method is provided. You may test it in this minHeap public class MinHeapPriorityQueue<E extends Comparable<? super E>>{ private E data[]; private int size; public MinHeapPriorityQueue(){ this(100); } public MinHeapPriorityQueue(int cap){ size = 0; data = (E[]) new Comparable[cap]; } public MinHeapPriorityQueue(int[] a){ } public...

  • Create an ArrayListReview class with one generic type to do the following • Creates an array...

    Create an ArrayListReview class with one generic type to do the following • Creates an array list filled with the generic type of the ArrayListReview class, and inserts new elements into the specified location index-i in the list. (5 points) • Create a method inside the class to implement the calculation of Fibonacci numbers. Use System.nanoTime to find out how much time it takes to get fab(50). Create a method inside the class to check if an array list is...

  • 1) can any one please givem the code for this a) If n is a power...

    1) can any one please givem the code for this a) If n is a power of 2, as it is in Figure 9-3, you would merge pairs of individual entries, starting at the beginning of the array. Then you would return to the beginning of the array and merge pairs of twoentry subarrays. Finally, you would merge one pair of four-entry subarrays. Notice that the subarrays in each pair of subarrays contain the same number of entries. In general,...

  • Please merge all the codes below and add comments using JAVA Program. I need a complete...

    Please merge all the codes below and add comments using JAVA Program. I need a complete code which is the combination of the following codes: // Merges the left/right elements into a sorted result. // Precondition: left/right are sorted public static void merge(int[] result, int[] left,                                        int[] right) {     int i1 = 0;   // index into left array     int i2 = 0;   // index into right array     for (int i = 0; i < result.length; i++)...

  • COMPLETE BigMerge public class BigMerge {       /*    * Returns j such that a[j][index[j]]...

    COMPLETE BigMerge public class BigMerge {       /*    * Returns j such that a[j][index[j]] is the minimum    * of the set S={a[i][index[i]] for every i such that index[i]<a[i].length}    * If the set S is empty, returns -1    * Runs in time a.length.    *    * NOTE: normally this would be private, but leave it    * public so we can test it separately from your merge.    */    public static int argMin(int [][]...

  • Hi All, Can someone please help me correct the selection sort method in my program. the...

    Hi All, Can someone please help me correct the selection sort method in my program. the output for the first and last sorted integers are wrong compared with the bubble and merge sort. and for the radix i have no idea. See program below import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Sort { static ArrayList<Integer> Data1 = new ArrayList<Integer>(); static ArrayList<Integer> Data2 = new ArrayList<Integer>(); static ArrayList<Integer> Data3 = new ArrayList<Integer>(); static ArrayList<Integer> Data4 = new ArrayList<Integer>(); static int...

  • USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list...

    USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list and use a Merge Sort to sort the object by age. Create a class called Person : name and age Create methods that add, and delete Person from the link list Create a method that sorts the persons' objects by age. package mergesort; public class MergeSortExample { private static Comparable[] aux; // auxiliary array for merges public static void sort(Comparable[] a) { aux =...

  • use the same code. but the code needs some modifications. so use this same code and...

    use the same code. but the code needs some modifications. so use this same code and modify it and provide a output Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...

  • Hey i got the program to work but i cant get the merge sorter from big...

    Hey i got the program to work but i cant get the merge sorter from big to little import java.util.*; class MergeSorter {    public static void sort(int[] a)    { if (a.length <= 1) { return; } int[] first = new int[a.length / 2]; int[] second = new int[a.length - first.length]; for (int i = 0; i < first.length; i++) {    first[i] = a[i]; } for (int i = 0; i < second.length; i++) {    second[i] =...

  • 1.) Complete the merge method below, which is the merge step of MergeSort. That is, it...

    1.) Complete the merge method below, which is the merge step of MergeSort. That is, it takes two sorted arrays as input and returns a new sorted array that contains all the elements from the two input arrays. Furthermore, it should keep all duplicated values, and it should run in O(n) time, where n is the size of the output array. public static int[] main(int[] a, int[] b) { }

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
ADVERTISEMENT