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 + 1;
int length2 = right - middle;
// create helper arrays
int subarray1[length1];
int subarray2[length2];
// fill helper arrays
int i;
for (i=0; i<length1; ++i)
{
subarray1[i] = input[left + i];
}
for (i=0; i<length2; ++i)
{
subarray2[i] = input[middle + 1 + i];
}
subarray1[length1] = 100;
subarray2[length2] = 100;
int j,k;
j = 0;
i = 0;
// go through subarray and insert into main array
for (k=left; k<=right; ++k)
{
if (subarray1[i] <= subarray2[j])
{
input[k] = subarray1[i];
++i;
}
else if (subarray1[i] > subarray2[j])
{
input[k] = subarray2[j];
++j;
}
}
return 0;
}
-------------------------------------------------------------------------------------
Since merge-sort () is called first on array A, the left side of this array would recursively divided using, merge-sort(input, start , middle) until base case (start <end) is met resulting into:
<7,5,2,8,9,1>
<7,5,2>
<7,5>
<7>
Then we can deal with the right using merge-sort(input, middle + 1, end)
<7,5>
<5>
<7,5,2>
<2>
<8,9,1>
<8,9>
...
until we get something like: < 7 > < 5 > < 2 > < 8 > < 9 > < 1 >
This where we should merge(at least I think) using merge (input, start, middle, end)
Which would sort 7 and 5 into < 5 , 7 >
Which would make the first iteration of Merge Sort = [5, 7 , 2 , 8 , 9 , 1]
Am I right, or am I missing something? I have used visualalgo to visualize how Merge sot works, and it is different from the visuals from "introduction to algorithms." I would assume that since we recursively call merge-sort using merge-sort(input, start, middle) the left side is "divided" first since programming works top to bottom, and when the base case is met start >=end that is when we can move to the next recursive call on the right side, aka merge-sort(input, middle + 1, end). Then we can finally "conquer" and "combine" by calling merge(input, start, middle, end), which will create subarrays (1 and 2) of different sizes ( based on what is the size of what we want to sort, aka left, middle and right). Sorry for the rambling, by I hope by giving my approach on how I see this, someone can better assist me with figuring out how the first iteration or pass of Merge sort looks like.
When sorting using merge sort, I get confused about what going on. Consider the following array...
How would I be able to get a Merge Sort to run in this code? MY CODE: #include <iostream> #include <fstream> #include <stdlib.h> #include <stdio.h> #include <time.h> using namespace std; class mergersorter { private: float array[1000] ; int n = 1000; int i=0; public: void fillArray() { for(i=1;i<=n;i++) { array[i-1]= ( rand() % ( 1000) )+1; } } void arrayout () { ...
2. In class, we discussed the recursive Merge-Sort algorithm. This sorts the whole array by sorting the left side, sorting the right side, and then merging them. Write a similar recursive algorithm that finds the maximum element of an array. (Find the max of the left side, then find the maximum of the right side, then compare the two.) Write pseudo-code for this algorithm. Give the recurrence relation that describes the number of comparisons that your algorithm uses.
Please help me to solve the problem with java language! An implementation of the Merge Sort algorithm. Modify the algorithm so that it splits the list into 3 sublists (instead of two). Each sublist should contain about n/3 items. The algorithm should sort each sublist recursively and merge the three sorted sublists. The traditional merge sort algorithm has an average and worst-case performance of O(n log2 n). What is the performance of the 3-way Merge Sort algorithm? Merge Sort algorithm...
[5 marks] Using selection sort algorithm to sort the array int array[7]-5, 6, 2, 7, 9, 4, 3). Assuming we call the following function using statement selection sort (int array, 7); Please fill the table to show the changes of the array int_array after each iteration. The first column is filled. void selection_sort (int list[], int list_size) for (int i = 0; i < list size - 1; 1++) int current min = list[1]; int current_min_index-i for (int j -...
In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 processes. First half of the array will be sorted by thread 1 and the second half by thread 2. When the threads complete their tasks, the main program will merge the half arrays. You should not waste your time on merge-sort algorithm. Use the merge sort algorithm given below void mergesort(int a[],int i,int j) { int mid; if(i<j) { mid=(i+j)/2; mergesort(a,i,mid); //left recursion mergesort(a,mid+1,j); //right...
I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....
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++)...
Implement merge sort and merge
#include <iostream>
using namespace std;
void * merge(int arr[], int start1, int end1, int start2, int
end2){
int * combined = new int[end2-start1 + 1];
}
void mergeSort(int arr[], int start, int end){
//base case: down to 1 item, do nothing
//recursive case:
//MergeSort(left)
//MergeSort(right)
//Merge(left, right)
int m = (end - start) / 2;
if(start==end){ }
else {
mergeSort(arr, start, m);
mergeSort(arr, m+1, end);
merge(arr, start, m, m+1, end);
}
}
void fill(int arr[],...
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...
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...