Question

HW58.1. Array Merge Sort Youve done merge (on Lists), so now its time to do merge sort (on arrays). Create a public non-fin

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 import java.util.Arrays; class Merge { public static int[] merge(int[] first, int[] second) { int[] result = new int[first.length + second.length]; int i = 0, j = 0, k = 0; int n1 = first.length; int n2 = second.length; while(i < n1 && j < n2) { if(first[i] <= second[j]) { result[k] = first[i]; i++; } else { result[k] = second[j]; j++; } k++; } while( i < n1) { result[k] = first[i]; k++; i++; } while( j < n2) { result[k] = second[j]; k++; j++; } return result; } public static int[] copyOfRange(int[] original, int from, int to) { return Arrays.copyOfRange(original, from, to); } } public class MergeSort extends Merge { public static int[] mergesort(int[] values) { if (values == null || values.length <= 1) { return values; } int m = values.length/2; int[] left = copyOfRange(values, 0, m); int[] right = copyOfRange(values, m, values.length); left = mergesort(left); right = mergesort(right); if (left != null && right != null) return merge(left, right); else { if (left == null) return right; else return left; } } public static void main(String []args){ int arr[] = {12, 11, 13, 5, 6, 7}; MergeSort ms = new MergeSort(); int[] res = ms.mergesort(arr); for(int i = 0; i < res.length; i++) System.out.print(res[i] + " "); } } 

I have also provided Merge class to help you with that. I have also added helper main method to allow you to execute code. You can remove main method as that is not asked in the class.

Sample exeucteion

 5 6 7 11 12 13 
Add a comment
Know the answer?
Add Answer to:
HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge...
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
  • HW60.1. Array Quicksort You've done partition so now it's time to finish Quicksort. Create a publ...

    HW60.1. Array Quicksort You've done partition so now it's time to finish Quicksort. Create a public non-final class named Quicksort that extends Partitioner. Implement a public static method void quicksort (int] values) that sorts the input array of ints in ascending order. You will want this method to be recursive, with the base case being an array with zero or one value. You should sort the array in place, which is why your function is declared to return void. If...

  • Find the space complexity of Merge Sort below as a function of n (the length of...

    Find the space complexity of Merge Sort below as a function of n (the length of A). Assume: • The elements of A require (1) space. • Merge takes 2 sorted arrays as input and merges them into one sorted array containing both inputs' elements in (n) space. A (there is no index trickery allowing Al and A2 Note that Al and A2 are independent arrays from to be "in" A; A is not being sorted in place). Merge Sort...

  • 4. (15 points) Recall that in merge sort, we partitioned the array into two halves then...

    4. (15 points) Recall that in merge sort, we partitioned the array into two halves then recursively apply merge sort on those two halves. Suppose instead we partitioned the array into three parts. Write a threewayMergeSort(int list) that returns sorted version of list via apply merge sort on three partitions instead of two // ThreeWayMergeSort.java public class ThreeWayMergeSort f public int threewayMergeSort (int [] list) f (a) (10 points) Fill threewayMergeSort methood (b) (2 points) Write the runtime of threeway...

  • 1.) Complete the merge method below, which is the merge step of MergeSort. That is, it...

    1.) Complete the merge method below, which is the merge step of MergeSort. That is, it takes two sorted arrays as input and returns a new sorted array that contains all the elements from the two input arrays. Furthermore, it should keep all duplicated values, and it should run in O(n) time, where n is the size of the output array. public static int[] main(int[] a, int[] b) { }

  • 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...

  • Write a program to merge two sorted arrays into a third array. Ask the user to...

    Write a program to merge two sorted arrays into a third array. Ask the user to enter the size of the two arrays. The length of array1 could be greater than, equal to or less than the length of array2. Fill both with random numbers 0-99. Sort both arrays as explained below. Write a method merge that takes the two arrays as arguments. The method returns a merged array with size equal to the size of both arrays combined. Note:...

  • 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...

  • 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 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 the same code. but the code needs some modifications. so use this same code and...

    use the same code. but the code needs some modifications. so use this same code and modify it and provide a output Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...

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
Active Questions
ADVERTISEMENT