Write code in java but it should detect runs as described in the comment in the
ID: 3822342 • Letter: W
Question
Write code in java but it should detect runs as described in the comment in the question. Should be natural mergesort.
https://www.chegg.com/homework-help/Data-Structures-and-Algorithms-in-C--4th-edition-chapter-9-problem-13E-solution-9781133608424
On this address this question is in C++ and I want this same code but I want to see this in java.
13. mergesort merges the subarrays of an array that is already in order. Another top-down version of mergesort alleviates this problem by merging only runs, subarrays with ordered elements. Merging is applied only after two runs are determined. For example, in the array 16 7 8 3 4 1 11 12 13 2), runs 16 7 8] and 13 4] are first merged to become (3 4 6 7 8], then runs (1 11 12 13] and [2] are merged to become [1 2 11 12 13, and finally, runs 13 4 678 and [1211 12 13 are merged to become [1 2 3 4 6 7 8 11 12 13]. Implement this algorithm and investigate its complexity. A mergesort that takes advantage of a partial ordering of data (that is, uses the runs) is called a natural sort. A version that disregards the runs by always dividing arrays into (almost) even sections is referred to as straight merging. In orr sorted with mergesortExplanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
public class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of L[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method
public static void main(String args[])
{
int arr[] = {12, 11, 13, 5, 6, 7};
System.out.println("Given Array");
printArray(arr);
MergeSort ob = new MergeSort();
ob.sort(arr, 0, arr.length-1);
System.out.println(" Sorted array");
printArray(arr);
}
}
/*
Sample run:
Given Array
12 11 13 5 6 7
Sorted array
5 6 7 11 12 13
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.