#ifndef Mergesort_h
#define Mergesort_h
#include <cmath>
#include <vector>
Namespace MyNamespace
{
typedef int (*COMPAREFUNC) (void*, void*, void*);
Template<typename t>
void merge (std::vector<t>& array, Comparefunc compare, void* userdefinedobject, int start, int middle, int end)
{
int leftlength = Middle-start + 1;
int rightlength = End-middle;
T Left[leftlength];
T Right[rightlength];
int i;
Int J;
for (i = 0; i < leftlength; ++i)
{
Left[i] = Array[start + i];
}
for (j = 0; j < rightlength; ++j)
{
RIGHT[J] = Array[middle + j + 1];
}
i = 0;
j = 0;
for (int k = start; k <= end; ++k)
{
T leftelement = Left[i];
T rightelement = Right[j];
if (I < leftlength && (j >= Rightlength | | compare (&leftelement, &rightelement, Userdefinedobject) & lt;= 0))
{
Array[k] = leftelement;
++i;
} else if (J < rightlength)
{
Array[k] = rightelement;
++j;
}
}
}
Template<typename t>
void sort (std::vector<t>& array, Comparefunc compare, void* userdefinedobject, int start, int end)
{
if (Start >= end)
{
Return
}
int middle = Std::floor (start + end) * 0.5);
Sort (array, compare, Userdefinedobject, start, middle);
Sort (array, compare, Userdefinedobject, middle + 1, end);
Merge (array, compare, Userdefinedobject, start, middle, end);
}
Template<typename t>
void MergeSort (std::vector<t>& array, Comparefunc comparator, void* userdefinedobject)
{
int left = 0;
int right = Array.size ()-1;
Sort (array, comparator, userdefinedobject, left, right);
}
}
#endif
---------------------------------------------------actual Test----------------------------------------------------------------- ------
int main (void) {
{
int array[]={0, 9, 1, 8, 2, 7, 3, 6, 4, 5};
Std::vector<int> testvector (Array, array+ sizeof (array)/sizeof (array[0]));
MergeSort (Testvector, Compare_sorts, &userdefinedobject);
int expected[]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for (int i=0; i<sizeof (array)/sizeof (array[0]); ++i)
{
if (array[i]! = Expected[i])
{
cout << "sorts:" <<i<<endl;
}
}
}
{
Node array[] = {node (5,0), node (10,1), node (5,2), Node (0,3)};
Std::vector<node> testvector (array, array + sizeof (array)/sizeof (array[0]));
MergeSort (Testvector, Compare_stablesorts, &userdefinedobject);
Node Expected[]={node (0,3), node (5,0), node (5,2), Node (10,1)};
for (int i=0; i<sizeof (array)/sizeof (array[0]); ++i)
{
if (array[i].value! = Expected[i].value | | Array[i].index! = expected[i].index)
{
cout << "Stable sorts:" << i << Endl;
}
}
}
{
Boundingsphere Array[]={boundingsphere (CARTESIAN3 (-2.0, 0.0, 0.0), 1.0),
Boundingsphere (CARTESIAN3 (-1.0, 0.0, 0.0), 1.0),
Boundingsphere (CARTESIAN3 (-3.0, 0.0, 0.0), 1.0)
};
Boundingsphere Expected[]={boundingsphere (CARTESIAN3 (-3.0, 0.0, 0.0), 1.0),
Boundingsphere (CARTESIAN3 (-2.0, 0.0, 0.0), 1.0),
Boundingsphere (CARTESIAN3 (-1.0, 0.0, 0.0), 1.0),
};
Std::vector<boundingsphere> testvector (array, array + sizeof (array)/sizeof (array[0]));
MergeSort (Testvector, Compare_object, &userdefinedobject);
for (int i=0; i<sizeof (array)/sizeof (array[0]); ++i)
{
if (! ( Array[i].equals (Expected[i]))
{
cout << "Sorts with user defined object:" << i << Endl;
}
}
}
}
Write yourself a sort of merge