Question

void merge(Card[] cardArray, int first, int mid, int last) This is a helper method for the...

void merge(Card[] cardArray, int first, int mid, int last) This is a helper method for the merge sort. It takes as parameters a card array, the first index, the middle index, and the end index. The method merges two adjacent sorted arrays into a single sorted one. The first array begins with the element at first and ends with the element at mid. The second array begins with the element at mid + 1 and ends with the element at last. You will have to use the compares method to make the comparisons.

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

// method to merge two adjacent sorted arrays into a single sorted one.

void merge(Card[] cardArray, int first, int mid, int last)
{
   int i,j,k;
  
   // create 2 temporary arrays to keep the two arrays (which are sorted)
   Card tempArray1[] = new Card[mid-first+1];
   Card tempArray2[] = new Card[last-mid];

   // Copy the data from cardArray to temp arrays
   for(i=0;i<tempArray1.length;i++)
       tempArray1[i] = cardArray[first+i];
  
   for(j=0;j<tempArray2.length;j++)
       tempArray2[j] = cardArray[mid+1+j];
  
   // merge both the arrays into cardArray in sorted manner
   for(i=0,j=0,k=first;i<tempArray1.length && j <tempArray2.length;)
   {
       // compareTo method to get the smaller card
       if(tempArray1[i].compareTo(tempArray2[j]) < 0)
       {
           cardArray[k] = tempArray1[i];
           i++;
       }else
       {
           cardArray[k] = tempArray2[j];
           j++;
       }
       k++;
   }
  
   // copy the remaining elements from tempArray1 to cardArray
   for(;i<tempArray1.length;i++)
   {
       cardArray[k] = tempArray1[i];
       k++;
   }
  
   // copy the remaining elements from tempArray1 to cardArray
   for(;j<tempArray2.length;j++)
   {
       cardArray[k] = tempArray2[j];
       k++;
   }
}

Add a comment
Know the answer?
Add Answer to:
void merge(Card[] cardArray, int first, int mid, int last) This is a helper method for the...
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
  • 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...

  • HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge...

    HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge sort (on arrays). Create a public non-final class named Mergesort that extends Merge. Implement a public static method int[] mergesort(int[] values) that returns the input array of ints sorted in ascending order. You will want this method to be recursive, with the base case being an array with zero or one value. If the passed array is null you should return null. If the...

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

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • Write merge method for mergeSort method, it takes the array of data, starting index of first...

    Write merge method for mergeSort method, it takes the array of data, starting index of first half, starting index of second half and end index of second half. please use the code below to test it public class A4Sort{ public static void mergeSort(int[] a, int l, int h){ if (l >= h) return; int mid = (h + l) / 2; mergeSort(a, l, mid); mergeSort(a, mid + 1, h); merge(a, l, mid + 1, h); } public static void main(String[]...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...

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

  • Create a program called Merge.java that implements a variety of the Merge function (in the Merge...

    Create a program called Merge.java that implements a variety of the Merge function (in the Merge Sort algorithm). The program should be able to do the following: accepts two command line parameters, each specifies a text file containing the integers to be merged. The structure of the files is as follows: For both files, there will be multiple lines, each of which contains one integer. The number of lines is unknown. reads the integers from the text files into two...

  • COMPLETE THE BUCKETSORT METHOD public static void bucketSort(int[] array) {        int bucketCount = array.length/2;...

    COMPLETE THE BUCKETSORT METHOD public static void bucketSort(int[] array) {        int bucketCount = array.length/2;        int minIntValue = 0;        int maxIntValue = array.length - 1;        // Create bucket array        List<Integer>[] buckets = new List[bucketCount];        // Associate a list with each index in the bucket array           for(int i = 0; i < bucketCount; i++){            buckets[i] = new LinkedList<>();        }        //...

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
ADVERTISEMENT