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.
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.
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
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in...
* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in place. * * @param students array to be sorted, can be empty, but cannot be null */ public static void sortGPA(Student[] students) { // TODO: implement this } Student class: public class Student extends Person { private double GPA; public Student(String lastName, String firstName, double gpa) { super(lastName, firstName); this.GPA = gpa; } public double getGPA() { return GPA; } @Override public boolean equals(Object...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT