Question

. Write a class with an int array as its only instance variable. Write a recursive...

. Write a class with an int array as its only instance variable. Write a recursive method that uses the following recursive strategy in order to sort the array:

  • ❑ Sort the left half of the array (this is a recursive call).

  • ❑ Sort the right half of the array (this is another recursive call).

  • ❑ Merge the two sorted halves of the array so that the array is sorted (there is no recursive call here).

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

package sort;
/*worst case of merge sort is NlogN
*    Merge sort
* {20, 13, 4, 34, 5, 15, 90, 100, 75, 102}
* / \   
* {20, 13, 4, 34, 5} {15, 90, 100, 75, 102}
* / \ / \
* {20, 13, 4}{34, 5} {15, 90, 100,} {75, 102}
* / \ / \ / \ / \
*{20, 13} {4} {34} {5} {15, 90} {100} {75} {102}
* / \ / \
*{20} {13} {15} {90}
*
*Height of above tree is Log(N)
*to merge the array two temp array it will take o(N) in worst case
*so total time complexity it O(Nlog(N))
*
*/

public class MergeSort {

   int ar[];
   MergeSort(int ar[]){
       this.ar=ar;
   }
   public void mergeSort(int ar[],int l,int r){
       if(l<r){
           int mid=(l+r)/2; //take mid index
           mergeSort(ar,l,mid); //call mergesort on first half
           mergeSort(ar,mid+1,r); // and second half
           marge(ar,l,r,mid); //no merge the two in sorted manner
       }
   }
   public void marge(int ar[],int l,int r,int mid){
       int t1[]=new int[mid-l+1]; //make temp array of first hlaf size and
       int t2[]=new int[r-mid]; //second half size
       int k=0; //index to 0
       for(int i=l;i<=mid;i++){ //copy fist half elements to first temp array
           t1[k++]=ar[i];
       }
       k=0;
       for(int i=mid+1;i<=r;i++){ //copy second half array to sec temp array
           t2[k++]=ar[i];
       }
       k=0;
       int k1=0; //from those two temp array copy elements in sorted array in original array
       while(k<t1.length && k1<t2.length){
           if(t1[k]<=t2[k1]){ //if t1's element is less first copy it
               ar[l++]=t1[k++];  
           }
           else{
               ar[l++]=t2[k1++]; //else t2's
           }
       }
       while(k<t1.length){ //copy the remaining elements
           ar[l++]=t1[k++];
       }
       while(k1<t2.length){
           ar[l++]=t2[k1++];
       }
   }
   //driver program to test
   public static void main(String[] args) {
       int A[]={20, 13, 4, 34, 5, 15, 90, 100, 75, 102};
       MergeSort m=new MergeSort(A);
       m.mergeSort(A,0,A.length-1);
       for(int i=0;i<A.length;i++){
           System.out.print(A[i]+" ");  
           }
      
   }

}

output

4 5 13 15 20 34 75 90 100 102

Add a comment
Know the answer?
Add Answer to:
. Write a class with an int array as its only instance variable. Write a recursive...
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
  • 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++)...

  • HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge...

    HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge sort (on arrays). Create a public non-final class named Mergesort that extends Merge. Implement a public static method int[] mergesort(int[] values) that returns the input array of ints sorted in ascending order. You will want this method to be recursive, with the base case being an array with zero or one value. If the passed array is null you should return null. If the...

  • 4. (15 points) Recall that in merge sort, we partitioned the array into two halves then...

    4. (15 points) Recall that in merge sort, we partitioned the array into two halves then recursively apply merge sort on those two halves. Suppose instead we partitioned the array into three parts. Write a threewayMergeSort(int list) that returns sorted version of list via apply merge sort on three partitions instead of two // ThreeWayMergeSort.java public class ThreeWayMergeSort f public int threewayMergeSort (int [] list) f (a) (10 points) Fill threewayMergeSort methood (b) (2 points) Write the runtime of threeway...

  • 2. In class, we discussed the recursive Merge-Sort algorithm. This sorts the whole array by sorting...

    2. In class, we discussed the recursive Merge-Sort algorithm. This sorts the whole array by sorting the left side, sorting the right side, and then merging them. Write a similar recursive algorithm that finds the maximum element of an array. (Find the max of the left side, then find the maximum of the right side, then compare the two.) Write pseudo-code for this algorithm. Give the recurrence relation that describes the number of comparisons that your algorithm uses.

  • Write a java program: Create a method fillRandom() that accepts an array of int as input...

    Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...

  • 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,...

  • Consider the following recursive method for finding the maximum element in an int array: public int...

    Consider the following recursive method for finding the maximum element in an int array: public int static max(int[] a, int lo, int hi) { if (lo > hi) return a[lo]; int mid = (lo + hi) / 2; int loMax = max(a, lo, mid); int hiMax = max(a, mid+1, hi); if (loMax > hiMax) return loMax; else return hiMax; } Write down the recurrence relation for counting the number of times the comparison if (loMax > hiMax) is performed. Use...

  • Consider the following mergeSortHelper method, which is part of an algorithm to recursively sort an array...

    Consider the following mergeSortHelper method, which is part of an algorithm to recursively sort an array of integers. /**  Precondition: (arr.length == 0 or 0 <= from <= to <= arr.length) * arr.length == temp.length */ public static void mergeSortHelper(int[] arr, int from, int to, int[] temp) { if (from < to) { int middle = (from + to) / 2; mergeSortHelper(arr, from, middle, temp); mergeSortHelper(arr, middle + 1, to, temp); merge(arr, from, middle, to, temp); } } The merge method...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

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