Task - 1: Write a java program (IntegerMergeSort.java) to implement the Merge Sort algorithm to sort the integer array. Your program will have the following method signature: ● ● Following is the Merge Sort Algorithm MERGE-SORT(A, p,r) if p≥r 1 2 3 q = [(p+r)/2] 4 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) MERGE(A, p, q,r) 1 n₁ = q-p+1 2 7 8 public void mergeSort(int[] A, int lowerBound, int upperBound) public void merge(int[] A, int lowerBound, int midPoint, int upperBound) 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: 9 10 11 12 13 14 15 16 17 return nR=r-q 3 let L[0:n - 1] and 4 for i = 0 to n-1 // copy A[p:q] into L[0:n, -1] 5 L[i] = A[p + i] 6 for j = 0 ton R-1 // copy A[q +1:r] into R[0:nR-1] R[j] = A[q+j+ 1] 20 21 22 23 24 25 26 27 i = 0 if L[i] ≤ R[j] // zero or one element? A[k] = L[i] i=i+1 j=0 k = p // 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

icon
Related questions
Question
Task - 1:
Write a java program (IntegerMergeSort.java) to implement the Merge Sort algorithm to
sort the integer array. Your program will have the following method signature:
●
●
Following is the Merge Sort Algorithm
MERGE-SORT(A, p,r)
if p≥r
1
2
1
2
public void mergeSort(int[] A, int lowerBound, int upperBound)
public void merge(int[] A, int lowerBound, int midPoint, int upperBound)
3
4 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)
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)
return
q = [(p+r)/2]
12
13
14
15
16
17
18
19
nR=r-q
3 let L[0:n 1] and R[0:nR - 1] be new arrays
4
for i = 0 to nL-1 // copy A[p:q] into L[0:n - 1]
5
L[i] = A[p + i]
6
7
8
i = 0
9 j = 0
20
21
22
23
24
25
26
27
n = q-p+1
10 k = p
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 in and j<nR
if L[i] ≤ R[j]
for j = 0 ton R-1 // copy A[q+1:r] into R[0:nR - 1]
R[j] = A[q+j+1]
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]
else A[k] = R[j]
j=j+1
while i <NL
A[k] = L[i]
// length of A[p:q]
// length of A[q+1:r]
k=k+1
// Having gone through one of L and R entirely, copy the
remainder of the other to the end of A[p:r].
//
i=i+1
k=k+1
while j<nR
// 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 - 1: Write a java program (IntegerMergeSort.java) to implement the Merge Sort algorithm to sort the integer array. Your program will have the following method signature: ● ● Following is the Merge Sort Algorithm MERGE-SORT(A, p,r) if p≥r 1 2 1 2 public void mergeSort(int[] A, int lowerBound, int upperBound) public void merge(int[] A, int lowerBound, int midPoint, int upperBound) 3 4 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) 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) return q = [(p+r)/2] 12 13 14 15 16 17 18 19 nR=r-q 3 let L[0:n 1] and R[0:nR - 1] be new arrays 4 for i = 0 to nL-1 // copy A[p:q] into L[0:n - 1] 5 L[i] = A[p + i] 6 7 8 i = 0 9 j = 0 20 21 22 23 24 25 26 27 n = q-p+1 10 k = p 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 in and j<nR if L[i] ≤ R[j] for j = 0 ton R-1 // copy A[q+1:r] into R[0:nR - 1] R[j] = A[q+j+1] 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] else A[k] = R[j] j=j+1 while i <NL A[k] = L[i] // length of A[p:q] // length of A[q+1:r] k=k+1 // Having gone through one of L and R entirely, copy the remainder of the other to the end of A[p:r]. // i=i+1 k=k+1 while j<nR // 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
steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS