Question

Please merge all the codes below and add comments using JAVA Program. I need a complete...

Please merge all the codes below and add comments using JAVA Program. I need a complete code which is the combination of the following codes:

// Merges the left/right elements into a sorted result.

// Precondition: left/right are sorted

public static void merge(int[] result, int[] left,

                                       int[] right) {

    int i1 = 0;   // index into left array

    int i2 = 0;   // index into right array

    for (int i = 0; i < result.length; i++) {

        if (i2 >= right.length ||

           (i1 < left.length && left[i1] <= right[i2])) {

            result[i] = left[i1];    // take from left

          i1++;

        } else {

            result[i] = right[i2];   // take from right

            i2++;

        }

    }

}

// Rearranges the elements of a into sorted order using

// the merge sort algorithm.

public static void mergeSort(int[] a) {

    // split array into two halves

    int[] left = Arrays.copyOfRange(a, 0, a.length/2);

    int[] right = Arrays.copyOfRange(a, a.length/2, a.length);

    // sort the two halves

    ...

    // merge the sorted halves into a sorted whole

    merge(a, left, right);

}

// Rearranges the elements of a into sorted order using

// the merge sort algorithm (recursive).

public static void mergeSort(int[] a) {

    if (a.length >= 2) {

        // split array into two halves

        int[] left = Arrays.copyOfRange(a, 0, a.length/2);

        int[] right = Arrays.copyOfRange(a, a.length/2, a.length);

        // sort the two halves

        mergeSort(left);

        mergeSort(right);

        // merge the sorted halves into a sorted whole

        merge(a, left, right);

    }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here, your first function and third function completes MergeSort solution, second function is irrelevant in this code.

The complete answer for this question is as below:

public static void merge(int[] result, int[] left,int[] right) {

       int i1 = 0; // index into left array

       int i2 = 0; // index into right array

       for (int i = 0; i < result.length; i++) {

           if (i2 >= right.length ||

           (i1 < left.length && left[i1] <= right[i2])) {

               result[i] = left[i1]; // take from left

               i1++;

           } else {

               result[i] = right[i2]; // take from right

               i2++;

           }

       }

   }


   // Rearranges the elements of a into sorted order using

   // the merge sort algorithm (recursive).

   public static void mergeSort(int[] a) {

       if (a.length >= 2) {

           // split array into two halves

           int[] left = Arrays.copyOfRange(a, 0, a.length / 2);

           int[] right = Arrays.copyOfRange(a, a.length / 2, a.length);

           // sort the two halves

           mergeSort(left);

           mergeSort(right);

           // merge the sorted halves into a sorted whole

           merge(a, left, right);

       }

   }

Add a comment
Know the answer?
Add Answer to:
Please merge all the codes below and add comments using JAVA Program. I need a complete...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How can I make this program sort strings that are in a text file and not...

    How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please. import java.util.*; public class MergeDemo { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("How many lines to be sorted:"); int size=input.nextInt(); String[] lines=new String[size]; lines[0]=input.nextLine(); System.out.println("please enter lines..."); for(int i=0;i { lines[i]=input.nextLine(); } System.out.println(); System.out.println("Lines Before Sorting:"); System.out.println(Arrays.toString(lines)); mergeSort(lines); System.out.println(); System.out.println("Lines after Sorting:"); System.out.println(Arrays.toString(lines)); } public...

  • In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 pro...

    In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 processes. First half of the array will be sorted by thread 1 and the second half by thread 2. When the threads complete their tasks, the main program will merge the half arrays. You should not waste your time on merge-sort algorithm. Use the merge sort algorithm given below void mergesort(int a[],int i,int j) { int mid; if(i<j) { mid=(i+j)/2; mergesort(a,i,mid); //left recursion mergesort(a,mid+1,j); //right...

  • Please help me to solve the problem with java language! An implementation of the Merge Sort...

    Please help me to solve the problem with java language! An implementation of the Merge Sort algorithm. Modify the algorithm so that it splits the list into 3 sublists (instead of two). Each sublist should contain about n/3 items. The algorithm should sort each sublist recursively and merge the three sorted sublists. The traditional merge sort algorithm has an average and worst-case performance of O(n log2 n). What is the performance of the 3-way Merge Sort algorithm? Merge Sort algorithm...

  • Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below...

    Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below : : Merge Sort algorithm using 2 processes a.) Define an integer array of 100 integers. Populate this array with random numbers. You can use int rand(void); function. Do not forget to initialize this function. You will sort the numbers in the array using merge-sort algorithm. In merge sort algorithm the half of the array will be sorted by one process and second...

  • Please I am in a hurry, I need an answer asap. I have seen an answer previously regarding to this...

    Please I am in a hurry, I need an answer asap. I have seen an answer previously regarding to this question, however I found a lot of mistakes on it, where the guy declared a public class, which is not a thing in C language. Furthermore it is important to divide the question into two C files, one for part a and one for part b. hw2 Merge Sort algorithm using 2 processes a.) Define an integer array of 100...

  • USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list...

    USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list and use a Merge Sort to sort the object by age. Create a class called Person : name and age Create methods that add, and delete Person from the link list Create a method that sorts the persons' objects by age. package mergesort; public class MergeSortExample { private static Comparable[] aux; // auxiliary array for merges public static void sort(Comparable[] a) { aux =...

  • I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on)...

    I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on) with comments: import java.util.ArrayList; public class Mergesort { /** * Sorts list given using the mergesort algorithm * @param list the list to be sorted * @param first the index of the first element of the list to be sorted * @param last the index of the last element of the list to be sorted */ public static void mergesort(ArrayList<Comparable> list, int first, int...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...

    Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer {    // Please expand method main() to meet the lab requirements.       // You have the following sorting methods available:    // insertionSort(int[] a);    // selectionSort(int[] a);    // mergeSort(int[] a);    // quickSort(int[] a);    // The array will be in sorted order after the routines are called!   ...

  • Hey i got the program to work but i cant get the merge sorter from big...

    Hey i got the program to work but i cant get the merge sorter from big to little import java.util.*; class MergeSorter {    public static void sort(int[] a)    { if (a.length <= 1) { return; } int[] first = new int[a.length / 2]; int[] second = new int[a.length - first.length]; for (int i = 0; i < first.length; i++) {    first[i] = a[i]; } for (int i = 0; i < second.length; i++) {    second[i] =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT