Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge Sort Issue, passing array between methods?

I am writing a program that implements the quicksort, insertion sort, and merge sort. I can get all working except the merge sort and I can not figure out why. Please ignore the operation and comparison variables. They are so I can analyze the runtime and number of operations for different input files. This code is run from the main through a class object in this fashion:

Merge testobj13(test_Med[0], 100);
    testobj13.merCall();
    testobj13.display();

For a sorted list the class keeps the list sorted, for a reversed list the list still pretty much stays reversed except for the first and last values, for a randomized list I cannot see any pattern between the output and the original input. I will be attempting to answer other questions while I wait for an answer. Any criticism is welcome even if it is just about my code or syntax not relating to the overall issue here. I wrote this code based off of a sudo code written by my algorithms class so Iam having trouble finding out what is wrong here.

# include <iostream>
# include <stdio.h>
# include <stdlib.h>

using namespace std;
class Merge{
public:
    int comparisons, operations, middle, i, j, k, size;
    int *myArray, *c;

Merge::~Merge(){
    myArray = 0;
    delete myArray; 
}

Merge::Merge(int a [], int n) {
    size= n;
    myArray= new int[size];
    comparisons = 0, operations = 0, middle = 0, i = 0, j = 0, k = 0;

    for(int x = 0; x < size; x++){
       myArray[x] = a[x];
    }
}

void combine(int arr [], int first, int middle, int last){

i = first, j = middle + 1, k = first; operations = operations + 3;
c = new int[last + 1]; operations ++;

while( (i <= middle) && (j <= last) ){
    comparisons++;operations++;
    if(arr[i] <= arr[j]){operations++;
        c[k] = arr[i]; operations++;
        i++; operations++;
    }
    else{
        c[k] = arr[j]; operations++;
        j++; operations++;
    }
    k++; operations++;
}
while(i <= middle){operations++;
    c[k] = arr[i]; operations++;
    i++; operations++;
    k++; operations++;
}
while(j <= last){operations++;
    c[k] = arr[j]; operations++;
    j++; operations++;
    k++; operations++;
}
for(int k = first; k <= last; k++){operations++;
    arr[k] = c[k]; operations++;
}
c = 0;
delete c;
}

void mer(int arr [], int first, int last){
operations++; //for the comparison in the following if statement
if ( first < last ){
    middle = (first + last) / 2; operations++;
    mer(arr, first, middle);  operations++;
    mer(arr, middle + 1, last); operations++;
    combine(arr, first, middle, last); operations++;
}
}

void merCall(){
mer(myArray, 0, size - 1);
}

void display(){

cout << "The array after going through Merge Sort: " ;

for(int x = 0; x < size; x++){
    cout << endl << myArray[x];
}

cout << endl << "Number of operations :" << operations << "\t comparisons: " <<   comparisons << endl;

}


};
like image 693
zg303 Avatar asked Aug 07 '26 01:08

zg303


1 Answers

Your "middle" variable is being overwritten during the recursion because it is a class member instead of a local variable:

middle = (first + last) / 2; operations++;

// This is going to affect middle
mer(arr, first, middle);  operations++;

// So this isn't going to work on the range you think it is.
mer(arr, middle + 1, last); operations++;

combine(arr, first, middle, last); operations++;

It would be better to declare middle as a local variable:

int middle = (first + last) / 2; operations++;
mer(arr, first, middle);  operations++;
mer(arr, middle + 1, last); operations++;
combine(arr, first, middle, last); operations++;
like image 75
Vaughn Cato Avatar answered Aug 09 '26 16:08

Vaughn Cato