
Here's my code in C++ so far. I don't understand how to split the array into subarrays then sort each subarray to merge into a final sorted array

HI, Please find my implementation.
Please let me know in case of any issue.
#include <iostream>
using namespace std;
// 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)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int L[n1], R[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there
are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there
are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
/* Function to print an array */
void displayElement(int A[], int size)
{
for (int i=0; i < size; i++)
cout<<A[i]<<" ";
cout<<endl;
}
/* Driver program to test above functions */
int main()
{
int arr[] = {8,2,4,6,9,7,10,1,5,3};
int arr_size = 10;
cout<<"Elements to sort: "<<endl;;
displayElement(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
cout<<"\nResult using merge sort: "<<endl;;
displayElement(arr, arr_size);
return 0;
}

Here's my code in C++ so far. I don't understand how to split the array into...
Please merge all the codes below and add comments using JAVA Program. I need a complete code which is the combination of the following codes: // Merges the left/right elements into a sorted result. // Precondition: left/right are sorted public static void merge(int[] result, int[] left, int[] right) { int i1 = 0; // index into left array int i2 = 0; // index into right array for (int i = 0; i < result.length; i++)...
Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...
Problem 5: Recurrence relations and detailed analysis of recursive algorithm efficiency g(n: non-negative integer) 1. if n ≤ 1 then return n 2. else return (5 * g(n─1) ─ 6 * g(n─2)) MergeSort divides the array to be sorted into two equal halves, calls itself recursively on each half to sort that subarray, and then calls the Merge algorithm to merge the two sorted halves in linear time. This leads to its two recurrence relations T(n)=2T(n/2)+cn, n>1;...
please help urgent c++
Use the vector/array below for the following tasks: {25, 39, 12, 85, 55, 69, 40, 75} Task1 - [2 points] Put your name on the top comment section as the author of this code. For example, // Author: (Your name] Task2 – [5 points] Display (cout) the elements that are greater than 40 in the array. Task 3 - [5 points] Sort the given input array in an ascending order using any sort algorithm learned from...
HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge sort (on arrays). Create a public non-final class named Mergesort that extends Merge. Implement a public static method int[] mergesort(int[] values) that returns the input array of ints sorted in ascending order. You will want this method to be recursive, with the base case being an array with zero or one value. If the passed array is null you should return null. If the...
1)
can any one please givem the code for this
a) If n is a power of 2, as it is in Figure 9-3, you would merge
pairs of individual entries, starting at the
beginning of the array. Then you would return to the beginning of
the array and merge pairs of twoentry
subarrays. Finally, you would merge one pair of four-entry
subarrays. Notice that the subarrays
in each pair of subarrays contain the same number of entries.
In general,...
1. What is the minimum number of locations a binary search algorithm will have to examine when looking for a particular value in a sorted array of 200 elements? (2 points) 1 6 7 8 200 2. In general, which of the following sorting algorithms is the MOST efficient? (2 points) Bubble sort Insertion sort Heap sort Merge sort Selection sort 3. Which of the following are quadratic-sorting algorithms? (2 points) I. insertion sort II. selection sort III. merge sort...
Consider the following mergeSortHelper method, which is part of an algorithm to recursively sort an array of integers. /** Precondition: (arr.length == 0 or 0 <= from <= to <= arr.length) * arr.length == temp.length */ public static void mergeSortHelper(int[] arr, int from, int to, int[] temp) { if (from < to) { int middle = (from + to) / 2; mergeSortHelper(arr, from, middle, temp); mergeSortHelper(arr, middle + 1, to, temp); merge(arr, from, middle, to, temp); } } The merge method...
When sorting using merge sort, I get confused about what going on. Consider the following array A = [7,5,2,8,9,1] and the following code: -------------------------------------------------------------------------------------------------------------------------- int merge_sort(int *input, int start, int end) { if (start < end) { int middle = (start + end ) / 2; merge_sort(input, start, middle); merge_sort(input, middle+1, end); merge(input, start, middle, end); } return 0; } int merge(int *input, int left, int middle, int right) { // determine lenghts int length1 = middle - left +...
please check my answers if it wrong answer me
a) (25 points) Suppose that you are asked to analyze the performance. Algorithms operate on 1D array of size nor 2D a of the algorithms below, write down the Big O or order of grow terms of n. If the algorithm cannot be applied to the array, write 0(1), O(log n), O(n), O(n logn), 90), O(n"), O(n!). The will only be given for reasonable Big O answers. & algorithms for their...