Question

Not sure if I did the mergesort execution correct as well as the merge execution. Can you try explaining it visually sort of like how I was approaching it visually. After that translate it to c++ code if possible. I saw another post that used int *L = new int[n1+1] in the merge function which is the same as int L[n1 +1]correct?

Heres my driver function

int main () € cout << Enter length youd like the array Leendl; int size, Aci]; cin » Size ; 1/5 for (int i=0; i< size; it)

A er OT 2 3 4 Merye sort(A; 0,4) if ocu 9= (0+u)/2 112] Merge sort(,0,2) Apr Merge sort [1,2,2) if 262 Il False Moge sort (A,MERGE(A. p.4.r) In = 4-p+1 2 n2 = r-9 3 let L[1..ni + 1) and R[1..12 + 1] be new arrays 4 for i = 1 to ni 5 L[i] = A[P + i -

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer : First thing i found out is when you call merge sort first time it divides the array into two sub arrays MergeSort(A,0,2) and MergeSort(A,3,4). What we need to do is execute them concurrently and then combine them through merge function.

As we know that that merge sort is a divide and conquer technique. In which MergeSort() function recursively calls itself to divide the array into two halves until the size become zero and then a Merge() function is called which combines them. I am writing a tree hierarchy of function calls respective to your question follow it for better understanding. Your merge function is correct and it just comparision. I am attaching a c++ implementation with same variable names as yours so it might be easy for you to understand.SURYA Gold Merge soct 3,4,5,2 10 lo Now merad 1,3,5 2,10 12,3, 5, 10 Pseudo o Size = 5 merge Sort (A, 0, 4); merge Sort (A, 0

Activities Text Editor Fri 11:50 Open. A main.cpp Downloads 5 write your code in this editor and press RUN button to compilActivities Text Editor Fri 11:50 main.cpp Downloads Open Save = 20 A[k] = right[j]; j++; IL k++; | O C while (i < temp1) A[k]Activities Text Editor Open white < LEMZ) Fri 11:50 main.cpp Downloads Save = 20 54 A[k] = right[j]; j++; k++; 58} 59 60 void

Enter Array Elements 3 1 5 2 10 Sorted Array is : 1 2 3 5 10

// C++ Implementation of Merge Sort.....................try to test it and have fun
#include <iostream>

using namespace std;

void Merge(int A[],int p,int x,int q)
{
  
int i, j, k;
int temp1 = x - p + 1;
int temp2 = q - x;
  
int left[temp1], right[temp2];
//initializing the above temporary arrays
for (i = 0; i < temp1; i++)
left[i] = A[p + i];
for (j = 0; j < temp2; j++)
right[j] = A[x + 1+ j];
  
  
i=0;
j=0;
k=p;
while (i < temp1 && j < temp2)
{
if (left[i] <= right[j])
{
A[k] = left[i];
i++;
}
else
{
A[k] = right[j];
j++;
}
k++;
}

while (i < temp1)
{
A[k] = left[i];
i++;
k++;
}
while (j < temp2)
{
A[k] = right[j];
j++;
k++;
}
}

void MergeSort(int A[],int p,int q)
{
if(p<q)
{
int x=p+(q-p)/2;
MergeSort(A,p,x);
MergeSort(A,x+1,q);
  
Merge(A,p,x,q);
}
}

int main()
{
int size;
cout<<"Enter the size of array you want to be:"<<endl;
cin>>size;
int A[size];
cout<<"Enter Array Elements"<<endl;
for(int i=0;i<size;i++)
{
cin>>A[i];
}
  
MergeSort(A,0,size-1);
cout<<"Sorted Array is :"<<endl;
for(int i=0;i<size;i++)
cout<<A[i]<<" ";
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Not sure if I did the mergesort execution correct as well as the merge execution. Can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How would I be able to get a Merge Sort to run in this code? MY...

    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 ()    {   ...

  • Hello, I want to check if my C++ code is correct and follows the requeriments described...

    Hello, I want to check if my C++ code is correct and follows the requeriments described thanks. Requeriments: Assignment Sorting Benchmark each of the sorting methods listed below. Insertion Sort Bubble Sort Selection Sort Heap Sort. Quick Sort. Merge Sort. Benchmark each of the above sorting methods for data sizes of 10000, 20000, 30000, 40000 and 50000. Display the results in a table as shown below. The table should have rows and columns. However, the rows and columns need not...

  • I am working on the divide/conquer algorithm. I am having a trouble with a print for...

    I am working on the divide/conquer algorithm. I am having a trouble with a print for output from reading the file. Here is my work. When you see I put the comment with TODO. that one I am struck with readfile. I wonder if you'd able to help me to fix the readfile to find a single number. Here is the input3.txt (1 12 13 24 35 46 57 58 69). after that, the output should be 0. int mergeInversion(int...

  • you will analyse two algorithms for finding the median of an array of integers. You will...

    you will analyse two algorithms for finding the median of an array of integers. You will compare both algorithms in terms of timing, and hopefully design a hybrid algorithm that uses both, depending on input size. You will write a report describing your experiments and results. the following Java program implements two algorithms for finding the median of an array of integers. The first uses merge sort, and the other implements the recursive linear time selection algorithm, the task is...

  • Lab 10A Measure the program execution time One easy way to measure the program execution time is ...

    Lab 10A Measure the program execution time One easy way to measure the program execution time is encapsulate the useful timing functionality of C++ chrono library, This is illustrated inhttps://www.learncpp.com/cpp-tutorial/8-16-timing-your-code/ The example usingChronoTimer.cpp (Github/m10) is an example program using this chrono Timer class object to measure the Sorting on on an integer array of 10000 random integers based on the Bubble Sort vs. the C++ built-in Sort (an https://www.quora.com/Which-sorting-algorithm-does-STL-standard-template-library-use-in-c++. ) Please measure the performance of sorting the same array used...

  • 1. (25 points) Assume each expression listed below represents the execution time of a program Express...

    1. (25 points) Assume each expression listed below represents the execution time of a program Express the order of magnitude for cach time using big O notation. a. T(n) = ns + 100n . log2 n + 5000 b. T(n) = 2" +n” + 7 d. T(n) 1+2+4+2 2 (75 points+5 extra credit) For cach of the code segments below, determine an equation for the worst-case computing time Tn) (expressed as a function of n, ie. 2n+4) and the order...

  • This is an assignment for my algorithm class which I have written the code partially and...

    This is an assignment for my algorithm class which I have written the code partially and only need to complete it by adding a time function that calculates the average running time. We could use any programming language we want so I am using C++. I am including the instruction and my partial code below. Thank you! Implement linearSearch(a,key) and binarySearch( a,key)functions. Part A.In this part we will calculate theaverage-case running time of each function.1.Request the user to enter a...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    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....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT