Question

In: Computer Science

JAVA please write this method public static void recursiveMergeSort(int[] arr) { }

JAVA please write this method

public static void recursiveMergeSort(int[] arr) {

}

Solutions

Expert Solution

class MergeSort { 
  public static void merge(int[] result, int[] first, int[] second)      
   {
      int i, j, k;
      i = j = k = 0;
      //while till both completly copied
      while ( i < first.length || j < second.length )
      {
         if ( i < first.length && j < second.length )
         {  
             //compate element
            if ( first[i] < second[j] )
               result[k++] = first[i++];
            else
               result[k++] = second[j++];
         }
         else if ( i == first.length )
            result[k++] = second[j++];   //while first completed then 2nd copy 
         else if ( j == second.length )
            result[k++] = first[i++];     //while second completed then 1st copy 
      }
   }
  
    public static void recursiveMergeSort(int[] arr)
   {
      int[] Arrayleft, Arrayright;
 // if array consist 1 element
      if ( arr.length == 1 )
      {
         return;
      }
      int midindex = arr.length/2;//get mid index
      Arrayleft  = new int[midindex]; //array to hold left part
      for ( int i = 0; i < midindex; i++ )
         Arrayleft[i] = arr[i];//copy elements
      Arrayright = new int[arr.length-midindex];  //array to hold right part
      for ( int i = 0; i < arr.length-midindex; i++ )
         Arrayright[i] = arr[i+midindex];//copy elements
      recursiveMergeSort( Arrayleft );     //call recusively
      recursiveMergeSort( Arrayright );     //call recusively
      merge( arr, Arrayleft, Arrayright );  //Merge left and right array
   }
    static void DisplayArray(int array[]) 
    { 
        int size = array.length; 
        for (int i = 0; i < size; ++i) 
            System.out.print(array[i] + " "); 
        System.out.println(); 
    } 
  
    // Driver method 
    public static void main(String args[]) 
    { 
        int array[] = { 4,8,52,41,14,96,7 }; 
  
        System.out.println("Original Array"); 
        DisplayArray(array); 
        recursiveMergeSort(array); 
        System.out.println("Sorted Array"); 
        DisplayArray(array); 
    } 
} 

/*

Output

Original Array
4 8 52 41 14 96 7
Sorted Array
4 7 8 14 41 52 96

*/


Related Solutions

JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
Re-write this method using iteration ONLY JAVA class Main { public static void merge(int arr[], int...
Re-write this method using iteration ONLY JAVA class Main { public static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r-m; int left[] = new int[n1]; int right[] = new int[n2]; for (int i = 0; i < n1; ++i) left[i] = arr[l + i]; for (int j = 0; j < n2; ++j) right[j] = arr[m + 1 + j]; int i=0; //left int j=0; //...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
in java. using the following template as a method, public static void shellSort(int[] array) { create...
in java. using the following template as a method, public static void shellSort(int[] array) { create a program that counts the number of comparisons and swaps of the sorting method does using int array1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Best Case Test int array2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; // Worst Case Test int array3[] = {10, 4, 8, 2, 6, 1, 9, 3, 7, 5}; //...
   private static void merge(int arr[], int l, int m, int r) {        //...
   private 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...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
public static int example3 (int [] arr ) { int n = arr.length , total =...
public static int example3 (int [] arr ) { int n = arr.length , total = 0; for (int j = 0; j < n ; j += 2) for (int k = 0; k <= j ; k ++) total += arr [ j ]; return total ; } what is the running time of this method?
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT