Question

In: Computer Science

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; // right

int k=l; // main

while( i<n1 && j<n2){

if(left[i]<=right[j]){

arr[k] = left[i];

i++;

}

else{

arr[k] = right[j];

j++;

}

k++;

}

while(i<n1){

arr[k] = left[i];

i++;

k++;

}

while(j<n2){

arr[k] = right[j];

j++;

k++;

}


}

static void sort(int arr[], int l, int r) {

if(l>=r){

return;

}

if(l<r){

int m = (l+r)/2;

//call the left part divide

sort(arr, l, m);

//call the right to divide

sort(arr, m+1, r);

//merge left and right

merge(arr, l,m,r);

}

}

static void printArray(int arr[])

{

int n = arr.length;

for (int i = 0; i < n; ++i)

System.out.print(arr[i] + " ");

System.out.println();

}

// Driver method

public static void main(String args[])

{ // 2 3.

int arr[] = { 5,7,6,3 , 2,4,1 };

//main array = 1,2,3,4,5, 6, 7

// left 1,3,6,7

// right 2,4,5

// arr = 1,3,6,7,2,4,5

System.out.println("Given Array");

printArray(arr);

sort(arr, 0, arr.length - 1);

System.out.println("\nSorted array");

printArray(arr);

}

}

Solutions

Expert Solution

// below is java code , modified over your code

class Main {

   static void sort(int arr[],int low , int n)
   {
       int csize,lf;
      
       for (csize = 1; csize <= n; csize = 2*csize)
       {
       for (lf = 0; lf < n;lf =lf+ 2*csize)
           {
               int mid = Math.min(lf + csize - 1, n);
      
               int re = Math.min(lf + 2*csize - 1, n);
               merge(arr, lf, mid, re);
           }
       }
   }
  
  
   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; // right

       int k=l; // main

       while( i<n1 && j<n2){

       if(left[i]<=right[j]){

       arr[k] = left[i];

       i++;

       }

       else{

       arr[k] = right[j];

       j++;

       }

       k++;

       }

       while(i<n1){

       arr[k] = left[i];

       i++;

       k++;

       }

       while(j<n2){

       arr[k] = right[j];

       j++;

       k++;

       }


   }
  

   static void printArray(int arr[])

   {

   int n = arr.length;

   for (int i = 0; i < n; ++i)

   System.out.print(arr[i] + " ");

   System.out.println();

   }
  
  
   public static void main(String args[])

   { // 2 3.

   int arr[] = { 5,7,6,3 , 2,4,1 };

   //main array = 1,2,3,4,5, 6, 7

   // left 1,3,6,7

   // right 2,4,5

   // arr = 1,3,6,7,2,4,5

   System.out.println("Given Array");

   printArray(arr);

   sort(arr, 0, arr.length -1);

   System.out.println("\nSorted array");

   printArray(arr);

   }
}

//output


Related Solutions

JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
   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...
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...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
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)
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
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}; //...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT