Hi,
I am trying to implement a non_recursive version (bottom-up) of mergeSort. When I compile my code, however, my x-code brings up numerous errors that have to do with some files that I did not think I was messing with (stl_algo.h, and stl_algobase.h, and stl_iterator_base_types.h) -I will attach a picture of the list of errors.
This is my code for the non_recursive mergeSort
#include <iostream>
#include <vector>
using namespace std;
#ifndef MERGESORT_H
#define MERGESORT_H
/**
* Mergesort algorithm (driver).
*/
template <typename Comparable>
void mergeSort( vector<Comparable> & a )
{
int n = a.size()-1;
vector<Comparable> aux;
for (int m = 1; m < n; m = m+m)
{
for (int i = 0; i< n-m; i += m+m)
{
/* the last perameter of merge is the min of i+m+m or n*/
int d = i+m+m;
if (d < n) { merge(a, aux, i, i+m, d); }
else { merge(a, aux, i, i+m, n); }
}
}
};
/**
* Internal method that makes NON_RECURSIVE mergeSort
*/
template <typename Comparable>
void mergeSort( vector<Comparable> & a, vector<Comparable> aux, int l, int m, int r)
{
for (int i = l; i<m; i++)
{
aux.at(i) = a.at(i);
for (int j=m; j<r; j++)
{
aux.at(j) = a.at(m+r-j-1);
int i = 1; j = r-1;
for (int k =1; k<r; k++)
{
if (aux.at(j) < aux.at(i)) { a.at(k) =aux.at(j-1); }
else { a.at(k) = aux.at(i+1); }
}
}
}
};
#endif /*mergeSort.h*/
Here is my main where I am testing it:
#include <iostream>
#include <vector>
#include "mergeSort.h"
using namespace std;
int main ()
{
vector<int>test;
test.push_back(6);
test.push_back(4);
test.push_back(2);
test.push_back(3);
test.push_back(1);
test.push_back(5);
test.push_back(8);
cout << "***Non-recursive MergeSort Algorithm***" <<'\n' <<'\n';
//Initial Unsorted List
cout << "Initial Unsorted List: "<<'\n';
for ( int i = 0; i < 7; i++ )
cout << test[i] << " ";
//Calling MergeSort
mergeSort(test);
cout <<"Calling MergeSort" <<'\n';
//Sorted List
cout << "Sorted List: "<<endl;
for ( int i = 0; i < 7; i++ )
cout << test[i] << " ";
return 0;
}
Does anyone see what the problem is here? Thanks a bunch in advance!