Task - 2: Write a java program (AnyTypeMergeSort.java) to implement the Merge Sort algorithm to sort the array containing any type of elements (suppose strings). Your program will have the following method signature: • public void mergeSort(T[] A, int lowerBound, int upperBound) • public void merge(T[] A, int lowerBound, int midPoint, int upperBound) Following is the Merge Sort Algorithm MERGE-SORT (A, p,r) 1 if p≥r 2 3 q = [(p+r)/2] 4 return MERGE-SORT(A, p, q) 5 MERGE-SORT(A, q + 1,r) 6 // Merge A[p:q] and A[q + 1:r] into A[p:r]. 7 MERGE(A, p, q, r) 6 7 Where p and r represent lower (starting index) and upper (ending index) bounds of array A respectively. The Merge Sort calls the following Merge Algorithm that accepts q as the mid of the array along with p and r: 12 13 14 15 16 17 18 19 MERGE(A, p, q, r) // length of A[p:q] 1 n₁ = q-p+1 nR=r-q 2 // length of A[q +1:r] R[0:nR - 1] be new arrays 3 let L[0:n, -1] and 4 for i = 0 to nL-1 // copy A[p:q] into L[0:n - 1] 5 L[i] = A[p + i] 26 27 // zero or one element? 8 i=0 9 j=0 10 kp 11 // As long as each of the arrays L and R contains an unmerged element, copy the smallest unmerged element back into A[p:r]. // while i < n₂ and j
Task - 2: Write a java program (AnyTypeMergeSort.java) to implement the Merge Sort algorithm to sort the array containing any type of elements (suppose strings). Your program will have the following method signature: • public void mergeSort(T[] A, int lowerBound, int upperBound) • public void merge(T[] A, int lowerBound, int midPoint, int upperBound) Following is the Merge Sort Algorithm MERGE-SORT (A, p,r) 1 if p≥r 2 3 q = [(p+r)/2] 4 return MERGE-SORT(A, p, q) 5 MERGE-SORT(A, q + 1,r) 6 // Merge A[p:q] and A[q + 1:r] into A[p:r]. 7 MERGE(A, p, q, r) 6 7 Where p and r represent lower (starting index) and upper (ending index) bounds of array A respectively. The Merge Sort calls the following Merge Algorithm that accepts q as the mid of the array along with p and r: 12 13 14 15 16 17 18 19 MERGE(A, p, q, r) // length of A[p:q] 1 n₁ = q-p+1 nR=r-q 2 // length of A[q +1:r] R[0:nR - 1] be new arrays 3 let L[0:n, -1] and 4 for i = 0 to nL-1 // copy A[p:q] into L[0:n - 1] 5 L[i] = A[p + i] 26 27 // zero or one element? 8 i=0 9 j=0 10 kp 11 // As long as each of the arrays L and R contains an unmerged element, copy the smallest unmerged element back into A[p:r]. // while i < n₂ and j
Related questions
Question
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 2 images