Out: 30th November 2010 Type: Medium Due: 8th December 2010 at 11:59pm Note: You can use Eclipse for this assignment if you wish. In class we discussed different algorithms to sort an array in ascending order. This assignment explores a useful operation that you can perform on sorted arrays: merging them so that the merged array is also sorted. Given two arrays A and B of different lengths that are already sorted in ascending order, one can merge them into an array C as follows: 1. Maintain current positions for arrays A and B. 2. If number at current position in A is greater than that in B, add A’s current number to C and advance A’s position. Otherwise add B’s current number to C and advance B’s position. 3. Continue step 2 until you reach the end of one of the two arrays. When you do, copy the remaining elements of the other array to the end of C. What to do: 1. Write a class Merge. It will have no variables. 2. Write a static method merge in Merge that takes two arrays as parameters and returns the merged array using the above algorithm. Make sure your method works even if there are common numbers in the two arrays, and if one or both arrays have just one number in them. You will not receive any points if you use any other method, including putting numbers in another array and then sorting it. 3. Write another static method inplace_merge in Merge. This method takes a single array A as a parameter, along with three indices left, middle and right. Assuming all three are valid indices, this method assumes that the array A is sorted between (left, middle) and (middle+1, right), including the ends. It then merges these two sub-arrays and copies the result back to A between left and right. You may use the above method to accomplish this if you wish. 4. You have almost implemented a popular algorithm to sort an array, mergesort. The pseudocode for mergesort is as follows: mergesort(A,left,right) { if (left >=right) return Compute middle floor((left+right)/2) Call mergesort recursively as mergesort(A,left,middle) Call mergesort recursively as mergesort(A,middle+1,right) Merge the two sorted arrays A(left,middle) and A(middle+1,right) in place using inplace_merge above. // Now the array A is sorted correctly from index ‘left’ to index ‘right’. } Write the above method in the Merge class. Write another method sort(A) that calls the mergesort method so that the array A is sorted. 5. The main method of your program should ask the user for the following input from the keyboard in the given order: a. Number of elements in 1st array. b. Numbers of 1st array. c. Number of elements in 2nd array. d. Numbers of 2nd array. It should then print (1) both arrays as entered by the user (2) sorted arrays using the mergesort algorithm (3) the array obtained by merging the two arrays. Hint: To verify that your merge works correctly, you can use the code from class that sorts arrays using selection, bubble or insertion sort. But make sure that the program you submit uses mergesort to sort the two arrays entered by the user.
Am currently starting to code it now.