Question

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

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

a.

public class ThreeWayMergeSort
{
// sample values
int[] input = {26, 2, 34, 77, 10, 13, 1, 19, 58, 65};
int len = input.length;
public static void main(String args[])
{
int[] data = new int[len];
data = threewayMergeSort(input);
System.out.println("After sorting: ");
for (int i = 0; i < data.length; i++)
System.out.print(data[i] + ", ");
}
  
public static int[] threewayMergeSort(int[] list)
{
// sort function
mergeSortThreeWayRec(list, 0, list.length);
// return this array
return list;
}
// performing the merge sort algorithm on the given array of values in the rangeof indices [low, high]. low is minimum index, high is maximum index.
  
public static void mergeSortThreeWayRec(int[] list, int low, int high)
{
// Splitting array into 3 parts
int mid1 = low + ((high - low) / 3);
int mid2 = low + 2 * ((high - low) / 3) + 1;
  
// Sorting 3 arrays recursively from
// 1. low to mid1
// 2. mid1 to mid2
// 3. mid2 to high
mergeSortThreeWayRec(list, low, mid1);
mergeSortThreeWayRec(list, mid1, mid2);
mergeSortThreeWayRec(list, mid2, high);
// Merging the sorted arrays
merge(list, low, mid1, mid2, high);
}
/* Merge the sorted ranges [low, mid1], [mid1,
mid2] and [mid2, high] mid1 is first midpoint
index in overall range to merge mid2 is second
midpoint index in overall range to merge
*/
public static void merge(int[] list, int low, int mid1, int mid2, int high)
{
//create a temporary destination array for storing the sorted values.
int[] destArray = new int[len];
int i = low, j = mid1, k = mid2, l = low;
/* choose smaller of the smallest in the three parts
int i is used for traversing till mid1 from low
int j is used to traverse from mid1 till mid2
int k is used to traverse from mid2 till high
int l is used for the destination array traversal from low value
*/
// this will happen until all the common length of the three parts are covered
while ((i < mid1) && (j < mid2) && (k < high))
{
if (list[i]<list[j])
{
if (list[i]<list[k])
destArray[l++] = list[i++];
else
destArray[l++] = list[k++];
}
else
{
if (list[j]<list[k])
destArray[l++] = list[j++];
else
destArray[l++] = list[k++];
}
}
// case where first and second parts have remaining values
while ((i < mid1) && (j < mid2))
{
if (list[i]<(list[j])
destArray[l++] = list[i++];
else
destArray[l++] = list[j++];
}
// case where second and third parts have remaining values
while ((j < mid2) && (k < high))
{
if (list[j]<(list[k])
destArray[l++] = list[j++];
else
destArray[l++] = list[k++];
}
// case where first and third parts have remaining values
while ((i < mid1) && (k < high))
{
if (list[i]<(list[k])
destArray[l++] = list[i++];
else
destArray[l++] = list[k++];
}
// copy remaining values from the first part
while (i < mid1)
destArray[l++] = list[i++];
// copy remaining values from the second part
while (j < mid2)
destArray[l++] = list[j++];
// copy remaining values from the third part
while (k < high)
destArray[l++] = list[k++];
// copy all the elements to the initial list.
for(int z=0;z<len;z++)
list[z] = destArray[z];
}
}

b.

T(n) = T(n/3) + T(n/3) + T(n/3) + O(n)

1st T(n/3) is for time taken to solve the first 1/3rd part and the remaining are fro second 1/3rd part and last 1/3rd part respectively. O(n) is for merging the parts together.

Hence, T(n) = 3T(n/3) + O(n)

c.

From the above equation the runtime for 3-way merge sort is nlog3n. You'll get this upon using the telescoping on the above recurrence relation.

Add a comment
Know the answer?
Add Answer to:
4. (15 points) Recall that in merge sort, we partitioned the array into two halves then...
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
  • 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++)...

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

  • . Write a class with an int array as its only instance variable. Write a recursive...

    . Write a class with an int array as its only instance variable. Write a recursive method that uses the following recursive strategy in order to sort the array: ❑ Sort the left half of the array (this is a recursive call). ❑ Sort the right half of the array (this is another recursive call). ❑ Merge the two sorted halves of the array so that the array is sorted (there is no recursive call here).

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

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

  • Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into...

    Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into two parts like Merge sort, 4-way Merge sort splits the array into four parts. 4-way Merge divides the input array into fourths, calls itself for each fourth and then merges the four sorted fourths. a) Give pseudocode for 4-way Merge sort. b) State a recurrence for the number of comparisons executed by 4-way Merge sort and solve to determine the asymptotic running time.

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

  • 6 6. Merge Bubble Sort: a) How does the merge bubble sort break the array into...

    6 6. Merge Bubble Sort: a) How does the merge bubble sort break the array into sublists? b) What does it need to use for each sublist and then what does it do with all of the sublists? c) What is the Big-O notation for this sort? 7. Merge Sort: a) How does the merge sort use recursion to break the array into sublists? b) What happens to each of these sublists to get the final sorted list? c) What...

  • Java Merge sort algorithm

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

  • Mergesort3: Your friend suggests the following variation of Mergesort: instead of splitting the list into two...

    Mergesort3: Your friend suggests the following variation of Mergesort: instead of splitting the list into two halves, we split it into three thirds. Then we recursively sort each third and merge them. Mergesort3 (A[1...n]):    If n <= 1, then return A[1..n]    Let k = n/3 and m = 2n/3    Mergesort3(A[1..k])    Mergesort3(A[k+1..m])    Mergesort3(A[m+1..n) Merge3(A[1..k], A[k+1,..m], A[m+1..n]) Return A[1..m]. Merge3(L0, L1, L2):    Return Merge(L0, Merge(L1,L2)). Assume that you have a function Merge that merges two sorted...

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