Question

In: Computer Science

write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.

write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.

Solutions

Expert Solution

public class MergeSort

{

    public static void merge_sort(long[] arr,int p, int r)

    {

        // if index is right

        if(p < r)

        {

            // get the mid index

            int mid = (p + r) / 2;

           

            // sort the left subarray

            merge_sort(arr, p, mid);

           

            // sort the right subarray

            merge_sort(arr, mid + 1, r);

           

            // merge the two subarrays

            merge(arr, p, mid, r);

        }

    }

   

    // merge thw two arrays

    public static void merge(long[] arr,int p,int q,int r)

    {

        // get the length of left subarray

        int n1 = q - p + 1;

       

        // get the length of right subarray

        int n2 = r - q;

       

        int i,j,k;

       

        // create two arrays left and right

        long[] left = new long[n1 + 1];

        long[] right = new long[n2 + 1];

        // fill left with the contents of left subarray of arr

        for(i = 0; i < n1; i++)

            left[i] = arr[p + i];

       

        // fill right with the contents of right subarray of arr

        for(i = 0; i < n2; i++)

            right[i] = arr[q + i + 1];

       

        // set the last element to infinity

        left[n1] = Long.MAX_VALUE;

        right[n2] = Long.MAX_VALUE;

       

        i=0;

        j=0;

       

        for(k = p ; k <= r; k++)

        {

            // if the current element of left is smaller than

            // current element of right

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

            {

                arr[k] = left[i];

                i++;

            }

            // if the current element of left is larger than

            // current element of right

            else

            {

                arr[k] = right[j];

                j++;

            }

        }

    }

   

    // display the array

    public static void display(long arr[])

    {

        int i;

       

        // iterate through the array

        for (i = 0; i< arr.length; i++)

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

       

        System.out.println();

    }

   

    public static void main(String[] args)

    {

        int maxSize = 20;

       

        // initialize the array

        long[] a = new long[maxSize];

       

        // fill the array with random elements

        for(int j = 0; j < maxSize; j++)

        {

            long n = (long)( java.lang.Math.random()*(maxSize-1) );

            a[j] = n;

        }

       

        // sort using quick sort

        merge_sort(a, 0, a.length - 1);

       

        display(a);

    }

}


Sample Output


Related Solutions

Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Write a recursive implementation of insertion sort. Draw a tree of recursive calls for your prototype...
Write a recursive implementation of insertion sort. Draw a tree of recursive calls for your prototype random dataset and compare its big-O efficiency to that of iterative insertion sort. Run your prototype datasets (best, worst, random) from homework 1 and compare the results to the ones for iterative insertion sort. Which version is better? Make sure to present the results in the table form used in homework 1. Include the data for both iterative and recursive versions.
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a...
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a data set (txt file) then do the sorting algorithm to measure how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718...
Using the worst case scenario for a recursive insertion sort on an array of 5 elements...
Using the worst case scenario for a recursive insertion sort on an array of 5 elements {5, 4, 3, 2, 1} Determine a formula that counts the numbers of nodes in that recursion tree. What is the Big O for execution time. Determine a formula that expresses the height of the recursion tree. What is the Big O for memory?
1.)recursive merge sort on a list.(Python) 2.)recursive bubble sort using a list without enumerate() function.(python) Show...
1.)recursive merge sort on a list.(Python) 2.)recursive bubble sort using a list without enumerate() function.(python) Show Base case, and recursive case.
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick sort? Give a simple scheme that makes any sorting algorithm stable. How much additional time and space does your scheme entail?
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge...
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
I'm having trouble implementing the merge function on this recursive merge sort. I am not allowed...
I'm having trouble implementing the merge function on this recursive merge sort. I am not allowed to change the parameter list in the functions. This is what I have so far, written in C. So far I've been getting segfaults, and I feel like it's because I'm off by 1, somewhere... void merge_the_array(int *list, int l1, int h1, int l2, int h2){ // create a temp array for l1-h1 int arr_a[(h1-l1)+1]; // create a temp array for l2-h2, adding 1...
Java program to implement the merge sort your own and test it to sort a list...
Java program to implement the merge sort your own and test it to sort a list of names based on the frequency.
Implement Library Sort in Java which is a version of Insertion Sort with gaps to speed...
Implement Library Sort in Java which is a version of Insertion Sort with gaps to speed up the computation. If the pseudocode in Wikipedia (https://en.wikipedia.org/wiki/Library_sort) is not enough, you can download the research paper from (https://arxiv. org/pdf/cs/0407003.pdf). Below is the algorithm and pseudo code. Implementation Algorithm Let us say we have an array of n elements. We choose the gap we intend to give. Then we would have a final array of size (1 + ε)n. The algorithm works in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT