Using Java implementations presented in the algorithms below.Practice Mergesort algorithm with Comparable Java objects whose class definition implements Java's Comparable interface. For example, using sample Comparable Student objects or Comparable Java class objects of your own implementation.
Using Java implementations presented in the algorithms below.Practice Mergesort
Algorithm:
void mergesort(E[] A, E[] temp, int l, int r) { int i, j, k, mid = (l+r)/2; if (l == r) return; // List has one element if ((mid-l) >= THRESHOLD) mergesort(A, temp, l, mid); else inssort(A, l, mid-l+1); if ((r-mid) > THRESHOLD) mergesort(A, temp, mid+1, r); else inssort(A, mid+1, r-mid); // Do merge. First, copy 2 halves to temp. for (i=l; i<=mid; i++) temp[i] = A[i]; for (j=1; j<=r-mid; j++) temp[r-j+1] = A[j+mid]; // Merge sublists back to array for (i=l,j=r,k=l; k<=r; k++) if (temp[i].compareTo(temp[j])<0) A[k] = temp[i++]; else A[k] = temp[j--];}

![Page <
203
of 344
ZOOM
+
Mergesort Implementation
static <E extends Comparable<? super E>>
void mergesort(E[] A, E[] temp, int l, int r) {
int mid = (1+r)/2;
if (1 =
mergesort(A, temp, l, mid);
mergesort (A, temp, mid+1, r);
for (int i=i; i<=r; i++)' // Copy subarray
temp[i] = A[i];
// Do the merge operation back to A
int il
r) return;
1; int i2 = mid + 1;
for (int curr=l; curr<=r; curr++) {
if (il
A[curr] =
else if (i2 > r) ¯// Right sublist exhausted
A[curr] = temp[il++] ;
else if (temp[il].compareTo (temp[i2])<0)
A[curr] = temp[il++];
else A[curr] = temp[i2++] ;
}
}
mid+1) // Left sublist exhausted
temp[i2++] ;](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F33367630-baef-4794-a3c7-3f7d1dfa9a18%2F613037f2-d94a-412f-bc2c-4b9a33f6c636%2F8iumz3r_processed.png&w=3840&q=75)

Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images









