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

icon
Related questions
Question
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
4
5
6 // Merge A[p:q] and A[q + 1:r] into A[p:r].
7 MERGE(A, p, q, r)
4
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:
MERGE(A, p, q, r)
1 n₂ = q-p+1
2 nR=r-9
3
let L[0:n
5
6
7
8
9
10
11
return
ANZEIGEN
q = [(p+r)/2]
MERGE-SORT(A, p, q)
MERGE-SORT (A, q + 1,r)
22
25
26
27
i = 0
j = 0
k = p
12
13
14
15
16
17
18
19 // Having gone through one of L and R entirely, copy the
//
remainder of the other to the end of A[p:r].
for i = 0 to n - 1 // copy A[p:q] into L[0:n - 1]
L[i] = A[p + i]
for j = 0 to n R1 // copy A[q+1:r] into R[0:nR-1]
R[j] = A[q+j+1]
20 while i < nL
// length of A[p:q]
// length of A[q + 1:r]
1] and R[0:nR - 1] be new arrays
// 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 in and j<nR
if L[i] ≤ R[j]
A[k] = L[i]
i=i+1
// zero or one element?
// midpoint of A[p:r]
// recursively sort A[p:q]
// recursively sort A[q + 1:r]
24 while j<nR
else A[k] = R[j]
j=j+1
k = k + 1
A[k] = L[i]
i=i+1
k=k+1
// i indexes the smallest remaining element in L
// j indexes the smallest remaining element in R
// k indexes the location in A to fill
A[k] = R[j]
j=j+1
k=k+1
Transcribed Image Text: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 4 5 6 // Merge A[p:q] and A[q + 1:r] into A[p:r]. 7 MERGE(A, p, q, r) 4 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: MERGE(A, p, q, r) 1 n₂ = q-p+1 2 nR=r-9 3 let L[0:n 5 6 7 8 9 10 11 return ANZEIGEN q = [(p+r)/2] MERGE-SORT(A, p, q) MERGE-SORT (A, q + 1,r) 22 25 26 27 i = 0 j = 0 k = p 12 13 14 15 16 17 18 19 // Having gone through one of L and R entirely, copy the // remainder of the other to the end of A[p:r]. for i = 0 to n - 1 // copy A[p:q] into L[0:n - 1] L[i] = A[p + i] for j = 0 to n R1 // copy A[q+1:r] into R[0:nR-1] R[j] = A[q+j+1] 20 while i < nL // length of A[p:q] // length of A[q + 1:r] 1] and R[0:nR - 1] be new arrays // 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 in and j<nR if L[i] ≤ R[j] A[k] = L[i] i=i+1 // zero or one element? // midpoint of A[p:r] // recursively sort A[p:q] // recursively sort A[q + 1:r] 24 while j<nR else A[k] = R[j] j=j+1 k = k + 1 A[k] = L[i] i=i+1 k=k+1 // i indexes the smallest remaining element in L // j indexes the smallest remaining element in R // k indexes the location in A to fill A[k] = R[j] j=j+1 k=k+1
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 2 images

Blurred answer