Question

In: Computer Science

Language: Java Implement Merge Sort

Language: Java

Implement Merge Sort

Solutions

Expert Solution

import java.util.Arrays;

public class MergeSort {
    /**
     * Recursive helper method for merging arr[l..m] and arr[m+1..r]
     *
     * @param arr
     * @param l
     * @param m
     * @param r
     */
    static void merge(double[] arr, int l, int m, int r) {
        double[] first = new double[m - l + 1];
        double[] second = new double[r - m];
        // copy array arr[l..m] into first
        for (int i = 0; i < first.length; i++)
            first[i] = arr[l + i];
        // copy array arr[m+1..r] into second
        for (int i = 0; i < second.length; i++)
            second[i] = arr[m + 1 + i];
        int ind1 = 0, ind2 = 0, i = l;
        // merge first and second arrays into arr[l..r]
        while (ind1 < first.length || ind2 < second.length) {
            if (ind2 >= second.length || (ind1 < first.length && first[ind1] < second[ind2])) {
                arr[i++] = first[ind1++];
            } else if (ind1 >= first.length || second[ind2] < first[ind1]) {
                arr[i++] = second[ind2++];
            }
        }
    }

    /**
     * Recursive merge sortArray helper method
     *
     * @param arr
     * @param l
     * @param r
     */
    static void mergeSort(double[] arr, int l, int r) {
        if (l < r) {
            // Find the middle point
            int m = (l + r) / 2;

            // SortAll first and second halves
            mergeSort(arr, l, m);
            mergeSort(arr, m + 1, r);

            // Merge the sorted halves
            merge(arr, l, m, r);
        }
    }

    /**
     * merge sortArray method with the normal array input parameter
     *
     * @param arr
     */
    static void mergeSort(double[] arr) {
        mergeSort(arr, 0, arr.length - 1);
    }

    public static void main(String[] args) {
        double[] arr = {0.1, 5, 10.5, 9.8, 3.3, 5.6, 8.5, 7.3, 1.5, 99.9, 30};
        System.out.println("Original array: " + Arrays.toString(arr));
        mergeSort(arr);
        System.out.println("Sorted array: " + Arrays.toString(arr));
    }
}


Related Solutions

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 merge sort,quick sort, and radix sort algorithms in java and test how long it will...
implement merge sort,quick sort, and radix sort algorithms in java and test how long it will take to sort with random data sets of users input numbers.
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.
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.
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.
IN MATLAB!! Q6. Create a 1D array of numbers and implement ‘Merge Sort’ in MATLAB to...
IN MATLAB!! Q6. Create a 1D array of numbers and implement ‘Merge Sort’ in MATLAB to sort it in ascending order
24.10 Lab Ch24: MergeSortDouble Create a class MergeSortDouble and implement a sort(double[]) method using merge sort...
24.10 Lab Ch24: MergeSortDouble Create a class MergeSortDouble and implement a sort(double[]) method using merge sort algorithm. Test will call the sort() method. java please!
merge sort Determine the best, average, and worst case inputs for merge sort.
merge sort Determine the best, average, and worst case inputs for merge sort.
Data structure program Implement (your own) the Radix Sort using single linked list java language
Data structure program Implement (your own) the Radix Sort using single linked list java language
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