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--];}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images