Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

At every recursive step, print out to the console what the two halves are that a

ID: 3833921 • Letter: A

Question

At every recursive step, print out to the console what the two halves are that are going to be merged together.

public class Mergesort
{
private int[] numbers;
private int[] helper;

private int number;

public void sort(int[] values)
{
this.numbers = values;
number = values.length;
this.helper = new int[number];

System.out.println("START");

mergesort(0, number - 1);

System.out.println("END");
}

private void mergesort(int low, int high)
{
// Check if low is smaller then high, if not then the array is sorted
if (low < high)
{
// Get the index of the element which is in the middle
int middle = (low + high) / 2;
// Sort the left side of the array
mergesort(low, middle);
// Sort the right side of the array
mergesort(middle + 1, high);
// Combine them both
merge(low, middle, high);
}
}

private void merge(int low, int middle, int high)
{

// Copy both parts into the helper array
for (int i = low; i <= high; i++)
{
helper[i] = numbers[i];
}

int i = low;
int j = middle + 1;
int k = low;

// Copy the smallest values from either the left or the right side back
// to the original array
while (i <= middle && j <= high)
{
if (helper[i] <= helper[j])
{
numbers[k] = helper[i];
i++;
}
else
{
numbers[k] = helper[j];
j++;
}
k++;
}

// Copy the rest of the left side of the array into the target array
while (i <= middle)
{
numbers[k] = helper[i];
k++;
i++;
}

}

private void printArray()
{
for(int x : numbers)
System.out.print(x + " ");

System.out.println(" ");
}
}

Explanation / Answer

public class Mergesort
{
    private int[] numbers;
    private int[] helper;

    private int number;

    public void sort(int[] values)
    {
        this.numbers = values;
        number = values.length;
        this.helper = new int[number];

        System.out.println("START");

        mergesort(0, number - 1);

        System.out.println("END");
    }

    private void mergesort(int low, int high)
    {
        // Check if low is smaller then high, if not then the array is sorted
        if (low < high)
        {
            // Get the index of the element which is in the middle
            int middle = (low + high) / 2;
            // Sort the left side of the array
            mergesort(low, middle);
            // Sort the right side of the array
            mergesort(middle + 1, high);
            // Combine them both
            merge(low, middle, high);
        }
    }

    private void merge(int low, int middle, int high)
    {
       //printing left half and right half
       System.out.println("Laft Half");//printing left half
       for(int i=0;i<middle;i++)//left half starts from low to middle
       {
           System.out.println(numbers[i]+" ");
       }
       System.out.println(" ");
       System.out.println("Right Half");//printing right half
       for(int i=middle+1;i<high;i++)//right half starts from middle+1 to high
       {
           System.out.println(numbers[i]+" ");
       }
       System.out.println(" ");
        // Copy both parts into the helper array
        for (int i = low; i <= high; i++)
        {
            helper[i] = numbers[i];
        }

        int i = low;
        int j = middle + 1;
        int k = low;

        // Copy the smallest values from either the left or the right side back
        // to the original array
        while (i <= middle && j <= high)
        {
            if (helper[i] <= helper[j])
            {
                numbers[k] = helper[i];
                i++;
            }
            else
            {
                numbers[k] = helper[j];
                j++;
            }
            k++;
        }

        // Copy the rest of the left side of the array into the target array
        while (i <= middle)
        {
            numbers[k] = helper[i];
            k++;
            i++;
        }

    }

    private void printArray()
    {
        for(int x : numbers)
            System.out.print(x + " ");

        System.out.println(" ");
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote