Question

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 ()
   {
       for (int i = 0; i < n; i++)
       {
           cout << array[i] << ' ';
       }
   }

   void swap (float *x, float *y)
   {
       float temp = *x;
       *x = *y;
       *y = temp;
   }

   void merge(int l, int m, int r)
   {
       int i, j, k;
       int n1=m-l+1;
       int n2=r-m;

       int L[n1], R[n2];

       for (i = 0; i < n1; i++)
       L[i]=array[1+i];

       for (j = 0; j < n2; j++)
           R[j]=array[m+1+j];

       i=0;
       j=0;
       k=l;

       while (i < n1 && j < n2)
           {
               if (L[i] <= R[j])
               {
                   array[k]=L[i];
                   i++;
               }
               else
               {
               array[k]=R[j];
               j++;
               }
               k++;
           }
           while (i < n1)
           {
               array[k]=L[i];
               i++;
               k++;
           }
           while (j < n2)
           {
               array[k]=R[j];
               j++;
               k++;
           }
   }
   void mergeSort();
   int l, r;
   {
       if (l < r)
       {
           int m=l+(r-1)/2;
           mergeSort(array,l,m);
           mergeSort(array,m+1,r);
          
           merge(array, l, m , r);

      
       }
   };
  

   int main()
   {
       mergersorter ss;
       clock_t time;

       ss.fillArray();
       time = clock ();
       ss.mergeSort();
       time = clock () - time;
       ss.arrayout ();

       cout << "SORT TIME: " << (float) time / CLOCKS_PER_SEC *
       1000 << " ms";

       system("Pause");

       return 0;
   }

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

If you have any doubts, please give me comment...

#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()

{

for (int i = 0; i < n; i++)

{

cout << array[i] << ' ';

}

}

void swap(float *x, float *y)

{

float temp = *x;

*x = *y;

*y = temp;

}

void merge(int l, int m, int r)

{

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

int L[n1], R[n2];

for (i = 0; i < n1; i++)

L[i] = array[l + i];

for (j = 0; j < n2; j++)

R[j] = array[m + 1 + j];

i = 0;

j = 0;

k = l;

while (i < n1 && j < n2)

{

if (L[i] <= R[j])

{

array[k] = L[i];

i++;

}

else

{

array[k] = R[j];

j++;

}

k++;

}

while (i < n1)

{

array[k] = L[i];

i++;

k++;

}

while (j < n2)

{

array[k] = R[j];

j++;

k++;

}

}

void mergeSort(){

mergeSort(0, n-1);

}

void mergeSort(int l, int r)

{

if (l < r)

{

int m = l + (r - l) / 2;

mergeSort(l, m);

mergeSort(m + 1, r);

merge(l, m, r);

}

}

};

int main()

{

mergersorter ss;

clock_t time;

ss.fillArray();

time = clock();

ss.mergeSort();

time = clock() - time;

ss.arrayout();

cout << "SORT TIME: " << (float)time / CLOCKS_PER_SEC * 1000 << " ms";

system("Pause");

return 0;

}

Add a comment
Know the answer?
Add Answer to:
How would I be able to get a Merge Sort to run in this code? MY...
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
  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

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

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

  • In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 pro...

    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 want to compare the runtimes and swap operations times among quick Sort, selection Sort and...

    I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...

  • I need help of how to include the merge sort code into this counter code found...

    I need help of how to include the merge sort code into this counter code found below: #include<iostream> using namespace std; int bubble_counter=0,selection_counter=0; // variables global void bubble_sort(int [], int); void show_array(int [],int); void selectionsort(int [], int ); int main() { int a[7]={4,1,7,2,9,0,3}; show_array(a,7); //bubble_sort(a,7); selectionsort(a,7); show_array(a,7); cout<<"Bubble counter = "<<bubble_counter<<endl; cout<<"Selection Counter = "<<selection_counter<<endl; return 0; } void show_array(int array[],int size) { for( int i=0 ; i<size; i++) { cout<<array[i]<< " "; } cout<<endl; } void bubble_sort( int array[],...

  • Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below...

    Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below : : Merge Sort algorithm using 2 processes a.) Define an integer array of 100 integers. Populate this array with random numbers. You can use int rand(void); function. Do not forget to initialize this function. You will sort the numbers in the array using merge-sort algorithm. In merge sort algorithm the half of the array will be sorted by one process and second...

  • Not sure if I did the mergesort execution correct as well as the merge execution. Can...

    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 you'd like the array Leendl; int...

  • use the same code. but the code needs some modifications. so use this same code and...

    use the same code. but the code needs some modifications. so use this same code and modify it and provide a output Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...

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