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.
USING JAVA Almost sort the array using Merge Sort. Perform Merge Sort as usual except that...
USING JAVA Almost sort the array using Merge Sort. Perform Merge Sort as usual except that during the final merge, it is not necessary to merge all n elements, but only the elements in positions 1 to k.
How to write a quick sort using just one recursive call in Java?
How to write a quick sort using just one recursive call in Java?
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...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content after each pass (i.e., from pass 1 to pass 9). Each pass is defined as the completion of one merge operation. Suppose an array contains the following integers: -1, 20, 10, -5, 0, -7, 100, -7, 30, -10. Sort the array using the following algorithms: selection sort, bubble sort, and insertion sort. For each algorithm, write the array content after each pass (i.e., from...
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?
Language: Java Implement Merge Sort
Language: Java Implement Merge Sort
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT