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.
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...
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...
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the...
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the list after each outer loop. Do his manually, i.e. step through the algorithm yourself without a computer. This question is related to data structure and algorithm in javascript (.js). Please give your answer keeping this in your mind.
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the process step-by-step, and find the time complexity in Big-O notation for each method. For sorting, use ascending order. 49, 7, 60, 44, 18, 105
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays...
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays of integers. Neither list contains duplicates, and the resulting list should not contain duplicates either. Hint: You may want to call a helper function from merge. PROGRAM: C
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT