1234567891011121314151617181920212223242526272829303132333435363738
#include "MergeSort.h" #include <iostream> #include <cstdlib> using namespace std; int compare(const ItemType&, const ItemType&); int main() { const unsigned MAX = 10; ItemType a[MAX]; for (unsigned i = 0; i < MAX; i++) { a[i] = rand()%2000; } MergeSort(a, MAX, compare); ItemType first = a[0]; for (unsigned i = 1; i < MAX; i++) { ItemType next = a[i]; if (next < first) { cout << "Error" << a[0] << " > " << a[i] << endl; first = next; } } return 0; }; int compare (const ItemType& i1, const ItemType& i2) { return int(i1-i2); }
1234567891011121314151617
void MergeSort(ItemType* base, size_t nelem, int(*fcmp)(const ItemType&, const ItemType&)) { if (nelem > 1) { size_t nelem1 = nelem/2; ItemType* base2 = base + nelem1; MergeSort(base, nelem1, fcmp); mergeSort(base2, nelem - nelem1, fcmp); merge(base, nelem, fcmp); } } void merge(ItemType* base, size_t nelem, int(*fcmp)(const ItemType&, const ItemType&)) { }
1234567
#include <cstdlib> typedef float ItemType; void MergeSort(ItemType*, size_t, int(*fcmp)(const ItemType&, const ItemType&));
1. variable or field "MergeSort" declared void 1. 'ItemType' was not declared in this scope 1. 'base' was not declared in this scope ....
#include